博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 类包含关系Demo 笔记
阅读量:6453 次
发布时间:2019-06-23

本文共 3242 字,大约阅读时间需要 10 分钟。

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();}

执行结果:

你可能感兴趣的文章
java 代替Python_Java总是“沉沉浮浮”,替代者会是Python?
查看>>
贪吃蛇java程序简化版_JAVA简版贪吃蛇
查看>>
poi java web_WebPOI JavaWeb 项目 导出excel表格(.xls) Develop 238万源代码下载- www.pudn.com...
查看>>
java 顶点着色_金属顶点着色器绘制纹理点
查看>>
php扩展有哪些G11,php 几个扩展(extension)的安装笔记
查看>>
ajax长连接 php,ajax怎么实现服务器与浏览器长连接
查看>>
oracle报1405,【案例】Oracle报错ORA-15054 asm diskgroup无法mount的解决办法
查看>>
linux 脚本map,Linux Shell Map的用法详解
查看>>
如何在linux系统下配置共享文件夹,如何在windows和Linux系统之间共享文件夹.doc
查看>>
thinkpad装linux无线网卡驱动,ThinkPad E530 Fedora 20 下无线网卡驱动的安装
查看>>
linux操作系统加固软件,系统安全:教你Linux操作系统的安全加固
查看>>
linux中yum源安装dhcp,24.Linux系统下动态网络源部署方法(dhcpd)
查看>>
ASP.NET性能优化之分布式Session
查看>>
TaffyDB Introduction
查看>>
转载:《TypeScript 中文入门教程》 16、Symbols
查看>>
JavaScript、jQuery、HTML5、Node.js实例大全-读书笔记4
查看>>
C#技术------垃圾回收机制(GC)
查看>>
漫谈并发编程(三):共享受限资源
查看>>
【转】github如何删除一个仓库
查看>>
Linux系统编程——进程调度浅析
查看>>