正方形、长方形、立方体

Description
给出正方形(Square)、长方形(Rectangle)、立方体(Cuboid)的边长,求周长、面积、体积。
Square类只需存一条边长,构造函数产生一条输出,有边长、周长、面积的函数。
Rectangle类需存长和宽,若从Square类派生而来,因此只需增加一条边,构造函数产生一条输出,有长、宽、周长、面积的函数。
Cuboid类需存长宽高,若从Rectangle类派生而来,因此也只增加一条边,构造函数产生一条输出,有长、宽、高、周长、面积、体积的函数。它的周长定义为所有棱长之和。


请仔细阅读append.cc代码,并设计好正方形、长方形、立方体派生关系,使main()函数能够运行并得到正确的输出。

Input
输入分为三部分,每一部分都已一个整数n开始,表示后面有n组测试数据。
在第一部分测试数据中,每组是一个整数,表示正方形的边长。
在第二部分测试数据中,每组是两个整数,表示长方形的长和宽。
在第三部分测试数据中,每组是三个整数, 表示立方体的长和宽。

Output

每组测试数据对应的输出为两部分,前面是构造函数的输出,最后是输出图形的信息,包括长宽高、周长、面积、体积等信息,格式见sample;

Sample Input
1
4
1
3 4
1
3 4 6
Sample Output
Construct Square (4)
A Square length 4, Perimeter 16, Area 16

=========================
Construct Square (3)
Construct Rectangle (3, 4)
A Rectangle length 3, width 4, Perimeter 14, Area 12

=========================
Construct Square (3)
Construct Rectangle (3, 4)
Construct Cuboid (3, 4, 6)
A Cuboid length 3, width 4, height 6, Perimeter 52, Area 108, Volume 72
HINT

Append Code
append.c, append.cc,

int main()
{
    int cases, l, w, h;
    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l;
        Square squa(l);
        cout << "A Square length " << squa.length() << ", ";
        cout << "Perimeter " << squa.perimeter() << ", ";
        cout << "Area " << squa.area() << endl;
    }

    cout << "=========================" << endl;

    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l >> w;
        Rectangle rect(l, w);
        cout << "A Rectangle length " << rect.length() << ", width " << rect.width() << ", ";
        cout << "Perimeter " << rect.perimeter() << ", ";
        cout << "Area " << rect.area() << endl;
    }

    cout << "=========================" << endl;

    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l >> w >> h;
        Cuboid cubo(l, w, h);
        cout << "A Cuboid length " << cubo.length() << ", width " << cubo.width() << ", height " << cubo.height() << ", ";
        cout << "Perimeter " << cubo.perimeter() << ", ";
        cout << "Area " << cubo.area() << ", ";
        cout << "Volume " << cubo.volume() << endl;
    }

}

AC代码

#include <iostream>
using namespace std;
class Square
{
private:
    int len;
public:
    Square(int le=0):len(le){cout<<"Construct Square ("<<len<<")"<<endl;}
    int length(){return len;}
    int perimeter(){return 4*len;}
    int area(){return len*len;}
};
class Rectangle:public Square
{
private:
    int widt;
public:
    Rectangle(int le,int wid):Square(le),widt(wid){cout<<"Construct Rectangle ("<<length()<<", "<<widt<<")"<<endl;}
    int width(){return widt;}
    int perimeter(){return 2*widt+2*length();}
    int area(){return widt*length();}
};
class Cuboid:public Rectangle
{
private:
    int heigh;
public:
    Cuboid(int le,int wid,int he):Rectangle(le,wid),heigh(he){cout<<"Construct Cuboid ("<<length()<<", "<<width()<<", "<<heigh<<")"<<endl;}
    int height(){return heigh;}
    int perimeter(){return 4*length()+4*width()+4*heigh;}
    int area(){return 2*length()*width()+2*length()*heigh+2*width()*heigh;}
    int volume(){return length()*width()*heigh;}
};
int main()
{
    int cases, l, w, h;
    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l;
        Square squa(l);
        cout << "A Square length " << squa.length() << ", ";
        cout << "Perimeter " << squa.perimeter() << ", ";
        cout << "Area " << squa.area() << endl;
    }

    cout << "=========================" << endl;

    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l >> w;
        Rectangle rect(l, w);
        cout << "A Rectangle length " << rect.length() << ", width " << rect.width() << ", ";
        cout << "Perimeter " << rect.perimeter() << ", ";
        cout << "Area " << rect.area() << endl;
    }

    cout << "=========================" << endl;

    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l >> w >> h;
        Cuboid cubo(l, w, h);
        cout << "A Cuboid length " << cubo.length() << ", width " << cubo.width() << ", height " << cubo.height() << ", ";
        cout << "Perimeter " << cubo.perimeter() << ", ";
        cout << "Area " << cubo.area() << ", ";
        cout << "Volume " << cubo.volume() << endl;
    }
}

本题注意:
1、命名虽然题目中没有给出,但是后面main函数中有,所以在做题的时候不仅要看清楚题意,同时也要结合main函数;
2、赋值的顺序千万不要弄反了,结合题意,结合main函数,结合输入、输出的数据来做题;
3、一个函数的名字和要返回的函数的名字是不能一样的;例如int length(){retrun length;}这样是错误的,因为名字一样,必须保证不一样。
4、因为没有重复名字的函数,所以调用函数的时候直接调用即可,不需要类名::函数名()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值