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. 1.1selenium 介绍

    1.1selenium 介绍selenium 是一个 web 的自动化测试工具,不少学习功能自动化的同学开始首选 selenium , 相因为它相比 QTP 有诸多有点:* 免费,也不用再为破解 QT ...

  2. What is corresponding Cron expression to fire in every X seconds, where X > 60? --转载

    原文地址:http://stackoverflow.com/questions/2996280/what-is-corresponding-cron-expression-to-fire-in-eve ...

  3. (转)Oracle EXP-00091解决方法

    转自:http://blog.csdn.net/dracotianlong/article/details/8270136 EXP-: 正在导出有问题的统计信息. . . 正在导出表 WF_GENER ...

  4. 16进制串与ASCII字符串相互转换

    提供两个函数,方便十六进制串与ASCII 字符串之间的相互转换,使用函数需要注意的是返回的串是在堆上通过 calloc 分配的,所以,记得使用完返回值释放该块,并且将指向该块的指针 =NULL .// ...

  5. 【Codeforces Round #301 (Div. 2) E】Infinite Inversions

    [链接] 我是链接,点我呀:) [题意] 给你一个无限长的序列1,2,3,4... 然后给你n个操作. 每个操作ai,bi; 表示调换位置为ai和位置为bi的数的位置. (ai,bi<=10^9 ...

  6. 利用Eclipse+openJTAG调试led.axf文件

    转自calvinlee1984 Subject:利用Eclipse+openJTAG调试led.axf文件 Date:     3-Mar-2011 By:         Calvinlee1984 ...

  7. 10.12 android输入系统_InputStage理论

    android应用程序对输入系统的处理分为多个阶段,我们把这些阶段称为InputStage 理论处理流程: (1)activity发给window,如果window不能处理,再由activity处理; ...

  8. MySQL的安装及使用教程

    MySQL的安装及使用教程 一.  MySQL的下载及安装 首先登陆MySQL的官网,选择Downloads→Windows→MySQL Installer→Windows(x86,32-bit),M ...

  9. Caffe 安装 Ubuntu14.04+CUDA7.0/7.5(亲测有效)

    自己安装Caffe的过程中,参考了很多资料,但由于版本或者其他原因,安装过程中总是遇到这样或者那样的问题,因此留做记录,方便之后查看,也希望对遇到相似麻烦的朋友们提供帮助.  下面我们开始安装吧: 硬 ...

  10. 美国汪利宏的蒙特卡洛及卷积模拟程序,可以模拟top-hat光束和高斯光束在生物组织中的传输

    链接:https://pan.baidu.com/s/1yaCsQ8TCVPSIZ4TVBZgfnw 密码:otzr