C++类的组合结合应用实例

类的组合

类组合的构造函数设计

原则

不仅要对本类中的基本类型成员数据赋初值,也要对对象成员初始化。

声明形式

类名::类名(对象成员所需的形参,本类成员形参):对象1(参数),对象2(参数),…
{ // 函数体语句 }

构造组合类对象时的初始化次序

①.首先对构造函数初始化列表中列出的成员(包括基本类型成员和对象成员)进行初始化,初始化次序是成员在类体中定义的次序(成员对象构造函数调用顺序:按对象成员的声明顺序)
②.处理完初始化列表之后,再执行#构造函数的函数体。

实例

问题描述

设计一个CRectangle类,其中包括CPoint类的两个对象成员,表示左上角和右下角的两个点。要求求解矩形的面积。
【注意】每个类的构造函数、复制构造函数需要输出“*** is called”,具体的请根据输出进行分析。

样例分析

main函数已经给定如下:

int main()

{

int a=1, b=1, c=6, d=11;

cout<<"# Define p1 ######"<<endl;

CPoint p1;

cout<<"# Define p2 ######"<<endl;

CPoint p2(10,20);

cout<<"# Define rect1 ######"<<endl;

CRectangle rect1;

cout<<"# Define rect2 ######"<<endl;

CRectangle rect2(p1, p2);

cout<<"# Define rect3 ######"<<endl;

CRectangle rect3(a, b, c, d);

cout<<"# Define rect4 ######"<<endl;

CRectangle rect4(rect2);

cout<<"# Calculate area ######"<<endl;

cout << "rect1面积为" << rect1.GetArea() << endl;

cout << "rect2面积为" << rect2.GetArea() << endl;

cout << "rect3面积为" << rect3.GetArea() << endl;

cout << "rect4面积为" << rect4.GetArea() << endl;

system("pause");

return 0;

}

【样例输出】

#Define p1 ######

CPoint contstructor with default value(0,0) is called.

#Define p2 ######

CPoint contstructor with default value(0,0) is called.

#Define rect1 ######

CPoint contstructor with default value(0,0) is called.

CPoint contstructor with default value(0,0) is called.

CRectangle default contstructor is called.

#Define rect2 ######

CPoint copy contstructor is called.

CPoint copy contstructor is called.

CPoint copy contstructor is called.

CPoint copy contstructor is called.

CRectangle contstructor with (CPoint,CPoint) is called.

#Define rect3 ######

CPoint contstructor with default value(0,0) is called.

CPoint contstructor with default value(0,0) is called.

CRectangle contstructor with (int,int,int,int) is called.

#Define rect4 ######

CPoint copy contstructor is called.

CPoint copy contstructor is called.

CRectangle copy contstructor is called.

#Calculate area ######

rect1面积为0

rect2面积为200

rect3面积为50

rect4面积为200

C++代码

#include <iostream>
#include <cstdlib> //如果不加该头文件,之后system("pause")会报错
using  namespace  std;
class CPoint//定义CPoint类
{
private:
    int x,y;
public:
    CPoint(int xx,int yy);//构造函数
    CPoint(CPoint &p);//复制构造函数
    CPoint();//默认构造函数
    int getX() {return x;}
    int getY() {return y;}
};
CPoint::CPoint(int xx,int yy)//构造函数的实现
{
    x = xx;
    y = yy;
    cout<<"CPoint contstructor with default value(0,0) is called."<<endl;
}
CPoint::CPoint(CPoint &p)//复制构造函数
{
    x = p.x;
    y = p.y;
    cout<<"CPoint copy contstructor is called."<<endl;
}
CPoint::CPoint()//默认构造函数
{
    x = 0;
    y = 0;
    cout<<"CPoint contstructor with default value(0,0) is called."<<endl;
}
class CRectangle//定义
{
private://私有数据成员
    CPoint p1,p2;//CPoint类对象的p1,p2
    int area;
public://外部接口
    CRectangle(CPoint xp1,CPoint xp2);
    CRectangle(int x1,int y1,int x2,int y2);
    CRectangle(CRectangle &rec);
    CRectangle();
    int GetArea();
};
//组合类的构造函数1
CRectangle::CRectangle(CPoint xp1,CPoint xp2):p1(xp1),p2(xp2)
{
    cout<<"CRectangle contstructor with (CPoint,CPoint) is called."<<endl;
    int x = static_cast<int>(p2.getX()-p1.getX());
    int y = static_cast<int>(p2.getY()-p1.getY());
    area = x*y;
}
//组合类的构造函数2
CRectangle::CRectangle(int x1,int y1,int x2,int y2):p1(x1,y1),p2(x2,y2)
{
    cout<<"CRectangle contstructor with (int,int,int,int) is called."<<endl;
    area = (x2-x1)*(y2-y1);
}
//组合类的复制构造函数
CRectangle::CRectangle(CRectangle &rec):p1(rec.p1),p2(rec.p2)
{
    cout<<"CRectangle copy contstructor is called."<<endl;
    area = rec.area;
}
//组合类的默认构造函数
CRectangle::CRectangle()
{
    cout<<"CRectangle default contstructor is called."<<endl;
}
int CRectangle::GetArea()
{
    return area;
}
int  main()
{
        int  a=1,  b=1,  c=6,  d=11;
        cout<<"#  Define  p1  ######"<<endl;
        CPoint  p1;//建立CPoint类的对象p1
        cout<<"#  Define  p2  ######"<<endl;
        CPoint  p2(10,20);//建立CPoint类的对象p2
        cout<<"#  Define  rect1  ######"<<endl;
        CRectangle  rect1;//建立CRectangle类的对象rect1
        cout<<"#  Define  rect2  ######"<<endl;
        CRectangle  rect2(p1,  p2);//建立CRectangle类的对象rect2
        cout<<"#  Define  rect3  ######"<<endl;
        CRectangle  rect3(a,  b,  c,  d);//建立CRectangle类的对象rect3
        cout<<"#  Define  rect4  ######"<<endl;
        CRectangle  rect4(rect2);//利用复制构造函数建立CRectangle类的对象rect4
        cout<<"#  Calculate  area  ######"<<endl;
        cout  <<  "rect1面积为"  <<  rect1.GetArea()  <<  endl;
        cout  <<  "rect2面积为"  <<  rect2.GetArea()  <<  endl;
        cout  <<  "rect3面积为"  <<  rect3.GetArea()  <<  endl;
        cout  <<  "rect4面积为"  <<  rect4.GetArea()  <<  endl;
        system("pause");
        return  0;
}

运行结果为:

本文到此就结束了,如有错误,欢迎大家及时批评指正!

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芷汀若静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值