Description
Design a class named Triangle that extends GeometricObject class.
Hint
Please submit the GeometricObject class and Triangle class. Don't submit the main() function. |
一发WA:
#include<iostream>
#include<cmath>
using namespace std;
class GeometricObject
{
public:
GeometricObject(){
color="blue";
filled=true;
}
GeometricObject(string color, bool filled)
{
this->color = color;
this->filled = filled;
}
string getColor()
{ return color; }
void setColor(string color)
{ this->color = color; }
bool isFilled()
{ return filled; }
void setFilled(bool filled)
{ this->filled = filled;}
string toString()
{
return "Geometric object color " + color +
" filled " + ((filled) ? "true" : "false");
}
private:
string color;
bool filled;
};
class Triangle: public GeometricObject{
private:
double side1,side2,side3;
//string color;
// bool filled;
public:
Triangle(){
//color = "blue";
//filled = true;
side1=1.0;
side2=1.0;
side3=1.0;
}
Triangle(int s1,int s2,int s3/*,const string &color="blue",bool filled="true"*/){
side1=s1;
side2=s2;
side3=s3;
//this->color=color;
//this->filled=filled;
}
Triangle(Triangle &T){
side1=T.side1;
side2=T.side2;
side3=T.side3;
//color=T.color;
//filled=T.filled;
}
double getSide1(){
return side1;
}
double getSide2(){
return side2;
}
double getSide3(){
return side3;
}
double getArea(){
double p=(side1+side2+side3)/2.0;
return pow(p*(p-side1)*(p-side2)*(p-side3),0.5);
}
double getPerimeter(){
return side1+side2+side3;
}
/* string toString()
{
return "Triangle object color " + color +
" filled " + ((this->isFilled()) ? "true" : "false");
}*/
};
正解:
#include<iostream>
#include<cmath>
using namespace std;
class GeometricObject
{
public:
GeometricObject(string color, bool filled)
{
this->color = color;
this->filled = filled;
}
string getColor()
{ return color; }
void setColor(string color)
{ this->color = color; }
bool isFilled()
{ return filled; }
void setFilled(bool filled)
{ this->filled = filled;}
string toString()
{
return "Geometric object color " + color +
" filled " + ((filled) ? "true" : "false");
}
private:
string color;
bool filled;
};
class Triangle: public GeometricObject{
private:
double side1,side2,side3;
public:
Triangle():GeometricObject("blue",true){
side1=1.0;
side2=1.0;
side3=1.0;
}
Triangle(double s1,double s2,double s3):GeometricObject("blue",true){
side1=s1;
side2=s2;
side3=s3;
}
double getSide1(){
return side1;
}
double getSide2(){
return side2;
}
double getSide3(){
return side3;
}
double getArea(){
double p=(side1+side2+side3)/2.0;
return sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
double getPerimeter(){
return side1+side2+side3;
}
};
conclusion:醉了。。。一个int double 纠结了一下午,WA还以为是算法错了,结果是数据类型弄错。以后还是多注意一点吧
Description
Design a class named Triangle that extends GeometricObject class.
Hint
Please submit the GeometricObject class and Triangle class. Don't submit the main() function. |