多继承
代码示例
#include "iostream"
using namespace std;
class base1
{
public:
base1() {}
base1(int a, int b) : a(a), b(b) {}
int a;
protected:
int b;
};
class base2
{
public:
base2() {}
base2(int a, int b) : c(a), d(b) {}
int c;
protected:
int d;
};
class step : public base1, public base2
{
public:
step(int a, int b) : base1(a, b), base2(a, b)
{
}
void show878()
{
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
}
};
int main()
{
base1 xy1(1, 2);
base2 xy2(3, 4);
step xy3(10, 20);
xy3.show878();
}
多级继承
代码示例
#include "iostream"
using namespace std;
class base
{
public:
void show1()
{
cout << "base" << endl;
}
};
class step1 : public ::base
{
public:
void show2()
{
cout << "step1" << endl;
}
};
class step2 : public ::step1
{
public:
void show3()
{
cout << "step2" << endl;
}
};
int main()
{
step2 rlxy;
rlxy.show1();
rlxy.show2();
rlxy.show3();
}