虚函数实现多态的使用例子

本文通过具体的C++代码示例展示了多态性和继承的概念。首先定义了一个基础的学生类,接着创建了两个派生类:大学本科生类和研究生类,并实现了各自的打印消息方法。此外,还演示了如何使用泛型函数来处理不同类型的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


例子一:

#include "StdAfx.h"  
#include <iostream>  
#include <vector>  
#include <iostream>  
#include <algorithm>  
#include <functional>  

using namespace std;


#define IF_FEMALE(female) ((true == female) ? "true" : "false");
class student
{
public:
	char * name;
	bool female;
	int  age;

	student();
	~student();
	student(char * name, bool  female, int age);

	virtual void print_message();
};

class UniversityStudent : public student
{
public:
	char * schoolName;

	UniversityStudent();
	~UniversityStudent();
	UniversityStudent(char * name, bool  female, int age, char * schoolName);

	void print_message();

};

class MasterStudent : public student
{
public:
	char * degree;

	MasterStudent();
	~MasterStudent();
	MasterStudent(char * name, bool  female, int age, char * degree);
	void print_message();
};


student :: student()
{

}

student :: ~student()
{

}

student :: student(char * name, bool  female, int age)
{
	this->name = name; 
	this->female = female;
	this->age = age;
}

void student :: print_message()
{
	char * p = IF_FEMALE(this->female);
	printf("\n ************************** ");
	printf("\n name   : %s ", this->name);
	printf("\n female : %s ", p);
	printf("\n age    : %d ", this->age);
}

UniversityStudent :: UniversityStudent()
{

}

UniversityStudent :: ~UniversityStudent()
{

}

UniversityStudent :: UniversityStudent(char * name, bool  female, int age, char * schoolName):student(name,female,age)
{
	this->schoolName = schoolName;
}

void UniversityStudent :: print_message()
{
	char * p = IF_FEMALE(this->female);
	printf("\n ************************** ");
	printf("\n name   : %s ", this->name);
	printf("\n female : %s ", p);
	printf("\n age    : %d ", this->age);
	printf("\n schoolName : %s ", this->schoolName);
}

MasterStudent :: MasterStudent()
{

}

MasterStudent :: ~MasterStudent()
{

}

MasterStudent :: MasterStudent(char * name, bool  female, int age, char * degree) : student(name,female,age)
{
	this->degree = degree;
}

void MasterStudent :: print_message()
{
	char * p = IF_FEMALE(this->female);
	printf("\n ************************** ");
	printf("\n name   : %s ", this->name);
	printf("\n female : %s ", p);
	printf("\n age    : %d ", this->age);
	printf("\n degree : %s ", this->degree);
}

void print_people_message(student * stu)
{
	stu->print_message();
};


typedef vector <student*> :: iterator stuIndex;
typedef void (*FUN_print)(student * stu);


void main(void)  
{ 
	student std("lily", true, 18);
	UniversityStudent uniStd("lucy", true, 19, "nwpu");
	MasterStudent mastStd("sam", false, 20, "Master ");

	vector<student *> people;

	people.push_back(&std);
	people.push_back(&uniStd);
	people.push_back(&mastStd);

	FUN_print fun = print_people_message;

	//for_each(people.begin(),people.end(),fun);

	stuIndex indexEnd = people.end();
	for(stuIndex i=people.begin();i!=indexEnd;i++)
	{
		fun(*i);
	}
	//student * p = &std;
	//p->print_message();

	//p = &uniStd;
	//p->print_message();

	//p = &mastStd;
	//p->print_message();

}

输出结果:




例子二:

#include "StdAfx.h"  
#include <iostream>  
#include <vector>  
#include <iostream>  
#include <algorithm>  
#include <functional>  

using namespace std;

typedef struct _Point
{
	int x;
	int y;

	_Point();
	~_Point();
	_Point(int x,int y);
	virtual void print_message();
	friend _Point operator + (_Point pointA, _Point pointB);
}Point;

typedef struct _Circle : _Point
{
	int radius; 

	_Circle();
	~_Circle();
	_Circle(int x,int y, int radius);
	void print_message();
}Circle;

typedef struct _Cylinder : _Circle
{
	int height;

	_Cylinder();
	~_Cylinder();
	_Cylinder(int x,int y, int radius,int height);
	void print_message();

}Cylinder;


_Point operator + (_Point pointA, _Point pointB)
{
	_Point point;
	point.x = pointA.x + pointB.x;
	point.y = pointA.y + pointB.y;

	return point;
}

_Point :: _Point()
{

}

_Point :: ~_Point()
{

}

_Point :: _Point(int x,int y)
{
	this->x = x;
	this->y = y;
}

void _Point :: print_message()
{
	printf("\n ************************** ");  
	printf("\n point : (%d %d) ",this->x,this->y);
}


_Circle :: _Circle()
{

}

_Circle :: ~_Circle()
{

}

_Circle :: _Circle(int x,int y,int radius):_Point(x,y)
{
	this->radius = radius;
}

void _Circle :: print_message()
{
	printf("\n ************************** ");  
	printf("\n center : (%d %d) ",this->x,this->y);
	printf("\n radius : (%d) ",this->radius);
}


_Cylinder :: _Cylinder()
{

}

_Cylinder :: ~_Cylinder()
{

}

_Cylinder :: _Cylinder(int x,int y, int radius,int height) : _Circle(x,y, radius)
{
	this->height = height;
}

void _Cylinder :: print_message()
{
	printf("\n ************************** ");  
	printf("\n center : (%d %d) ",this->x,this->y);
	printf("\n radius : (%d) ",this->radius);
	printf("\n height : (%d) ",this->height);
}


typedef vector <Point*> :: iterator poiIndex;  
typedef void (*FUN_print)(Point * stu);  

void print_people_message(Point * point)  
{  
	point->print_message();  
};  

void main()
{
	Point * pointA = new Point(640,480); 
	Circle circleB(640,480,100);
	Cylinder cylinderC(640,480,100,200);

	vector<Point *> points;  

	points.push_back(pointA);  
	points.push_back(&circleB);  
	points.push_back(&cylinderC);  

	Point pointB(352,288);
	Point pointC = *pointA + pointB;
	points.push_back(&pointB);  
	points.push_back(&pointC);  

	FUN_print fun = print_people_message;  
	for_each(points.begin(),points.end(),fun); 

	return;
}


输出结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值