クラスのコンストラクターを使用して 2 つの初期化操作を含める必要があるクラスを設計および実装しています。1つはデフォルトの初期化(私は適切に行ったと思います)であり、もう1つはコンストラクター自体にあるはずのユーザー入力からの初期化です(まだ書くのに問題があります)。私は個別のコンパイルを使用しているので、.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;
}