【类的组合 :类名A 对象】
(对象名())可不是类名
#include <iostream> #include <string.h> using namespace std; class Person { private: string name; string sex; int age; public: Person(string na, string se, int ag) :name(na), sex(se), age(ag) {} Person() {} ~Person() {} void show() { cout << "姓名,性别,年龄为:" << name << " " << sex << " " << age << " " << endl; } }; class Student { private: int gress; Person p; public: Student(string na = "李明", string se = "男", int ag = 20, int gr = 11) :p(na, se, ag), gress(gr) {} void show() { p.show(); cout << "级别为:" << gress << endl; } }; int main() { Student s; s.show(); return 0; }