编写一个长方体类rectangular,求1个长方体的体积,请编写一个基于对象的程序。数据成员包括length, width, height。要求用成员函数实现以下功能。
(1)通过构造函数实现length, width, height初始化;
(2)计算长方体的体积;
(3)输出长方体的体积;
(4)编写析构函数,观察析构函数的调用位置。
class rectangular
{
private:
int length;
int width;
int height;
public:
rectangular() {}
rectangular(int l,int w,int h) {length = l;width = w; height = h;}
~rectangular() {cout<<"rectangular对象析构"<<endl;}
int getTJ() {return length*width*height;}
void printTJ() {cout<<"长方体体积为:"<<getTJ()<<endl;}
};
void main()
{
int l,w,h;
cout<<"请分别输入长方体的长、宽和高:";
cin>>l>>w>>h;
rectangular r(l,w,h);
r.printTJ();
}
#include<iostream>
using namespace std;
class rectangle{
public:
//初始化
rectangle(){
length = width = height = 0;
}
//输入数据
void input(){
cin >> length >> width >> height;
}
//输出结果
void output(){
volume = length * width * height;
cout << volume << endl;
}
private:
double length;
double width;
double height;
double volume;
};
int main()
{
rectangle a[3];
for(int i = 0; i < 3; i++){
a[i].input();
a[i].output();
}
return 0;
}
#include <iostream>
using namespace std;
class Tiji
{
public:
Tiji(float len, float width,float height)
{
Length = len;
Width = width;
Height=height;
}
~Tiji(){};
float GetArea() { return Length * Width; }
float GetLength() { return Length; }
float GetWidth() { return Width; }
float Getheight(){return Height;}
private:
float Length;
float Width;
float Height;
};
void main()
{
float length, width,height;
cout << "请输入长方体的长度:";
cin >> length;
cout << "请输入长方体的宽度:";
cin >> width;
cout << "请输入长方体的高度:";
cin >> height;
Tiji r(length, width,height);
cout << "长为" << length << "宽为" << width <<"高为"<<height<< "的长方体的体积为:"
<< height*(length*width) << endl;
}
您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~
ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓
【电脑端】戳>>> https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】 戳>>> https://mall.csdn.net/item/52471?utm_source=1146287632