#include<iostream>
using namespace std;
class Base {
int a;
public:
Base()
{
cout << "嘿嘿" << endl;
}
};
class BBB {
public:
BBB()
{
cout << "哈哈"<<endl;
}
};
class Derived :public BBB ,public Base{
int b;
public:
Derived() { }
};
int main() {
Derived myd1;
return 0;
}
输出结果:
哈哈
嘿嘿
换一下位置:
class Derived : public Base,public BBB
输出:
嘿嘿
哈哈
#include<iostream>
using namespace std;
class Base {
public:
Base()
{
cout << 'A';
}
Base(char c)
{
cout << c;
}
};
class Derived :public Base {
public:
Derived(char c) //输出AB
//如果改为Derived(char c):Base(c) 输出BB
{
cout << c;
}
};
int main() {
Derived d('B');
return 0;
}
输出:AB
如果改为:Derived(char c):Base(c)
输出:BB
#include<iostream>
using namespace std;
class b1 {
public:
b1(int x) { cout << x; }
~b1()
{
cout << 'b';
}
};
class b2 {
public:
b2(int x) { cout << x; }
~b2()
{
cout << 'd';
}
};
class derived :public b2, public b1 {
public:
derived(int x, int y) :b1(y), b2(x)
{
cout << 'e';
}
~derived()
{
cout << 'f';
}
};
int main() {
derived obj(5, 7);
return 0;
}
输出
57efbd