Using c++ to implement a class of Triangle.
时间: 2024-05-20 21:18:00 浏览: 134
Sure, I can help you with that! Creating a class for Triangle in C is fairly simple. You'll need to define the class with three properties for the three sides of the triangle and some methods to calculate its area and perimeter. Is there anything specific you need help with?
相关问题
using c++ to implement a class of triangle
好的,我可以回答你这个问题。你可以使用以下的代码实现一个三角形类:
```
#include <iostream>
using namespace std;
class Triangle {
private:
int side1, side2, side3;
public:
Triangle(int s1, int s2, int s3) {
side1 = s1;
side2 = s2;
side3 = s3;
}
int getPerimeter() {
return (side1 + side2 + side3);
}
double getArea() {
double s = (side1 + side2 + side3) / 2.0;
return sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
};
int main() {
Triangle t(3, 4, 5);
cout << "Perimeter: " << t.getPerimeter() << endl;
cout << "Area: " << t.getArea() << endl;
return 0;
}
```
这个程序实现了一个三角形类,并且计算了其周长和面积。你可以根据你的需求进行修改。
using c++ to implement a class of trian gle
Sure, to implement a class of triangle using C, you can define a structure with three variables representing the length of each side of the triangle. Then, you can write functions to compute the area and perimeter of the triangle based on these side lengths. Would you like me to provide sample code for this?
阅读全文