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

执行结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGlzaGVudGFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

C++ 类包含关系Demo 笔记的更多相关文章

  1. Java程序设计(2021春)——第三章类的重用笔记与思考

    Java程序设计(2021春)--第三章类的重用笔记与思考 本章概览: 3.1 类的继承(概念与语法) 3.2 Object类(在Java继承最顶层的类) 3.3 终结类和终结方法(只能拿来用,不可以 ...

  2. JavaFx 中常见的包和类(javafx笔记 )

    JavaFx 中常见的包和类(javafx笔记 ) 更多详细内容请参考<Pro JavaFX 8>. javafx.stage 包包含以下类: Stage 类 ​ Stage 类是任何 J ...

  3. Qt 串口类QSerialPort 使用笔记

    Qt 串口类QSerialPort 使用笔记 虽然现在大多数的家用PC机上已经不提供RS232接口了.但是由于RS232串口操作简单.通讯可靠,在工业领域中仍然有大量的应用.Qt以前的版本中,没有提供 ...

  4. C++类的静态成员笔记

    下面是C++类的静态成员笔记. 静态数据成员特征 用关键字static声明 为该类的所有对象共享,静态数据成员具有静态生存期 必须在类外定义和初始化,用(::)来指明所属的类 举例说明-具有静态数据成 ...

  5. java 构造方法 constructor demo笔记

    demo 地址 http://pan.baidu.com/s/1bo2FG1T package com.ws.study; /** * @author Administrator * */ publi ...

  6. ILRuntime官方Demo笔记

    调用/执行 热更中的方法 调用热更代码中方法,写在AppDomain中,记录一下主要几个方法: AppDomain.LoadAssembly 加载热更dll 执行热更代码的方法,有两种方式: appd ...

  7. Java中Object类的方法笔记

    今天看了下Object类的源码,以下是我看源码的一些笔记,欢迎有小伙伴来补充~ 首先列举下几个主要方法(面试被问到过的): equals:这个主要是用于比较对象的,Object中比较的是比较原始的,直 ...

  8. python 面向对象(类)--学习笔记

    面向对象是一种编程方式, 主要集中在类和对象的两个概念 python 中的类符合封装, 继承, 多态的特征 类 是一个模板, 是n多函数的集成 对象 是类的实例化 类的成员分为三大类:字段.方法.属性 ...

  9. 安卓Media相关类测试demo

    最近在研究安卓系统给app开发者提供的标准Media相关的工具类,本人做了一些demo来测试这些工具的使用方法. 本demo包含若干apk源码,需要说明以下几点: 1. 构建方式 Makefile使用 ...

随机推荐

  1. input表单验证(全面)

    1.英文字母 1 <script type="text/javascript"> 2 //验证只能是字母 3 function checkZm(zm){ 4 var z ...

  2. Thinkphp5创建控制器

    今天我们就来创建一个控制器: <?php namespace app\index\controller; use think\Controller; class Test extends Con ...

  3. COGS——C 14. [网络流24题] 搭配飞行员

    http://cogs.pro/cogs/problem/problem.php?pid=14 ★★☆   输入文件:flyer.in   输出文件:flyer.out   简单对比时间限制:1 s  ...

  4. 【例题 6-19 UVA - 1572】Self-Assembly

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 旋转和翻转,会发现. 如果可以顺着某个方向一直放的话. 总是能转换成往下或者往右连的. 则只要能够出现一个连接顺序的循环,则总是有解 ...

  5. 【Codeforces Round #431 (Div. 1) B】

    [链接]h在这里写链接 [题意] 场上有 n 个点,它们分别向上与向右在不同时刻开始运动,相遇则改变移动方向,求最终这些点到达的坐标. [题解] 先把每个点的坐标都往它本该移动的方向相反的方向退ti个 ...

  6. Linear to physical address translation with support for page attributes

    Embodiments of the invention are generally directed to systems, methods, and apparatuses for linear ...

  7. (转)yum & wget代理设置

    转自 http://www.cnblogs.com/windows/archive/2012/12/14/2817533.html   yum  配置代理服务器访问要设置所有 yum 操作都使用代理服 ...

  8. [Node.js] Testing ES6 Promises in Node.js using Mocha and Chai

    Writing great ES6 style Promises for Node.js is only half the battle. Your great modules must includ ...

  9. bitnami WAMP stack使用方法(转)

    想学习PHP,在网上找了些资料看了一下.介绍一个简单快速的服务器搭建方法,基于WAMP(WINDOWS+APATCH_MYSQL/MARIADB+PERL/PHP/PYTHON)架构.对应的也有LAM ...

  10. 《大型网站技术架构》1:概述 分类: C_OHTERS 2014-05-07 20:40 664人阅读 评论(0) 收藏

    参考自<大型网站技术架构>第1~3章 1.大型网站架构演化发展历程 (1)初始阶段的网站架构:一台服务器分别作为应用.数据.文件服务器 (2)应用服务和数据服务分离:三台服务器分别承担上述 ...