编写C++程序,程序中需要体现类的多重继承、类的多级继承、虚继承、公共继承、保护继承、私有继承、子对象、类的组合、虚函数、纯虚函数等特性,须完整各类的构造函数、析构函数。 逻辑自定。
时间: 2023-08-09 18:04:26 浏览: 100
C++编译原理 等等总结
好的,我可以为您提供一个简单的C++程序,它包含类的多重继承、类的多级继承、虚继承、公共继承、保护继承、私有继承、子对象、类的组合、虚函数、纯虚函数等特性。以下是程序的示例:
```
#include <iostream>
using namespace std;
// 基类Base1,公共继承
class Base1 {
public:
Base1() {
cout << "Base1的构造函数" << endl;
}
virtual ~Base1() {
cout << "Base1的析构函数" << endl;
}
virtual void func() {
cout << "Base1的虚函数" << endl;
}
};
// 基类Base2,保护继承
class Base2 {
protected:
Base2() {
cout << "Base2的构造函数" << endl;
}
virtual ~Base2() {
cout << "Base2的析构函数" << endl;
}
virtual void func() {
cout << "Base2的虚函数" << endl;
}
};
// 基类Base3,私有继承
class Base3 {
private:
Base3() {
cout << "Base3的构造函数" << endl;
}
virtual ~Base3() {
cout << "Base3的析构函数" << endl;
}
virtual void func() {
cout << "Base3的虚函数" << endl;
}
};
// 派生类Derived1,多重继承
class Derived1 : public Base1, public Base2 {
public:
Derived1() {
cout << "Derived1的构造函数" << endl;
}
~Derived1() {
cout << "Derived1的析构函数" << endl;
}
void func() {
cout << "Derived1的虚函数" << endl;
}
};
// 派生类Derived2,多级继承
class Derived2 : public Derived1 {
public:
Derived2() {
cout << "Derived2的构造函数" << endl;
}
~Derived2() {
cout << "Derived2的析构函数" << endl;
}
};
// 派生类Derived3,虚继承
class Derived3 : virtual public Base1 {
public:
Derived3() {
cout << "Derived3的构造函数" << endl;
}
~Derived3() {
cout << "Derived3的析构函数" << endl;
}
};
// 派生类Derived4,子对象
class Derived4 {
public:
Derived4() {
cout << "Derived4的构造函数" << endl;
}
~Derived4() {
cout << "Derived4的析构函数" << endl;
}
};
class Derived5 {
public:
Derived5() {
cout << "Derived5的构造函数" << endl;
}
~Derived5() {
cout << "Derived5的析构函数" << endl;
}
};
class ComplexDerived : public Derived4, public Derived5 {
public:
ComplexDerived() {
cout << "ComplexDerived的构造函数" << endl;
}
~ComplexDerived() {
cout << "ComplexDerived的析构函数" << endl;
}
};
// 派生类Derived6,类的组合
class Derived6 {
public:
Derived6() {
cout << "Derived6的构造函数" << endl;
}
~Derived6() {
cout << "Derived6的析构函数" << endl;
}
};
class ComplexDerived2 {
public:
ComplexDerived2() {
cout << "ComplexDerived2的构造函数" << endl;
d6 = new Derived6();
}
~ComplexDerived2() {
cout << "ComplexDerived2的析构函数" << endl;
delete d6;
}
private:
Derived6* d6;
};
// 抽象基类
class AbstractBase {
public:
virtual void func() = 0;
};
// 派生类Derived7,纯虚函数
class Derived7 : public AbstractBase {
public:
Derived7() {
cout << "Derived7的构造函数" << endl;
}
~Derived7() {
cout << "Derived7的析构函数" << endl;
}
void func() {
cout << "Derived7的纯虚函数实现" << endl;
}
};
int main() {
// 多重继承
cout << "多重继承" << endl;
Derived1 d1;
d1.func();
cout << endl;
// 多级继承
cout << "多级继承" << endl;
Derived2 d2;
d2.func();
cout << endl;
// 虚继承
cout << "虚继承" << endl;
Derived3 d3;
d3.func();
cout << endl;
// 子对象
cout << "子对象" << endl;
ComplexDerived cd;
cout << endl;
// 类的组合
cout << "类的组合" << endl;
ComplexDerived2 cd2;
cout << endl;
// 纯虚函数
cout << "纯虚函数" << endl;
Derived7 d7;
d7.func();
cout << endl;
return 0;
}
```
上面的程序中,我们定义了多个基类和派生类,包含了各种不同的继承方式和特性。在程序的主函数中,我们创建了各个类的实例,并调用了它们的构造函数、虚函数等。这个程序可以帮助您更好地理解类的多重继承、类的多级继承、虚继承、公共继承、保护继承、私有继承、子对象、类的组合、虚函数、纯虚函数等特性。
阅读全文