c++一个三角形类triangle,该类数据成员为三角形三边长a,b,c,函数成员为构造函数、析构函数和perimeter()(求三角形的周长)。完成该类的声明、对象定义和使用
时间: 2024-02-21 17:00:26 浏览: 124
以下是C++三角形类Triangle的声明和定义示例代码:
```cpp
// Triangle.h
#ifndef TRIANGLE_H
#define TRIANGLE_H
class Triangle {
public:
Triangle(double a, double b, double c);
~Triangle();
double perimeter() const;
private:
double a_;
double b_;
double c_;
};
#endif
// Triangle.cpp
#include "Triangle.h"
Triangle::Triangle(double a, double b, double c) : a_(a), b_(b), c_(c) {}
Triangle::~Triangle() {}
double Triangle::perimeter() const {
return a_ + b_ + c_;
}
```
使用示例代码:
```cpp
#include <iostream>
#include "Triangle.h"
using namespace std;
int main() {
double a, b, c;
cout << "请输入三角形的三边长:";
cin >> a >> b >> c;
Triangle triangle(a, b, c);
double perimeter = triangle.perimeter();
cout << "三角形的周长为:" << perimeter << endl;
return 0;
}
```
阅读全文
相关推荐
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)