#include <iostream>
#include <string>
using namespace std;
class A
{
public:
virtual int f() const
{
cout << "A::f()" << endl;
return 1;
}
virtual void f(string) const {}
virtual void g() const {}
};
class B : public A
{
public:
void g() const {}
};
class C : public A
{
public:
int f() const // 重写的时候返回的类型不能改变,
{
cout << "C::f()" << endl;
return 1;
}
};
class D :public A
{
public:
int f(int) const
{
cout << "D::A" << endl;
return 2;
}
};
class A1
{
public:
virtual string foodType() const = 0;
};
class A2
{
public:
virtual string type() const = 0;
virtual A1* eats() = 0;
};
class A3 : public A2
{
public:
string type() const
{
return "A3";
}
class A33 : public A1
{
public:
string foodType() const
{
return "A3 to 3";
}
};
A1* eats()
{
return &bf;
}
private:
A33 bf;
};
int main()
{
C c;
c.f();
D d;
d.f(3);
// d.f(); // 这里就不能调用基函数中虚函数int f()
return 0;
}
多态性与虚函数2
最新推荐文章于 2022-10-15 20:04:17 发布