C++多态性与虚函数(谭浩强12.1-12.4)

例12.1,一个典型的建立点、面、体的静态多态性例子:

#include 
using namespace std;
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; }
	friend ostream&operator<<(ostream&, const Point&);
protected:
	float x, y;
};
Point::Point(float a, float b)//基类构造函数,并对x,y初始化
{
	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.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;
}
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&, 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.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;//pRef是Point类对象的引用
	cout << "\npRef as a point :\n" << pRef;//pRef作为一个点输出
	Circle &cRef = cy1;//cRef是Circle类对象的引用
	cout << "\ncRef as aCircle :\n" << cRef;//cRef作为一个圆输出
	return 0;
}

程序运行结果如图:
C++多态性与虚函数(谭浩强12.1-12.4)_第1张图片

12.2基类与派生类中有同名函数:

#include 
using namespace std;
#include
class Student
{
public:
	Student(int, string, float);
	virtual void display();//声明基类的成员函数为虚函数
protected:
	int num;
	string name;
	float score;
};
Student::Student(int n, string nam, float s)
//类外定义构造函数需要指明作用域
{
	num = n;
	name = nam;
	score = s;
}
void Student::display()
{
	cout << endl << "num:" << num << endl;
	cout << "name:" << name << endl;
	cout << "score:" << score << endl;
}
class Graduate :public Student//声明公用派生类Graduate
{
public:
	Graduate(int, string, float, float);
	virtual void display();//成员函数声明为虚函数
private:
	float wage;
};
Graduate::Graduate(int n, string nam, float s, float w)
//类外定义构造函数需要指明作用域
: Student(n, nam, s), wage(w){}
//定义构造函数多重继承的构造函数初始化表
void Graduate::display()
{
	Student::display();
	cout << "wages=" << wage << endl;
}
int main()
{
	Student stud1(1001, "Li", 87.5);
	Graduate gradl(2001, "wang", 89.5, 1000);
	Student *pt = &stud1;//定义指向基类Student对象的指针
	pt->display();
	pt = &gradl;/*指针指向派生类Graduate的对象gradl,由于基类声明
了成员函数display()是虚函数,故能访问派生类中新增加的成员,*/
	pt->display();
	return 0;
}

程序执行效果如图:
C++多态性与虚函数(谭浩强12.1-12.4)_第2张图片
12.4,纯虚函数和抽象类实例:

#include 
using namespace std;
class Shape
{
public:
	virtual float area()const{ return 0.0; }//虚函数
	virtual float volume()const{ return 0.0; }//虚函数
	virtual void shapeName()const = 0;//纯虚函数
};
class Point:public Shape
{
public:
	Point(float = 0, float= 0);
	void setPoint(float, float);
	float getX()const{ return x; }//读x坐标,getX函数为常成员函数
	float getY()const{ return y; }
	virtual void shapeName()const{ cout << "Point:"; }//对虚函数再定义
	friend ostream&operator<<(ostream&, const Point&);
protected:
	float x, y;
};
Point::Point(float a, float b)//基类构造函数,并对x,y初始化
{
	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;
	virtual float area()const;
	virtual void shapeName()const{ cout << "Circle:"; }
	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.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;
}
class Cylinder :public Circle
{
public:
	Cylinder(float x = 0, float y = 0, float r = 0, float h = 0);
	void setHeight(float);
	//float getHeight()const;
	virtual float area()const;
	virtual float volume()const;
	virtual void shapeName()const{ cout << "Cylinder:"; }
	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.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()
{
	Point point(3.2, 4.5);
	Circle circle(2.4, 1.2, 5.6);
	Cylinder cylinder(3.5, 6.4, 5.2, 10.5);
	point.shapeName();//用对象名建立静态关联
	cout << point << endl;
	circle.shapeName(); //静态关联
	cout << circle << endl;
	cylinder.shapeName();//静态关联
	cout << cylinder << endl << endl;
	Shape *pt;//定义基类指针
	pt=&point;//使指针指向Point类对象
	pt->shapeName();//用指针建立动态关联
	cout << "x=" << point.getX() << ",y=" << point.getY() <<
		"\narea=" << pt->area() << "\nvolume=" << pt->volume()
		<< "\n\n";
	pt = &circle;
	pt->shapeName();//动态关联
	cout << "x=" << circle.getX() << ",y=" << circle.getY() <<
		"\narea=" << pt->area() << "\nvolume=" << pt->volume()
		<< "\n\n";
	pt = &cylinder;
	pt->shapeName();//动态关联
	cout << "x=" << cylinder.getX() << ",y=" << cylinder.getY() <<
		"\narea=" << pt->area() << "\nvolume=" << pt->volume()
		<< "\n\n";
	return 0;
}

程序执行效果如图:
C++多态性与虚函数(谭浩强12.1-12.4)_第3张图片

你可能感兴趣的:(C++程序设计,c++)