作者在 2011-10-12 23:19:47 发布以下内容
#include <iostream>
using namespace std;
#define PI 3.14159
class shape
{
public:
virtual int area() const=0;
virtual void show()=0;
};
class rectangle : public shape
{
public:
int _with;
int _height;
rectangle(int with,int height):_with(with),_height(height){}
int area()const
{
return _with*_height;
}
void show()
{
cout<<"rectangle: with:"<<_with<<" height:"<<_height<<endl;
cout <<"area :"<<area()<<endl;
}
bool operator ==(const rectangle &a)
{
if(this->area()==a.area())
return 1;
else
return 0;
}
bool operator >(const rectangle &a)
{
if(this->area()>a.area())
return 1;
else
return 0;
}
bool operator <(const rectangle &a)
{
if(this->area()<a.area())
return 1;
else
return 0;
}
~rectangle(){}
};
class ellipse: public shape
{
public:
int _with;
int _height;
ellipse(int with,int height):_with(with),_height(height){}
int area()const
{
return PI*_with*_height;
}
void show()
{
cout<<"ellipse: with:"<<_with<<" height:"<<_height<<endl;
cout <<"area :"<<area()<<endl;
}
bool operator ==(const ellipse &a)
{
if(this->area()==a.area())
return 1;
else
return 0;
}
bool operator >(const ellipse &a)
{
if(this->area()>a.area())
return 1;
else
return 0;
}
bool operator <(const ellipse &a)
{
if(this->area()<a.area())
return 1;
else
return 0;
}
~ellipse(){}
};
int main()
{
//cout << "Hello world!" << endl;
int a=0,b=0;
cout<<"矩形"<<endl;
cout<<"宽度 高度"<<endl;
rectangle **p;
p=new rectangle*[3];
for(int i=0;i<3;i++)
{
cin>>a>>b;
p[i]=new rectangle(a, b);
}
cout<<"椭圆"<<endl;
cout<<"外接矩形宽度 外接矩形高度"<<endl;
ellipse **q;
q=new ellipse*[3];
for(int i=0;i<3;i++)
{
cin>>a>>b;
q[i]=new ellipse(a, b);
}
p[2]->show(),p[1]->show();
cout<<p[2]->area()<<endl<<p[1]->area()<<endl;
cout<<(*(p[2])==*(p[1]))<<endl;
/*
ellipse a(5,3);
ellipse b(7,5),c(9,6);
cout<<" a=b "<<(a==b)<<" a<b "<<(a<b)<<" a>b "<<(a>b)<<endl;
*/
return 0;
}
using namespace std;
#define PI 3.14159
class shape
{
public:
virtual int area() const=0;
virtual void show()=0;
};
class rectangle : public shape
{
public:
int _with;
int _height;
rectangle(int with,int height):_with(with),_height(height){}
int area()const
{
return _with*_height;
}
void show()
{
cout<<"rectangle: with:"<<_with<<" height:"<<_height<<endl;
cout <<"area :"<<area()<<endl;
}
bool operator ==(const rectangle &a)
{
if(this->area()==a.area())
return 1;
else
return 0;
}
bool operator >(const rectangle &a)
{
if(this->area()>a.area())
return 1;
else
return 0;
}
bool operator <(const rectangle &a)
{
if(this->area()<a.area())
return 1;
else
return 0;
}
~rectangle(){}
};
class ellipse: public shape
{
public:
int _with;
int _height;
ellipse(int with,int height):_with(with),_height(height){}
int area()const
{
return PI*_with*_height;
}
void show()
{
cout<<"ellipse: with:"<<_with<<" height:"<<_height<<endl;
cout <<"area :"<<area()<<endl;
}
bool operator ==(const ellipse &a)
{
if(this->area()==a.area())
return 1;
else
return 0;
}
bool operator >(const ellipse &a)
{
if(this->area()>a.area())
return 1;
else
return 0;
}
bool operator <(const ellipse &a)
{
if(this->area()<a.area())
return 1;
else
return 0;
}
~ellipse(){}
};
int main()
{
//cout << "Hello world!" << endl;
int a=0,b=0;
cout<<"矩形"<<endl;
cout<<"宽度 高度"<<endl;
rectangle **p;
p=new rectangle*[3];
for(int i=0;i<3;i++)
{
cin>>a>>b;
p[i]=new rectangle(a, b);
}
cout<<"椭圆"<<endl;
cout<<"外接矩形宽度 外接矩形高度"<<endl;
ellipse **q;
q=new ellipse*[3];
for(int i=0;i<3;i++)
{
cin>>a>>b;
q[i]=new ellipse(a, b);
}
p[2]->show(),p[1]->show();
cout<<p[2]->area()<<endl<<p[1]->area()<<endl;
cout<<(*(p[2])==*(p[1]))<<endl;
/*
ellipse a(5,3);
ellipse b(7,5),c(9,6);
cout<<" a=b "<<(a==b)<<" a<b "<<(a<b)<<" a>b "<<(a>b)<<endl;
*/
return 0;
}
标题: | 1、抽象类的派生和运算符的重载 |
时 限: | 1000 ms |
内存限制: | 10000 K |
总时限: | 3000 ms |
描述: |
1.抽象类的定义:完成形状抽象类Shape的定义:公有函数:面积(Area);公有函数:显示(Show);实现构造函数和析构函数 2.从形状类Shape派生矩形类Rectangle: 添加公有成员:宽度(Width),高度(Height); 3.从形状类Shape派生椭圆类Ellipse: |
输入: |
创建6个实例: 矩形 |
输出: | 矩形之间的面积,椭圆间的面积的比较结果 |
输入样例: | |
输出样例: | |
提示: | 运算符重载为类的成员函数 |
来源: |