C++构造函数基于Rectangle类并派生Cuboid长方体类

这篇博客介绍了如何在C++中定义一个Rectangle类,包含长度和宽度属性以及计算面积的方法。接着,基于Rectangle类派生出Cuboid类,增加高度属性,并实现计算体积的函数。示例展示了如何创建Rectangle和Cuboid对象并输出其面积和体积。
摘要由CSDN通过智能技术生成

 题目:1.设置一个名为Rectangle的类表示矩形。这个类包括:

(1)两个名为length和width的float型数据域。他们分别表示矩形的长和宽;

(2)创建length和width为指定值的矩形的构造方法。

(3)一个名为getArea()的方法返回这个矩形的面积;

2.基于Rectangle类派生出长方体类Cuboid,这个类包括:

(1)名为height的float类型的数据域,表示长方体的高。

(2)为长方体的长、宽、高赋初值的构造函数。

(3)一个名为getVolume()的方法返回长方体的体积。

3.测试程序:

(1)创建一个Rectangle对象:一个矩形的长为5,宽为4,输出面积;

(2)创建一个Cuboid对象,长方体的长、宽、高分别是5,4,6。

#include <iostream>

using namespace std;
class Rectangle
{public:
    Rectangle(float l=0,float w=0):m_L(l),m_W(w)
    {

    }
    float getArea()
    {
        return m_L*m_W;
    }
    float getlength()
    {
        return m_L;
    }
    float getwidth()
    {
        return m_W;
    }
private:
    float m_L;
    float m_W;

};
class Cuboid:public Rectangle
{public:
    Cuboid(float l=0,float w=0,float
  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
#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; }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值