/*****************************************************************************************************************
文件说明:
利用运算符重载函数说明静态多态性
【1】多态的定义:
向不同的对象发送同一消息时,不同的对象在接收时会产生不同的行为
【2】多态的分类
从系统实现的角度来看,多态性分为两类:【静态多态性】和【动态多态性】
(1)【静态多态性】是通过函数重载实现的,要求在函数编译时就知道调用函数的全部信息;
静态多态性的函数调用速度快、效率高,但缺乏灵活性
(2)【动态多态性】不在编译时确定调用的是哪个函数,而是在程序运行中才动态的确定操作所针对的对象,
它又称为运行时的多态性,动态多态性是通过虚函数实现的
****************************************************************************************************************/
#include<iostream>
using namespace std;
/******************************************************************************************************************
//模块说明
声明类Point
*******************************************************************************************************************/
class Point
{
public:
Point(float x=0,float y=0); //有默认参数的构造函数
void setPoint(float,float); //设置坐标值
float getX()const{return x;} //读X坐标,getX函数为常成员函数
float getY()const{return y;} //读Y坐标,getY函数为常成员函数
friend ostream& operator<<(ostream&,const Point&); //友元重载运算符<<
protected:
float x,y;
};
/******************************************************************************************************************
模块说明:
下面是定义Point类的成员函数
*******************************************************************************************************************/
Point::Point(float a,float b) //Point构造函数
{
x = a;
y = b;
}
void Point:: setPoint(float a,float b) //设置x和y的坐标
{
x = a;
y = b;
}
ostream& operator<<(ostream&output,const Point&p) //重载运算符“<<”,使之能输出点的坐标
{
output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return cout;
}
/*******************************************************************************************************************
模块说明:
声明派生类Circle
***********************************************************************************************************************/
class Circle:public Point
{
public:
Circle(float x=0,float y=0,float r = 0); //构造函数
void setRadius(float); //设置半径
float getRadius()const; //读取半径
float area()const; //计算圆的面积
friend ostream &operator<<(ostream&,const Circle&);
protected:
float radius;
};
//int main() //该主函数 只是为了测试Point类
//{
// Point p(3.5,6.4);
// cout<<"x="<<p.getX()<<",y="<<p.getY()<<endl;
// p.setPoint(8.5,6.8);
// cout<<"p(new):"<<p<<endl;
//
// system("pause");
// return 0;
//}
/*******************************************************************************************************************
模块说明:
下面是定义Circle类的成员函数
***********************************************************************************************************************/
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){} //定义构造函数
void Circle::setRadius(float r) //设置半径值
{
radius = r;
}
float Circle::getRadius()const{return radius;} //读取半径的值
float Circle::area()const{return 3.14159*radius*radius;} //计算圆面积
ostream &operator<<(ostream&output,const Circle &c)
{
output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area()<<endl;
return output;
}
//int main() //该主函数只是为了测试Circle类
//{
// Circle c(3.5,6.4,5.2);
// cout<<"original circle:\nx="<<c.getX()<<",y="<<c.getY()<<",r="<<c.getRadius()<<",area="<<c.area()<<endl;
// c.setRadius(7.5);
// c.setPoint(5,5);
// cout<<"new circle:\n"<<c; //调用的是在Circle类中的声明的运算符重载函数
// Point &pRef = c; //派生类对象可以代替基类对象向基类对象的引用初始化或者赋值
// cout<<"pRef:"<<pRef;
//
//
// system("pause");
// return 0;
//}
/***********************************************************************************************************************
//模块说明:
声明Circle的派生类Cylinder
*************************************************************************************************************************/
class Cylinder:public Circle
{
public:
Cylinder(float x=0,float y=0,float r=0,float h=0); //构造函数
void setHeight(float); //设置圆柱的高
float getHeight()const; //读取圆柱的高
float area()const;
float volume()const; //计算圆柱体积
friend ostream& operator<<(ostream&output,const Cylinder&); //重载运算符
protected:
float height;
};
/*******************************************************************************************************************
模块说明:
下面是定义Cylinder类的成员函数
***********************************************************************************************************************/
Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){} //构造函数
void Cylinder::setHeight(float h){height=h;} //设置圆柱的高
float Cylinder::getHeight()const{return height;} //读取圆柱的高
float Cylinder:: area()const{return 2*Circle::area()+2*3.14159*radius*height;} //计算圆柱体的表面积
float Cylinder::volume()const{return Circle::area()*height;} //计算圆柱体积
ostream& operator<<(ostream&output,const Cylinder&cy) //重载运算符
{
output<<"Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height<<"\narea="<<cy.area()<<",volume="<<cy.volume()<<endl;
return output;
}
int main()
{
Cylinder cy1(3.5,6.4,5.2,10);
cout<<"original cylinder:\nx="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<",volume="<<cy1.volume()<<endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5,5);
cout<<"\nnew cylinder:\n"<<cy1;
Point &pRef =cy1;
cout<<"\npRef as a point:"<<pRef;
Circle &cRef = cy1;
cout<<"\ncRef as a circle:"<<cRef;
system("pause");
return 0;
}