编写类Shape的派生类:圆类Circle、三角形类Triangle和矩形类Rectangle
并重定义基类的成员函数使之返回正确的结果(show函数要输出对象的基本信息),然后编写程序测试他们。下面是形状类:classShape{public:doublearea...
并重定义基类的成员函数使之返回正确的结果(show函数要输出对象的基本信息),然后编写程序测试他们。下面是形状类:class Shape{public:double area(){return 0;}double girth({return 0;}void show(){
cout<<"Shape Object:"<<endl;}private:}; 展开
cout<<"Shape Object:"<<endl;}private:}; 展开
1个回答
推荐于2018-04-05
展开全部
// 完整代码如下:
#include <iostream>
#include <cmath>
using namespace std;
// PI定义
const double PI = 3.1415926;
// Shape
class Shape{
public:
virtual double area(){
return 0;
}
virtual double girth(){
return 0;
}
virtual void show(){
cout<<"Shape Object:"<<endl;
}
};
// Circle
class Circle : public Shape{
public:
Circle():m_Radius(0) {}
Circle(double radius):m_Radius(radius) {}
~Circle() {}
double area(){
return m_Radius * m_Radius * PI;
}
double girth(){
return 2 * PI * m_Radius;
}
void show(){
cout<<"Circle Object"<<endl;
}
private:
double m_Radius;
};
// Triangle
class Triangle : public Shape{
public:
Triangle():m_EdgeA(0), m_EdgeB(0), m_EdgeC(0) {}
Triangle(double la, double lb, double lc):m_EdgeA(la), m_EdgeB(lb), m_EdgeC(lc) {}
~Triangle() {}
double area(){
double temp = girth() * 0.5;
return sqrt( temp * (temp-m_EdgeA) * (temp-m_EdgeB) * (temp-m_EdgeC) );
}
double girth(){
return m_EdgeA + m_EdgeB + m_EdgeC;
}
void show(){
cout<<"Triangle Object"<<endl;
}
private:
double m_EdgeA;
double m_EdgeB;
double m_EdgeC;
};
// Rectangle
class Rectangle : public Shape{
public:
Rectangle():m_Length(0), m_Width(0) {}
Rectangle(double length, double width):m_Length(length), m_Width(width) {}
~Rectangle() {}
double area(){
return m_Length * m_Width;
}
double girth(){
return 2 * (m_Length + m_Width);
}
void show(){
cout<<"Rectangle Object"<<endl;
}
private:
double m_Length;
double m_Width;
};
// Test
int main()
{
Shape *p1 = new Circle(2);
Shape *p2 = new Triangle(3, 4, 5);
Shape *p3 = new Rectangle(6, 7);
// Area
cout<<"Area test:"<<endl
<<"p1 = "<<p1->area()<<endl
<<"p2 = "<<p2->area()<<endl
<<"p3 = "<<p3->area()<<endl<<endl;
// Girth
cout<<"Girth test:"<<endl
<<"p1 = "<<p1->girth()<<endl
<<"p2 = "<<p2->girth()<<endl
<<"p3 = "<<p3->girth()<<endl<<endl;
// Type
cout<<"Type test:"<<endl;
p1->show();
p2->show();
p3->show();
delete p1; p1 = NULL;
delete p2; p2 = NULL;
delete p3; p3 = NULL;
cout<<endl;
return 0;
}
// 测试结果:
#include <iostream>
#include <cmath>
using namespace std;
// PI定义
const double PI = 3.1415926;
// Shape
class Shape{
public:
virtual double area(){
return 0;
}
virtual double girth(){
return 0;
}
virtual void show(){
cout<<"Shape Object:"<<endl;
}
};
// Circle
class Circle : public Shape{
public:
Circle():m_Radius(0) {}
Circle(double radius):m_Radius(radius) {}
~Circle() {}
double area(){
return m_Radius * m_Radius * PI;
}
double girth(){
return 2 * PI * m_Radius;
}
void show(){
cout<<"Circle Object"<<endl;
}
private:
double m_Radius;
};
// Triangle
class Triangle : public Shape{
public:
Triangle():m_EdgeA(0), m_EdgeB(0), m_EdgeC(0) {}
Triangle(double la, double lb, double lc):m_EdgeA(la), m_EdgeB(lb), m_EdgeC(lc) {}
~Triangle() {}
double area(){
double temp = girth() * 0.5;
return sqrt( temp * (temp-m_EdgeA) * (temp-m_EdgeB) * (temp-m_EdgeC) );
}
double girth(){
return m_EdgeA + m_EdgeB + m_EdgeC;
}
void show(){
cout<<"Triangle Object"<<endl;
}
private:
double m_EdgeA;
double m_EdgeB;
double m_EdgeC;
};
// Rectangle
class Rectangle : public Shape{
public:
Rectangle():m_Length(0), m_Width(0) {}
Rectangle(double length, double width):m_Length(length), m_Width(width) {}
~Rectangle() {}
double area(){
return m_Length * m_Width;
}
double girth(){
return 2 * (m_Length + m_Width);
}
void show(){
cout<<"Rectangle Object"<<endl;
}
private:
double m_Length;
double m_Width;
};
// Test
int main()
{
Shape *p1 = new Circle(2);
Shape *p2 = new Triangle(3, 4, 5);
Shape *p3 = new Rectangle(6, 7);
// Area
cout<<"Area test:"<<endl
<<"p1 = "<<p1->area()<<endl
<<"p2 = "<<p2->area()<<endl
<<"p3 = "<<p3->area()<<endl<<endl;
// Girth
cout<<"Girth test:"<<endl
<<"p1 = "<<p1->girth()<<endl
<<"p2 = "<<p2->girth()<<endl
<<"p3 = "<<p3->girth()<<endl<<endl;
// Type
cout<<"Type test:"<<endl;
p1->show();
p2->show();
p3->show();
delete p1; p1 = NULL;
delete p2; p2 = NULL;
delete p3; p3 = NULL;
cout<<endl;
return 0;
}
// 测试结果:
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询