首页 > 代码库 > C++ 类包含关系Demo 笔记
C++ 类包含关系Demo 笔记
is-a关系 类包含关系
构造函数 复制构造函数 重载赋值操作符 析构函数
动态内存空间分配和释放 new delete操作
static 数据成员
友元函数 重载输入>>和输出<<操作符
#include<iostream> #include <windows.h> 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:"<<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:"<<ps.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: "<<grade<<endl; } //the derived class also contains: dynamic memeory allocate //student(student &s1):s(s1.s)//-----:s(s1) [inner class] student(student &s1) { cout << "Call Student copy constructor ..." << endl; s = s1.s;//------- sno=s1.sno+1; score=s1.score; grade = new char[strlen(s1.grade) + 1]; strcpy(grade,s1.grade); } student & operator=(const student &st) { cout << "call Student operator=() ..." << endl; if(this == &st) { cout << "this == &st" << endl; return *this; } delete [] grade; s = st.s;//----------------------------- sno = st.sno; score = st.score; grade = new char[strlen(st.grade) + 1]; strcpy(grade,st.grade); return *this; } friend istream & operator>>(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:"<<st.grade<<" score:"<<st.score<<endl; return os; } void display() { s.show(); cout<<"sno: " << sno<<" grade:"<<grade<<" score:"<<score<<endl; } ~student() { cout << "call student destructor ..." << endl; delete [] grade; } }; int student :: sno=2014001; void main() { cout << "test friend istream & operator>>() ,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(); }
运行结果:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。