定义一个Shape基类,在此基础上派生出Rectangle和Cirle类,二者都有GetArea()函数计算对象的面积。使用Rec
1个回答
展开全部
#include <iostream>
using namespace std;
const double PI=3.1415926;
class Shape
{
public:
double area();
protected:
int kind;
};
double Shape::area()
{
cout<<"nothing"<<endl;
return 0;
}
class Rectangle: public Shape
{
public:
Rectangle(double a, double b);
double area();
protected:
double a, b;
};
Rectangle::Rectangle(double a, double b)
{
this->a=a;
this->b=b;
}
double Rectangle::area()
{
return a*b;
}
class Circle: public Shape
{
public:
Circle(double r);
double area();
private:
double r;
};
Circle::Circle(double r)
{
this->r=r;
}
double Circle::area()
{
return PI*r*r;
}
int main()
{
Rectangle s(4.4, 1.2);
Circle c(1.0);
cout<<"Square area= "<<s.area()<<endl;
cout<<"Circle area= "<<c.area()<<endl;
return 0;
}
using namespace std;
const double PI=3.1415926;
class Shape
{
public:
double area();
protected:
int kind;
};
double Shape::area()
{
cout<<"nothing"<<endl;
return 0;
}
class Rectangle: public Shape
{
public:
Rectangle(double a, double b);
double area();
protected:
double a, b;
};
Rectangle::Rectangle(double a, double b)
{
this->a=a;
this->b=b;
}
double Rectangle::area()
{
return a*b;
}
class Circle: public Shape
{
public:
Circle(double r);
double area();
private:
double r;
};
Circle::Circle(double r)
{
this->r=r;
}
double Circle::area()
{
return PI*r*r;
}
int main()
{
Rectangle s(4.4, 1.2);
Circle c(1.0);
cout<<"Square area= "<<s.area()<<endl;
cout<<"Circle area= "<<c.area()<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询