例子一:
#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;
}
输出结果: