C++作业 设计一个CRectangle类,其中包括CPoint类的两个对象成员,表示左上角和右下角的两个点。要求求解矩形的面积。

该代码示例展示了如何在C++中定义一个表示矩形的CRectangle类,该类包含两个CPoint对象作为其边界点。CPoint类有默认和拷贝构造函数,CRectangle类则有默认、参数化和拷贝构造函数。程序通过实例化这些类并计算矩形面积来演示了构造函数的使用。输出显示了构造函数的调用情况,并给出了各矩形的面积。
摘要由CSDN通过智能技术生成

【问题描述】设计一个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> 
using  namespace  std; 
//
class CPoint
{
public :
	CPoint(int x1, int y1);
	CPoint(CPoint &p);
	int getx() { return x ;}
	int gety() { return y ;}
	~CPoint(){};
private :
	int x, y;	
};
//
class CRectangle
{
public :
	CRectangle();
	CRectangle(CPoint xp1, CPoint xp2);
	CRectangle(int a, int b, int c, int d);
	CRectangle(CRectangle &p);
	int GetArea();
	~CRectangle(){};
private :
	CPoint p1,p2;
};
//
CPoint :: CPoint(int x1=0, int y1=0)
{
	x = x1;
	y = y1;
	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;
}
//
CRectangle :: CRectangle() : p1(0,0),p2(0,0)
{
	cout << "CRectangle default contstructor is called." << endl;
}
CRectangle :: CRectangle(CPoint xp1, CPoint xp2) : p1(xp1),p2(xp2)
{
	cout << "CRectangle contstructor with (CPoint,CPoint) is called." << endl;
}
CRectangle :: CRectangle(int a, int b, int c, int d) : p1(a,b),p2(c,d)
{
	cout << "CRectangle contstructor with (int,int,int,int) is called." << endl;
}
CRectangle :: CRectangle(CRectangle &p) : p1(p.p1),p2(p.p2)
{
	cout << "CRectangle copy contstructor is called." << endl;
}
int CRectangle :: GetArea()
{
	return (p1.getx()-p2.getx())*(p1.gety()-p2.gety());
}
//
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; 
} 

在这里插入图片描述

#include <iostream> #include <string> using namespace std; struct CPoint { int x ; int y ; }; class CRectangle { private: const int id;//常量数据成员 static int total;//静态数据成员 const static string sclass; const static int f=1.0f; CPoint lefttop ; CPoint rightdown ; public: CRectangle( ); CRectangle( CPoint& lt, CPoint& rd ); CPoint GetLefttop() const { return lefttop; } CPoint GetRightdown() const { return rightdown; } void SetLefttop(CPoint & pt) { lefttop=pt; } void SetRightdown(CPoint & pt) { rightdown=pt; } int Getid() const { return id; } static int Gettotal() { return total; } int Area( ) const; int Perimeter( ) const; }; int CRectangle::total=0;//静态数据成员必须在的外部定义(正好一次)。 const string CRectangle::sclass="CRectangle"; CRectangle::CRectangle( ):id(++total) { lefttop.x=0; lefttop.y=0; rightdown.x=1; rightdown.y=1; } CRectangle::CRectangle( CPoint& lt, CPoint& rd ):id(++total) { lefttop = lt ; rightdown = rd ; } int CRectangle::Area( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return wd * ht ; } int CRectangle::Perimeter( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return 2 * ( wd + ht ) ; } int main() { CPoint lt, rd; cin >> lt.x >> lt.y; cin >> rd.x >> rd.y; CRectangle crt(lt,rd);//调用有参构造函数 CRectangle crt2;//调用默认构造函数 //创建常量对象 const CRectangle crt3(lt,rd); cout<<"当前创建的矩形个数为:"; cout<<CRectangle::Gettotal()<<endl; //返回矩形的左上和右下 CPoint lt1=crt.GetLefttop(); CPoint lt2=crt.GetRightdown(); //显示矩形的坐标 cout<<crt.Getid()<<"号矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示矩形面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; //修改矩形左上角 cout<<"请输入矩形新的左上坐标:"; cin>> lt.x>>lt.y; crt.SetLefttop(lt); lt1=crt.GetLefttop(); //显示修改后矩形的坐标 cout<<"矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示修改后矩形面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; }
设计一个长方形rectangle class),可以使用以下步骤来计算长方形的周长和面积的基本原理: 1. 定义:首先需要定义一个长方形,可以命名为Rectangle。 2. 定义属性:长方形有两个重要的属性,即长度(length)和宽度(width)。在的定义中,需要包含两个属性。 3. 构造函数:为了方便创建长方形对象并初始化属性,可以在中定义一个构造函数。构造函数接受长度和宽度的输入,并将其赋值给的属性。 4. 成员函数:在中定义两个成员函数,用于计算长方形的周长和面积。 - 计算周长函数:定义一个成员函数,命名为calculate_perimeter,该函数使用长度和宽度属性来计算长方形的周长。周长计算公式为:周长 = 2 * (长度 + 宽度)。 - 计算面积函数:定义另一个成员函数,命名为calculate_area,该函数使用长度和宽度属性来计算长方形的面积面积计算公式为:面积 = 长度 * 宽度。 5. 调用成员函数:创建一个长方形对象,并使用对象调用成员函数来计算周长和面积。例如,可以调用calculate_perimeter函数来计算长方形的周长,然后调用calculate_area函数来计算长方形的面积。 - 对象创建和初始化:使用构造函数创建一个长方形对象,并提供长度和宽度的值。 - 调用成员函数:通过对象名称和操作符(.),调用对象成员函数来计算周长和面积。例如,如果长方形对象名为rect,可以使用rect.calculate_perimeter()来计算周长,使用rect.calculate_area()来计算面积。 6. 输出结果:使用打印语句将计算得到的周长和面积输出到屏幕上,以便可以查看结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值