C++程序:有一个类Date包含int year int month int day三个protected数据。有一个基类Person包含string namechar sexint age三个protected数据 和 一个print函数用于输出数据信息。有一个Person的派生类Student新增int ID 、Date birthday、Person mother三个数据成员。要求每个
以下是C++程序:
#include
class Date { protected: int year, month, day; public: Date(int y, int m, int d) { year = y; month = m; day = d; } };
class Person { protected: string name; char sex; int age; public: Person(string n, char s, int a) { name = n; sex = s; age = a; } void print() { cout << "Name: " << name << endl; cout << "Sex: " << sex << endl; cout << "Age: " << age << endl; } };
class Student : public Person { private: int ID; Date birthday; Person mother; public: Student(string n, char s, int a, int id, Date bd, Person m) : Person(n, s, a), birthday(bd), mother(m) { ID = id; } void print() { Person::print(); cout << "ID: " << ID << endl; cout << "Birthday: " << birthday.year << "/" << birthday.month << "/" << birthday.day << endl; cout << "Mother's Name: " << mother.name << endl; } };
int main() { Date bd(1999, 8, 15); Person m("Mary", 'F', 40); Student st("John", 'M', 20, 123456, bd, m); st.print(); return 0;
原文地址: https://www.cveoy.top/t/topic/fcWL 著作权归作者所有。请勿转载和采集!