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

多态性7

时间:2023-04-20 13:34:51浏览次数:32  
标签:fun1 cout 多态性 void virtual Base public

#include<iostream>
using namespace std;
class Base{
public:
virtual void fun1() {
cout <<"Base::fun1()"<<endl;
}
virtual ~Base() {}
};
class Derived1:public Base{
public:
virtual void fun1() {
cout <<"Derived1::fun1()"<<endl;
}
virtual void fun2() {
cout <<"Derived1::fun2()"<<endl;
}
};
class Derived2:public Derived1{
public:
virtual void fun1() {
cout <<"Derived2::fun1()"<<endl;
}
virtual void fun2() {
cout <<"Derived2::fun2()"<<endl;
}
};
void fun(Base *b){
b->fun1();
Derived1 *d=dynamic_cast<Derived1 *>(b);
if(d!=0) d->fun2();
}
int main()
{
Base b;
fun(&b);
Derived1 d1;
fun(&d1);
Derived2 d2;
fun(&d2);
return 0;
}

标签:fun1,cout,多态性,void,virtual,Base,public
From: https://www.cnblogs.com/yuanxinglan/p/17336459.html

相关文章

  • 多态性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.动物的多种形态......
  • 多态性(polymorphism)
    外在表现出多种形式。一。分类  二。抽象类    多态表现在,抽象类自身不能实例化,要实例化必使用其派生的具类。      三。接口和抽象类的抽象......