zoukankan      html  css  js  c++  java
  • 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();
    
    }
    


    执行结果:





  • 相关阅读:
    VS2008中 没有QT的代码智能提示
    QT的一个奇怪问题,设置了Qt::Tool后,点击弹出对话框的确定取消按钮,程序直接退出。
    QT线程初次使用。遇到的问题。
    QMenu,QT的菜单添加
    VS2008 不能创建C++的项目,解决方法
    VS2008工具,两种加入库的方法。 设置程序运行时目录
    得到弹出菜单QMenu的高度
    QT 修改QTableWidget表头
    QT两个字符串转化函数,避免文字乱码。
    改变QlistWidget的行高
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5037920.html
Copyright © 2011-2022 走看看