## 多态的实现

//student.h
#include <iostream>
using namespace std;
class student
{
public:
student(char *n,int a,int h,int w);
student();
void set(char *n,int a,int h,int w);
char * sname();
int sage();
int sheight();
int sweight();
virtual void study();//把学习设置为虚函数
protected:
char name[10];
int age;
int height;
int weight;
};
char * student::sname()
{
return name;
}
int student::sage()
{
return age;
}
int student::sheight()
{
return height;
}
int student::sweight()
{
return weight;
}
void student::set(char *n,int a,int h,int w)
{
int i;
for (i=0;n[i]!='\0';i++)
{
name[i]=n[i];
}
name[i]='\0';
age=a;
height=h;
weight=w;
return;
}
student::student(char *n,int a,int h,int w)
{
cout <<"Constructing a student with parameter..." <<endl;
set(n,a,h,w);
}
student::student()
{
cout <<"Constructing a student without parameter..." <<endl;
}
void student::study()//成员函数定义处没有virtual
{
cout <<"随便学些什么。" <<endl;
return;
}
#include "student.h"
{
public:
double score();
void setGPA(double g);
virtual void study();//把学习设置为虚函数
protected:
double GPA;
};
{
return GPA;
}
{
GPA=g;
return;
}
{
return age>=18?true:false;
}
{
cout <<"学习高等数学和大学英语。" <<endl;
return;
}
//pupil.h
class Pupil:public student
{
public:
virtual void study();//把学习设置为虚函数
};
void Pupil::study()
{
cout <<"学习语数外。" <<endl;
return;
}
//main.cpp
#include <iostream>
#include "pupil.h"
using namespace std;
int main()
{
student s2;
Pupil s3;
student *sp=&s1;//sp指向本科生对象
s1.set("Tom",21,178,60);
sp->study();//体现多态性
sp=&s2; //sp指向学生对象
s2.set("Jon",22,185,68);
sp->study();//体现多态性
sp=&s3; //sp指向小学生对象
s3.set("Mike",8,148,45);
sp->study();//体现多态性
return 0;
}

Constructing a student without parameter...
Constructing a student without parameter...
Constructing a student without parameter...

## 无法实现多态的虚函数

//student.h
#include <iostream>
using namespace std;
class student
{
public:
student(char *n,int a,int h,int w);
student();
void set(char *n,int a,int h,int w);
char * sname();
int sage();
int sheight();
int sweight();
virtual void study(int c=0);//设置为虚函数，带默认参数
protected:
char name[10];//姓名
int age;//年龄
int height;//身高
int weight;//体重
};
……
void student::study(int c)
{
cout <<"随便学些什么。" <<endl;
return;
}
//main.cpp
#include <iostream>
#include "pupil.h"
using namespace std;
int main()
{
student s2;
Pupil s3;
student *sp=&s1;
s1.set("Tom",21,178,60);
sp->study(1);//带参数
sp=&s2;
s2.set("Jon",22,185,68);
sp->study();
sp=&s3;
s3.set("Mike",8,148,45);
sp->study();
return 0;
}

Constructing a student without parameter...
Constructing a student without parameter...
Constructing a student without parameter...

#### C++的多态与虚函数

2015-01-06 21:49:28

#### C++ 虚函数使用注意点 与 多态的关系

2016-12-12 21:36:36

#### C++学习笔记之多态（虚函数）

2015-10-03 15:16:16

#### C++动多态实现之虚函数与虚表指针

2017-03-27 18:07:22

#### 关于虚函数和多态（很好的解释）

2014-07-17 12:15:25

#### 利用C++模板，代替虚函数，实现类的静态多态性

2017-01-03 11:09:43

#### 多态，虚函数，纯虚函数

2016-08-22 14:29:08

#### C++多态性 虚函数与纯虚函数 重载与覆盖

2015-05-02 19:56:15

#### C++中为什么要用虚函数、指针或引用才能实现多态？

2013-11-07 21:05:31

#### C++虚函数多态性的实现与分析+虚继承的实现与分析

2014-04-15 22:44:34

C++多态和虚函数