Problem C: 正方形、长方形、立方体——连环继承

 

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
class Square{
    public:
        int l;
    public:
        Square(int a)
        {
            l=a;
            cout<<"Construct Square ("<<l<<")"<<endl;
        }
        int length()
        {
            return l;
        }
        int perimeter()
        {
            return 4*l;
        }
        int area()
        {
            return l*l;
        }
};
class Rectangle:public Square{
    public:
        int w;
    public:
        Rectangle(int a,int b):Square(a)
        {
            w=b;
            cout<<"Construct Rectangle ("<<l<<", "<<w<<")"<<endl;
        }
        int length()
        {
            return l;
        }
        int width()
        {
            return w;
        }
        int perimeter()
        {
            return 2*(l+w);
        }
        int area()
        {
            return l*w;
        }
};
class Cuboid:public Rectangle{
    public:
    int h;
    public:
     Cuboid(int a,int b,int c):Rectangle(a,b)
     {
        h=c;
        cout<<"Construct Cuboid ("<<l<<", "<<w<<", "<<h<<")"<<endl;
     }
     int length()
        {
            return l;
        }
        int width()
        {
            return w;
        }
        int height()
        {
            return h;
        }
        int perimeter()
        {
            return 4*(l+w+h);
        }
        int area()
        {
            return 2*(l*w+l*h+w*h);
        }
        int volume()
        {
            return l*w*h;
        }
};
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;
    }
 
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值