1. 程式人生 > >C++多型性的理解和舉例

C++多型性的理解和舉例

多型性是面向物件程式的一個重要特徵,下面通過程式來理解程式的多型性:

//多型性
#include<iostream>
using namespace std;

class Point
{
public:
	Point(float x=0,float y=0);
	void setPoint(float,float);
	float getX() const {return x;}
	float getY() const {return y;}
	friend ostream &operator<<(ostream &,const Point &);
protected:
	float x,y;  //必須定義成protected,子類的物件才能訪問,加入定義成private,則後面子類無法呼叫
};

Point::Point(float a,float b)
{
	x=a;
	y=b;
}

void Point::setPoint(float a,float b)
{
	x=a;
	y=b;
}

ostream & operator<<(ostream &output,const Point &p)//第一次過載<<
{
	output<<"["<<p.x<<","<<p.y<<"]"<<endl;
	return output;
}

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;
};

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.14*radius*radius;
}

ostream & operator<<(ostream &output,const Circle &c)
{
	output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area()<<endl;
	return output;
}

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;   //與Circle類中的area同名,同名覆蓋
	float volume() const;
	friend ostream & operator<<(ostream&,const Cylinder&);//<<運算子過載了三次,過載函式的引數個數和引數型別必須至少有一個不同
protected:
	float height;
};

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.14*radius*height;
}

float Cylinder::volume() const
{
	return Circle::area()*height;
}

ostream & operator<<(ostream& output,const Cylinder &cy)
{
	output<<"Center=["<<cy.getX()<<","<<cy.getY()<<"],r="<<cy.radius<<",height="<<cy.getHeight()<<"\narea="<<cy.area()<<",volume="<<cy.volume()<<endl;
	return output;
}

int main()
{
	//測試Point
	Point point(1.2,1.2);

	cout<<"x="<<point.getX()<<",y="<<point.getY()<<endl;
	cout<<point<<endl;

	point.setPoint(1.5,1.5);

	cout<<"X="<<point.getX()<<"Y="<<point.getY()<<endl;
	cout<<point<<endl;

	//測試Circle
	Circle point1(2.0,2.0,1.0);
	Point &pRef=point1;//Point 物件 被 Circle 物件 point1初始化,只初始化(x,y)
	cout<<"original circle:\nx="<<point1.getX()<<",y="<<point1.getY()<<",r="<<point1.getRadius()<<",area="<<point1.area()<<endl;
	cout<<"circle(過載<<):"<<point1;
	cout<<"Point pRef:"<<pRef<<endl;//輸出點的資訊,不輸出圓的資訊

	point1.setRadius(2.0);
	cout<<"original circle:\nx="<<point1.getX()<<",y="<<point1.getY()<<",r="<<point1.getRadius()<<",area="<<point1.area()<<endl;
	cout<<"circle(過載<<):"<<point1;
	cout<<endl;

	point1.setRadius(4.0);
	point1.setPoint(3.1,3.1);
	cout<<"original circle:\nx="<<point1.getX()<<",y="<<point1.getY()<<",r="<<point1.getRadius()<<",area="<<point1.area()<<endl;
	cout<<"circle(過載<<):"<<point1;
	cout<<endl;

	//測試Cylinder
	Cylinder point2(4.0,4.0,3.0,5.0);
	cout<<"Cylinder:"<<point2;
	point2.setPoint(1.0,1.0);
	point2.setRadius(1.0);
	point2.setHeight(1.0);
	cout<<"Cylinder:"<<point2;

	system("pause");
	return 0;
}