cpt4-组合类及其成员的初始化(复制构造函数隐式及显式使用)

一、知识点

组合类:使用其他类的对象作为成员的类称为组合类。
初始化要点:组合类中的对象成员需要使用参数初始化列表形式进行初始化。
组合类构造函数初始化语法:
类名::类名(对象成员参数,本类成员参数):对象1(参数1),对象2(参数2)
{
//函数
}

二、代码举例(自写复制构造函数)

1.说明:实现描述线段的类,成员包括两个端点类和长度。
2.实现:

//#include "stdafx.h" --VS2013运行需要。

#include<iostream>
using namespace std;
#include<cmath>

class Point
{
private:
	int x; int y;
public:
	Point();
	Point(int a, int b)
	{
		x = a;
		y = b;
		cout << "caling constructer fun of point" << endl;
	}
	int getx(){ return x; }
	int gety(){ return y; }
	Point(Point &p);
};
Point::Point(Point &p)
{
	x = p.x; y = p.y;
	cout << "calling copy constructer fun of point" << endl;
}
class Line
{
private:
	double len;
	Point lp1; Point lp2;
public:
	Line(Point p1, Point p2);
	Line(Line &l);
	double getlen(){ return len; }
};

Line::Line(Point p1, Point p2) :lp1(p1), lp2(p2)
{
	cout << "calling constructer fun of line" << endl;
	/*强制类型转换*/
	double dx = static_cast<double>(lp1.getx() - lp2.getx());//error:(lp1.x - lp2.x)
	double dy = static_cast<double>(lp1.gety() - lp2.gety());//ERROR: (lp1.y - lp2.y)
	len = sqrt(dx*dx + dy*dy);
}
Line::Line(Line &l) :lp1(l.lp1), lp2(l.lp2)
{
	// error:lp1 = l.lp1; lp2 = l.lp2;
	len = l.len;
	cout << "calling copy constructer fun of line" << endl;
}
int main()
{
	Point mp1(3, 0); Point mp2(0, 4);
	/*上句调用两次点的构造函数*/
	Line mline1(mp1, mp2);
	/*上句调用4次点的复制构造函数,形实结合(情形2)2次,初始化列表复制点信息
	2次*/
	cout << "Next step will copy mline to myline2" << endl;
	Line mline2(mline1);
	/*上句先后调用点的复制构造函数2次,线段构造函数1次*/
	cout << "The length of line1=" << mline1.getlen() << endl;
	cout << "The length of line2=" << mline2.getlen() << endl;
	return 0;
}

程序运行结果:
运行结果

代码举例(使用系统默认复制构造函数)

1.说明: 实现描述电脑的类,该类包含CPU和RAM两个类
2.实现:

#include<iostream>
using namespace std;
enum cpuRank{p1=1,p2,p3};//need a ';'
class Cpu
{
private:
    cpuRank rank; int frequence; int voltage;
public:
Cpu();
Cpu(cpuRank r,int f, int v);
~Cpu(){cout<<"~Cpu"<<endl;}
cpuRank getrank(){return rank;}
int getfrequence(){return frequence;}
int getvoltage(){return voltage;}
void run(){cout<<"Cpu start run"<<endl;}
void stop(){cout<<"Cpu stop run"<<endl;}
};
Cpu::Cpu(cpuRank r, int f, int v)
{
    rank=r; frequence=f; voltage=v;
     cout<<"calling constructer of Cpu"<<endl;
}
enum ramType{DDR1=1,DDR2,DDR3};
class Ram
{
    private:
    ramType rtype; int size; int frequence;
    public:
    Ram(ramType t, int s, int f)
    {
    rtype=t; size=s; frequence=f;
     cout<<"calling constructer of Ram"<<endl;
    }
    ~Ram(){cout<<"~Ram()"<<endl;}
    Ram();
    ramType gettype(){return rtype;}
    int getsize(){return size;}
    int getfrequence(){return frequence;}
    void run(){cout<<"Ram start run"<<endl;}
    void stop(){cout<<"Ram stop run"<<endl;} 
};
class Computer
{
    private:
    Cpu mycpu; Ram myram;
    int bandwidth;
    int storage;
    public:
    Computer();
    Computer(Cpu c, Ram r, int b,int s);
    ~Computer(){cout<<"~Computer"<<endl;}
    void run();
    void stop();
    int getbandwidth(){return bandwidth;}
    int getstorage(){return storage;}
};
Computer::Computer(Cpu c, Ram r, int b,int s):mycpu(c),myram(r)
{
    bandwidth=b;
    storage=s;
    cout<<"calling constructer of computer"<<endl;
}
void Computer::run()// run with no type of "void"
{
    mycpu.run();
    myram.run();
}
void Computer::stop()
{
    mycpu.stop();
    myram.stop();
}
int main()
{
    Cpu a(p1, 10,20);
    Ram b(DDR2,30,40);
    Computer l(a,b,50,60);
    l.run();
    l.stop();
    cout<<"computer bandwidth is "<<l.getbandwidth()<<endl;
    return 0;
}

运行结果:
在这里插入图片描述
结果说明:
(1)打印出3次调用构造函数,而析构函数打印7次,正是因为调用Computer构造函数时,调用了4次默认复制构造函数,其中形实结合两次,复制拷贝2次。
(2)最后两个析构函数对应main函数前两句;前两个析构函数对应系统默认复制构造函数在形实结合阶段的构造函数;中间三个析构函数就是Computer构造函数对应的析构函数和内嵌两个对象复制赋值的对应的复制构造函数。
(3)最后5个析构函数在“return 0”处设置断点还没打印出来,在其后一句设置断点才打印出来。

–以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值