运用类和对象的思想进行编程。代码如下:
#include<iostream>
using namespace std;
class triangle//声明三角形类
{
private:
double length;//长
double width;//宽
double c, s;//周长和面积
public:
void display()
{
cin >> length >> width;//输入长和宽
c = (length + width) * 2;//求三角形的周长
s = length * width;//求三角形的面积
cout << "三角形的周长是:" << c << endl;//输出周长
cout << "三角形的面积是:" << s << endl;//输出面积
}
};
int main()
{
triangle stu1,stu2;//定义两个对象
stu1.display();
stu2.display();
}
运行结果:(题目要求的数据需要手动输入)