我正在设计和实现一个类,我必须使用类的构造函数包含两个初始化操作。一个是默认初始化(我认为我已经正确完成),另一个是来自用户输入的初始化,它应该在构造函数本身中(我仍然无法编写它)。我正在使用单独的编译,因此我使用类和.cpp文件中的main函数显示文件中的代码。我正在使用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;
}
答案 0 :(得分:3)
用户必须手动输入值的构造函数是令人震惊的设计。你确定你被告知这样做吗?
执行此操作的正确方法是这样的
class Triangle
{
Triangle::Triangle(): side1(0), side2(0), side3(0)
{
}
Triangle::Triangle(double s1, double s2, double s3) : side1(s1), side2(s2), side3(s3)
{
}
...
};
int main()
{
double s1, s2, s3;
cin >> s1 >> s2 >> s3;
Triangle t(s1, s2, s3);
...
}
在单词中,您在main函数中输入值,然后将值传递给构造函数。
你的做事方式如此糟糕的原因是它使你的Triangle构造函数只能在你正在编写的程序的特定上下文中使用。您应该设计您的类,以便它们在不同的程序中可重用。当你刚刚开始时,这可能是一件很难让你欣赏的事情,因为你专注于让一个程序工作,而不是考虑你将来可能编写的程序。
如果你真的被告知你必须这样做,那么你将被一个无能为力的人教导。
答案 1 :(得分:1)
你需要这样做:
int main()
{
cout<<"enter your own values"<<endl;
double s1,s2,s3;
cin>>s1>>s2>>s3;
Triangle tr(s1,s2,s3);
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;
}
同时将consrtructor更改为
//constructor with user inputs, but I know there is something wrong...
Triangle::Triangle(double s1, double s2, double s3):side1(s1),side2(s2)side3(s3)
{
}
答案 2 :(得分:0)
根据您的任务描述,您需要机会创建对象类实例的不同配置,特定实例的配置取决于用户输入。
因此,作为处理任务的方法之一,我建议您阅读编程模式构建器
类构建器的对象负责创建特定类的实例,它提供诸如灵活的对象创建和错误预防等优点。我为你写了一个小例子:
class Triangle
{
friend class TriangleBuilder;
public:
double display_triangle_sides()
{
cout<<side1;
}
private:
double side1;
Triangle::Triangle(): side1(0){}
Triangle::Triangle(int v): side1(v){}
};
class TriangleBuilder
{
public:
void BuildDefaultTriangle(void)
{
this->m_instance = new Triangle;
}
void BuildCustomTriangle(void)
{
cout << "All right!! Enter side length)\n" << endl;
int tmp;
cin >> tmp;
this->m_instance = new Triangle(tmp);
}
Triangle* getTriangle(void)
{
return this->m_instance;
}
private:
Triangle* m_instance;
};