is-a关系 类包含关系
构造 拷贝构造函数 重载福值运营商 析构函数
动态内存分配和释放 new delete操作
static 数据成员
好友功能 重载输入>>输出<<操作符
#include#include using namespace std;class person {private: char *name; int age;public: person() {//class memeber has default value is necessary (or error may occur in person& operator=(const person &ps) delete [] name ) cout << "call Person constructor() ..." << endl; name = new char[20]; strcpy(name,"NULL"); age = 0; } person(char *n,int a) { cout << "call Person constructor(char *n,int a)..." << endl; name = new char[strlen(n) + 1]; strcpy(name,n); //name=n;//--error may ocurr when call ~person() delete[] name age=a; } // person(person &ps) { cout << "call Person copy constructor..." << endl; name = new char[strlen(ps.name) + 1]; strcpy(name,ps.name); age = ps.age; } // person& operator=(const person &ps) { cout << "call person operator=() ..." << endl; if(this == &ps) return *this; delete []name; name = new char[strlen(ps.name) + 1]; strcpy(name,ps.name); age = ps.age; return *this;//------return person & } friend istream & operator>>(istream &is, person &ps);//return value type : ---istream & friend ostream & operator<<(ostream &os,const person &ps); void show() { cout<<"name:" << name<<" age:"< << endl; } ~person() { cout << "call person destructor..." << endl; delete [] name; }};istream & operator>>(istream &is, person &ps){ cout << "input name :" << endl; is >> ps.name; cout << "input age:" << endl; is >> ps.age; return is;}ostream & operator<<(ostream &os,const person &ps){ os <<"name:" << ps.name<<" age:"< << endl; return os;}class student{private: person s;//include person class static int sno; char *grade; int score;public: student() { cout << "Call student constructor()..." << endl; grade = new char[20]; strcpy(grade,"no"); sno++; score=0; } student(char *n,int a,char *g,int sc):s(n,a)/*,grade(g)*/,score(sc) { cout << "call Student constructor(char *n,int a...)..." << endl; sno++; grade = new char[strlen(g) + 1]; strcpy(grade,g); cout<<"sno:" << sno << " score:" << score<< " grade: "< < >(istream &is,/*const */student &st) { operator>>(is,st.s);//输入内部对象成员的值 (调用内部类的友元函数--istream & operator>>(istream &,person &ps)) cout << "input sno: " << endl; is >> st.sno; cout << "input score:" << endl; is >> st.score; cout << "input grade:" << endl; is >> st.grade; return is; } friend ostream &operator<<(ostream &os,const student &st) { operator<<(os,st.s);//输出内部对象成员的值 (调用内部类的友元函数 --ostream & operator<<(ostream &,const student &st)) os<<"sno: " << st.sno<<" grade:"< <<" score:"< < >() 。ostream & operator<<()... " << endl; person p; cin >> p; cout << p;// p.show(); cout << "-----------------" << endl; student s; cin >> s; cout << s; //s.display();// system("pause"); student A("Tom",21,"Freshman",99); A.display(); cout << "*************************" << endl; student B(A); B.display(); cout << "*************************" << endl; person p1("li",25); person p2;// p2 = p1; p2.show(); cout << "*************************" << endl; student C = A; C.display(); cout << "************************" << endl; student D; D = B; D.display();}
执行结果: