首页 > 其他分享 >多态性8

多态性8

时间:2023-04-20 13:45:55浏览次数:35  
标签:typeid cout 多态性 Base fun info2

#include<iostream>
#include<typeinfo>
using namespace std;
class Base{
public:
virtual ~Base() {}
};
class Derived:public Base{};
void fun(Base *b){
const type_info &info1=typeid(b);
const type_info &info2=typeid(*b);
cout<<"typeid(b): "<<info1.name()<<endl;
cout<<"typeid(*b): "<<info2.name()<<endl;
if(info2==typeid(Base))
cout<<"A base class!"<<endl;
}
int main()
{
Base b;
fun(&b);
Derived d;
fun(&d);
return 0;
}

标签:typeid,cout,多态性,Base,fun,info2
From: https://www.cnblogs.com/yuanxinglan/p/17336488.html

相关文章

  • 多态性7
    #include<iostream>usingnamespacestd;classBase{ public: virtualvoidfun1(){ cout<<"Base::fun1()"<<endl; } virtual~Base(){}};classDerived1:publicBase{ public: virtualvoidfun1(){ cout<<"Derived1::f......
  • 多态性4
    #include<iostream>usingnamespacestd;classBase1{ public: virtualvoiddisplay()const;};voidBase1::display()const{ cout<<"Base1::display()"<<endl;}classBase2:publicBase1{ public: voiddisplay()const;};voidBase2::displ......
  • 多态性3
    #include<iostream>usingnamespacestd;classComplex{ public: Complex(doubler=0.0,doublei=0.0):real(r),imag(i){} friendComplexoperator+(constComplex&c1,constComplex&c2); friendComplexoperator-(constComplex&c1,constComplex......
  • 多态性1
    #include<iostream>usingnamespacestd;classComplex{ public: Complex(doubler=0.0,doublei=0.0):real(r),imag(i){} Complexoperator+(constComplex&c2)const; Complexoperator-(constComplex&c2)const; voiddisplay()const; private: ......
  • iOS7应用开发6:UINavigation, UITabbar控制器的多态性
    1、前期所实现的PlayingCard游戏,其ViewController只能适应PlayingCard这一种游戏规则。而将createDeck函数修改为返回一个nil后,整个ViewController与PlayingCard就没有关......
  • C#多态性学习,虚方法、抽象方法、接口等用法举例
    1.多态性定义  C#中的多态性是OOP(面向对象编程)的一个基本概念,它允许一个对象在不同情况下表现出不同的行为,以增强代码的可重用性和灵活性。  根据网上的教程,我们得......
  • 派生,super 多态与多态性 组合
    派生的方法与重用:方法一:指名道姓的调用某一类函数>>>classTeacher(People):...def__init__(self,name,sex,age,title):...People.__init__(self,name......
  • 面向对象程序设计 第八章 多态性
    目录运行时的多态性(虚函数、纯虚函数、抽象类)  override与final  重载运算符(运算符重载为成员函数)  运算符重载为非成员函数 ......
  • C# 多态性
    多态性:分为静态多态和动态多态1.静态多态相同一个方法名 参数的数量或者类型不一样,这就是静态多态。2.动态多态有抽象类:不同的子类去继承抽象类的方法,实现不同的功......
  • Python类的多态和多态性
    一、多态多态指的是一类事物有多种形态,一个类有很多个子类,因而多态的概念是基于继承的序列数据类型有多种形态:字符串,列表,元组动物有多种形态:人,狗,猪1.动物的多种形态......