哪位精通C++的大哥进来帮个忙啊~

2024-12-28 19:57:12
推荐回答(3个)
回答1:

#include
using namespace std;

class rectangle //rectangle类
{public:
int length,width; //两个数据成员length和width
int area;

void get_area()
{
cout<<"输入长和宽:"< cin>>length>>width;
area=length*width; //计算长方形面积的成员函数area
cout<<"面积等于"< }

};

class rectangular:public rectangle //定义rectangle的派生类rectangular
{
public:
int height,volume;//一个新的数据成员height和求长方形体积的成员函数
void get_volume()
{
cout<<"输入长,宽和高:"< cin>>length>>width>>height;
volume=length*width*height;
cout<<"体积等于:"< }
};

int main() //声明两个类的对象,调用函数求某个长方形的面积和体积
{
rectangle a;
rectangular v;
a.get_area();
v.get_volume();
return 0;
}

回答2:

// aa.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
using namespace std;
class rectangle
{
public:
rectangle(int w,int l)
{
width =w;
length=l;
}
rectangle():length(0),width(0)
{

}

int eare()
{
return (width*length);
}
private:
int width;
int length;
};

class rectangular:public rectangle
{
public:
rectangular(int w,int l,int h):rectangle(w,l),height(h)
{

}
rectangular(rectangle rec, int h):rectangle(rec),height(h)
{

}
rectangular():rectangle(0,0),height(0)
{

}
int volume()
{
return eare()*height;
}
private:
int height;
};

int main()
{
rectangle rec(5,4);
rectangular regu(5,4,3);
cout<}

回答3:

#include

class rectangle
{
public:
float length,width,sum;
rectangle(float length,float width)
{sum=length*width;}
void printA(void)
{cout<<"长方形面积="<}
class rectangular:rectangle
{
private:
float height,csum;
public:
rectangular(float length,float width,float height):rectangle(length,width)
{csum=length*width*height
}
void printB(void)
{cout<<"长方形体积="<}
float main()
{rectangle(float,float);
rectangular(float,float,float);
void printA();
void printB();
Return 0;
}