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

}


运行结果:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值