我正在设计和实现一个类,我必须使用类的构造函数包含两个初始化操作。一种是默认初始化(我认为我已经正确完成了),另一种是来自用户输入的初始化,它应该在构造函数本身中(我仍然无法编写它)。我正在使用单独的编译,因此我将文件中的代码与 .cpp 文件中的类和主函数一起显示。我正在使用 Dev-C++,部分代码如下。谢谢你的帮助。
#include <iostream>
#include <exception>
#include <math.h>
///#include "Exception.h"
#ifndef TRIANGLE_H
#define TRIANGLE_H
using namespace std;
class Triangle
{
private:
double side1;
double side2;
double side3;
double angle_side1_side2;
double angle_side1_side3;
double angle_side2_side3;
public:
//default constructor with default initialization
Triangle::Triangle(): side1(0), side2(0), side3(0), angle_side1_side2(0), angle_side1_side3(0), angle_side2_side3(0)
{
}
//constructor with user inputs, but I know there is something wrong...
Triangle::Triangle(double s1, double s2, double s3)
{
cout<<"enter your own values:"<<endl;
cin>>s1>>s2>>s3;
side1=s1;
side2=s2;
side3=s3;
cout<<"the current lengths of sides of your triangle are: " <<endl;
}
double display_triangle_sides()
{
cout<<side1<<" "<<side2<<" "<<side3<<" ";
}
double display_triangle_Area()
{
double S=(side1+side2+side3)/2; //semiperimeter
double T=sqrt(S*(S-side1)*(S-side2)*(S-side3)); //Heron formula to calculate Area of triangle
return T;
}
};
endif
//*****************************main function below************************
#include <iostream>
#include <exception>
#include "Untitled1.h"
using namespace std;
int main()
{
Triangle tr;
cout<<" length of each side of the current triangle : ";
tr.display_triangle_sides(); //I want to show here the values entered by the users
cout<<endl<<"Here is the Area of the current triangle : " << tr.display_triangle_Area()<<endl;
cout<<"type of triangle:"<<endl;
tr.Type_of_Triangle();
system("Pause");
return 0;
}