其实这种题用继承来做反而麻烦了,不过主要是熟悉一下继承的整个流程。
#include<iostream>
using namespace std;
class Rectangle{
protected:
double width;
double length;
public:
void setwidth(double w){width=w;}
void setlength(double l){length=l;}
void outData(){
cout<<"底面积:"<<width*length<<endl;
}
};
class Cube:public Rectangle{
private:
double high;
public:
void sethigh(double h){high=h;}
void outData(){
cout<<"立方体表面积:"<<2*((width*length)+(width*high)+(length*high))<<endl;
cout<<"立方体体积:"<<length*high*width<<endl;
}
};
int main(){
Cube a;
a.setwidth(5);
a.setlength(4);
a.sethigh(3);
a.Rectangle::outData();
a.outData();
}