1 Star 0 Fork 0

CoderQi/cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
继承实验.cpp 5.18 KB
一键复制 编辑 原始数据 按行查看 历史
CoderQi 提交于 2023-05-15 22:15 +08:00 . 实验3(继承)
//#include <iostream> // 预处理命令
//using namespace std; // 使用标准命名空间 std
//class A
//{
//public:
// A() { cout << "构造 A" << endl; } // 构造函数
// ~A() { cout << "析构 A" << endl; } // 析构函数
//};
//class B : public A
//{
//public:
// B() { cout << "构造 B" << endl; } // 构造函数
// ~B() { cout << "析构 B" << endl; } // 析构函数
//};
//class C : public B
//{
//public:
// C() { cout << "构造 C" << endl; } // 构造函数
// ~C() { cout << "析构 C" << endl; } // 析构函数
//};
//int main()
//{
// C obj; // 定义对象
// return 0; // 返回值 0, 返回操作系统
//}
//#include <iostream> // 预处理命令
//using namespace std; // 使用标准命名空间 std
//class A
//{
//protected:
// int a; // 数据成员
//public:
// A(int x) : a(x) { } // 构造函数
// void Show() const { cout << a << endl; } // 显示 a 之值
//};
//class B
//{
//protected:
// int b; // 数据成员
//public:
// B(int x) : b(x) { } // 构造函数
// void Show() const { cout << b << endl; } // 显示 a 与 b 之值
//};
//class C : public A, public B
//{
//public:
// C(int x, int y) : A(x), B(y) { } // 构造函数
// void Show() const // 显示 b 之值
// {
// cout << a << "," << b << endl;
// }
//};
//int main()
//{
// C obj(5, 18); // 定义对象
// obj.Show(); // 显示相关信息
// obj.A::Show(); // 显示相关信息
// obj.B::Show(); // 显示相关信息
// return 0; // 返回值 0, 返回操作系统
//}
//#include <iostream> // 预处理命令
//using namespace std; // 使用标准命名空间 std
//class A
//{
//private:
// int a; // 数据成员
//public:
// A(int x) :a(x) {} // 构造函数
// void Show() const // 显示 a 之值
// {
// cout << "a:" << a << endl;
// }
//};
//class B : public A
//{
//protected:
// int b; // 数据成员
//public:
// B(int x, int y) : A(x), b(y) { } // 构造函数
// void Show() const // 显示相关信息
// {
// A:: Show(); // 调用基类 A 的成员函数 Show()
// cout << "b:" << b << endl; // 显示 b 之值
// }
//};
//int main()
//{
// B obj(5, 18); // 定义对象
// obj.Show(); // 显示相关信息
// system("PAUSE"); // 调用库函数 system( ),输出系统提示信息
// return 0; // 返回值 0, 返回操作系统
//}
//#include <iostream> // 预处理命令
//using namespace std; // 使用标准命名空间 std
//// 基类 Base
//class Base
//{
//private:
// int m; // 数据成员
//public:
// Base(int a) : m(a) { } // 构造函数
// Base(const Base& copy) : m(copy.m) { } // 复制构造函数
// void Show() const // 显示 m 之值
// {
// cout << "m:" << m << endl;
// }
//};
//// 派生类 Derived
//class Derived : private Base
//{
//protected:
// int n; // 数据成员
//public:
// Derived(int a, int b) : Base(a) { this->n = b; } // 构造函数
// void Show() const // 显示相关信息
// {
// Base::Show(); // 调用基类 Base 的成员函数 Show()
// cout << "n:" << n << endl; // 显示 n 之值
// }
//};
//int main() // 主函数
//{
// Derived obj(10, 18); // 定义对象
// obj.Show(); // 显示相关信息
// return 0;// 返回值 0, 返回操作系统
//}
#include <iostream> // 预处理命令
using namespace std; // 使用标准命名空间 std
// 人(Person)类
class Person
{
protected:
char name[18]; // 姓名
int age; // 年龄
char sex[3]; // 性别
public:
Person(char nm[], int ag, char sx[]) // 构造函数
// 由已知参数 nm(姓名), ag(年龄)和 sx(性别)构造对象
{
strcpy(name, nm); // 姓名
age = ag; // 年龄
strcpy(sex, sx); // 性别
}
void Show() const // 显示相关信息
{
cout << "姓名:" << name << endl; // 显示姓名
cout << "年龄:" << age << endl; // 显示年龄
cout << "性别:" << sex << endl; // 显示性别
}
};
// 教师类
class Teacher : virtual public Person
{
protected:
char title[18]; // 职称
public:
Teacher(char nm[], int ag, char sx[], char tl[]) : Person(nm, ag, sx)
{
strcpy(title, tl);
} // 复制职称
void Show() const // 显示相关信息
{
Person::Show(); // 调用基类 Person 的成员函数 Show()
cout << "职称:" << title << endl; // 显示职称
cout << endl; // 换行
}
};
// 干部类
class Cadre : virtual public Person
{
protected:
char post[18]; // 职务
public:
Cadre(char nm[], int ag, char sx[], char pt[]) : Person(nm, ag, sx) // 构造函数
{
strcpy(post, pt);
} // 复制职务
void Show() const // 显示相关信息
{
Person::Show(); // 调用基类 Person 的成员函数 Show()
cout << "职务:" << post << endl; // 显示职务
cout << endl; // 换行
}
};
// 教师兼干部类
class TeacherCadre : public Teacher, public Cadre
{
protected:
double wages; // 工资
public:
TeacherCadre(char nm[], int ag, char sx[], char tl[], char pt[], double wg)
: Person(nm, ag, sx), Teacher(nm, ag, sx, tl), Cadre(nm, ag, sx, pt)
{
wages = wg;
} // 复制工资
void Show() const // 显示相关信息
{
Person::Show(); // 调用基类 Person 的成员函数 Show()
cout << "职称:" << title << endl; // 显示职称
cout << "职务:" << post << endl; // 显示职务
cout << "工资:" << wages << "元" << endl; // 显示工资
cout << endl; // 换行
}
};
int main() // 主函数 main()
{
Teacher objTeacher("文冠杰", 48, "男", "教授"); // 定义对象
Cadre objCadre("周杰", 56, "男", "院长"); // 定义对象
TeacherCadre objTeacherCadre("李靖", 50, "女", "教授", "院长", 6890); // 定义对象
objTeacher.Show(); // 显示相关信息
objCadre.Show(); // 显示相关信息
objTeacherCadre.Show(); // 显示相关信息
return 0; // 返回值 0, 返回操作系统
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/programmercg/cpp.git
git@gitee.com:programmercg/cpp.git
programmercg
cpp
cpp
master

搜索帮助