在线急求一个c++习题
要求使用指向类成员函数的指针实现多态性来计算来计算,正方行,矩形,直角三角形和圆的面积O(∩_∩)O谢谢没有积分了帮帮我把...
要求使用指向类成员函数的指针实现多态性 来计算来计算,正方行,矩形,直角三角形和圆的面积 O(∩_∩)O谢谢 没有积分了帮帮我把
展开
展开全部
#include <iostream>
using namespace std;
const double PI=3.14;
class Sharp
{
public:
virtual void Area()=0;
float area;
};
class Rect:public Sharp
{
public:
Rect(float x,float y):length(x),heigth(y){};
void Area()
{
area=length*heigth;
printf("Rect area:%.2f\n",area);
}
private:
float length,heigth;
};
class Square:public Sharp
{
public:
Square(float x):length(x){};
void Area()
{
area=length*length;
printf("Square area:%.2f\n",area);
}
private:
float length;
};
class Circle:public Sharp
{
public:
Circle(float r):radius(r){};
void Area()
{
area=PI*radius*radius;
printf("Circle area:%.2f\n",area);
}
private:
float radius;
};
class Triangle:public Sharp
{
public:
Triangle(float x,float y):length(x),heigth(y){};
void Area()
{
area=0.5*length*heigth;
printf("Triangle area:%.2f\n",area);
}
private:
float length,heigth;
};
int main(void)
{
Sharp *p1;
Rect a(1,2);
p1=&a;
p1->Area();
Square b(4);
p1=&b;
p1->Area();
Circle c(9);
p1=&c;
p1->Area();
Triangle d(2,3);
p1=&d;
p1->Area();
return 0;
}
using namespace std;
const double PI=3.14;
class Sharp
{
public:
virtual void Area()=0;
float area;
};
class Rect:public Sharp
{
public:
Rect(float x,float y):length(x),heigth(y){};
void Area()
{
area=length*heigth;
printf("Rect area:%.2f\n",area);
}
private:
float length,heigth;
};
class Square:public Sharp
{
public:
Square(float x):length(x){};
void Area()
{
area=length*length;
printf("Square area:%.2f\n",area);
}
private:
float length;
};
class Circle:public Sharp
{
public:
Circle(float r):radius(r){};
void Area()
{
area=PI*radius*radius;
printf("Circle area:%.2f\n",area);
}
private:
float radius;
};
class Triangle:public Sharp
{
public:
Triangle(float x,float y):length(x),heigth(y){};
void Area()
{
area=0.5*length*heigth;
printf("Triangle area:%.2f\n",area);
}
private:
float length,heigth;
};
int main(void)
{
Sharp *p1;
Rect a(1,2);
p1=&a;
p1->Area();
Square b(4);
p1=&b;
p1->Area();
Circle c(9);
p1=&c;
p1->Area();
Triangle d(2,3);
p1=&d;
p1->Area();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询