C++之普通成员函数、虚函数以及纯虚函数的区别与用法要点
普通成员函数是静态编译的,没有运行时多态,只会根据指针或引用的“字面值”类对象,调用自己的普通函数;虚函数为了重载和多态的需要,在基类中定义的,即便定义为空;纯虚函数是在基类中声明的虚函数,它可以再基类中有定义,且派生类必须定义自己的实现方法。
假设我们有三个类Person、Teacher、Student它们之间的关系如下:
类的关系图
普通成员函数
【Demo1】
根据这个类图,我们有下面的代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#ifndef
__OBJEDT_H__
#define
__OBJEDT_H__
#include
<string>
#include
<iostream>
class
Person
{
public :
Person( const
string& name, int
age) : m_name(name), m_age(age)
{
}
void
ShowInfo()
{
cout
<< "姓名:"
<< m_name << endl;
cout
<< "年龄:"
<< m_age << endl;
}
protected :
string
m_name; //姓名
int
m_age; //年龄
};
class
Teacher : public
Person
{
public :
Teacher( const
string& name, int
age, const
string& title)
:
Person(name, age), m_title(title)
{
}
void
ShowInfo()
{
cout
<< "姓名:"
<< m_name << endl;
cout
<< "年龄:"
<< m_age << endl;
cout
<< "职称:"
<< m_title << endl;
}
private :
string
m_title; //职称
};
class
Student : public
Person
{
public :
Student( const
string& name, int
age, int
studyId)
:
Person(name, age), m_studyId(studyId)
{
}
void
ShowInfo()
{
cout
<< "姓名:"
<< m_name << endl;
cout
<< "年龄:"
<< m_age << endl;
cout
<< "学号:"
<< m_studyId << endl;
}
private :
int
m_studyId; //学号
};
#endif
//__OBJEDT_H__
|
测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
void
test()
{
Person*
pPerson = new
Person( "张三" ,
22);
Teacher*
pTeacher = new
Teacher( "李四" ,
35, "副教授" );
Student*
pStudent = new
Student( "王五" ,
18, 20151653);
pPerson->ShowInfo();
cout
<< endl;
pTeacher->ShowInfo();
cout
<< endl;
pStudent->ShowInfo();
cout
<< endl;
delete
pPerson;
delete
pTeacher;
delete
pStudent;
}
|
结果:
姓名:张三
年龄:22
姓名:李四
年龄:35
职称:副教授
姓名:王五
年龄:18
学号:20151653
说明:
这里的ShowInfo就是一个普通的函数。pPerson、pTeacher和pStudent三个对象调用ShowInfo分别展示自己的信息。
我们知道:父类的指针是可以指向子类的对象的。我们把上面的测试代码稍微改一下:
【Demo2】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
void
test()
{
Person*
pPerson = new
Person( "张三" ,
22);
Person*
pTeacher = new
Teacher( "李四" ,
35, "副教授" );
Person*
pStudent = new
Student( "王五" ,
18, 20151653);
pPerson->ShowInfo();
cout
<< endl;
pTeacher->ShowInfo();
cout
<< endl;
pStudent->ShowInfo();
cout
<< endl;
delete
pPerson;
delete
pTeacher;
delete
pStudent;
}
|
结果:
姓名:张三
年龄:22
姓名:李四
年龄:35
姓名:王五
年龄:18
这时,pTeacher和pStudent只输出了姓名和年龄,并没有输出子类所具有的特性(职称和学号)。这应该不是你期望的结果,你可能期望pTeacher和pStudent输出老师和学生的完整信息,这时就需要用虚函数。
虚函数
我们把Person中的ShowInfo成员改成虚函数(在前面加上virtual),代码如下:
【Demo3】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class
Person
{
public :
Person( const
string& name, int
age) : m_name(name), m_age(age)
{
}
virtual
void
ShowInfo()
{
cout
<< "姓名:"
<< m_name << endl;
cout
<< "年龄:"
<< m_age << endl;
}
protected :
string
m_name; //姓名
int
m_age; //年龄
};
|
在执行上面【Demo2】中的测试代码,得到我们想到的结果:
姓名:张三
年龄:22
姓名:李四
年龄:35
职称:副教授
姓名:王五
年龄:18
学号:20151653
虚函数用法要点:
- 虚函数的声明方式:virtual RETURN_TYPE functionName(ARGS 参数列表);
- 虚函数作用:现实C++中的多态,进行动态绑定(父类指针可指向子类的对象),直到运行时才知道要调用哪个版本(哪个类定义)的函数;
- 我们必要对虚函数进行定义;
- 一旦父类的成员函数声明virtual,其子类的函数不管有没有声明为virtual,都是虚函数;
- 如果虚函数使用默认实参,父类和子类定义的默认实参最好一致。
【Demo4】:针对第4点说明:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class
Person
{
public :
Person( const
string& name, int
age) : m_name(name), m_age(age)
{
}
virtual
void
ShowInfo()
{
cout
<< "姓名:"
<< m_name << endl;
cout
<< "年龄:"
<< m_age << endl;
}
string
GetName(); //正确,普通函数如果不被使用,可以只有声明没有定义
virtual
int
GetAge(); //错误,虚函数必须要有定义,即使是一个空实现,因为编译器无法确定会使用哪个函数
protected :
string
m_name; //姓名
int
m_age; //年龄
};
|
【Demo5】:针对第5点进行说明:
设计我们的类如下定义。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
class
Person
{
public :
virtual
void
SetAge( int
age = 0)
{
m_age
= age;
}
//...
省略
};
class
Teacher : public
Person
{
public :
virtual
void
SetAge( int
age = 1)
{
m_age
= age;
}
//...
省略
};
class
Student : public
Person
{
public :
virtual
void
SetAge( int
age = 2)
{
m_age
= age;
}
//...
省略
};
|
测试1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
void
test()
{
Person*
pPerson = new
Person( "张三" ,
22);
Teacher*
pTeacher = new
Teacher( "李四" ,
35, "副教授" );
Student*
pStudent = new
Student( "王五" ,
18, 20151653);
pPerson->SetAge();
pTeacher->SetAge();
pStudent->SetAge();
pPerson->ShowInfo();
cout
<< endl;
pTeacher->ShowInfo();
cout
<< endl;
pStudent->ShowInfo();
cout
<< endl;
delete
pPerson;
delete
pTeacher;
delete
pStudent;
}
|
结果:
姓名:张三
年龄:0
姓名:李四
年龄:1
职称:副教授
姓名:王五
年龄:2
学号:20151653
测试2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
void
test()
{
Person*
pPerson = new
Person( "张三" ,
22);
Person*
pTeacher = new
Teacher( "李四" ,
35, "副教授" );
Person*
pStudent = new
Student( "王五" ,
18, 20151653);
pPerson->SetAge();
pTeacher->SetAge();
pStudent->SetAge();
pPerson->ShowInfo();
cout
<< endl;
pTeacher->ShowInfo();
cout
<< endl;
pStudent->ShowInfo();
cout
<< endl;
delete
pPerson;
delete
pTeacher;
delete
pStudent;
}
|
结果:
姓名:张三
年龄:0
姓名:李四
年龄:0
职称:副教授
姓名:王五
年龄:0
学号:20151653
纯虚函数
在上面的例子中,我们假设所有的人都要工作,但不同的人工作的方式不同。于是我们就要强制要求继承自Person的子类都要有工作的方法,这就需要纯虚函数。定义如下:
【Demo6】
1
2
3
4
5
6
7
|
class
Person
{
public :
//...
省略
virtual
void
DoWork() = 0;
//...
省略
};
|
但此时我们编译
1
|
Person*
pPerson = new
Person( "张三" ,
22);
|
这句话时会报错:error C2259: ‘Person' : cannot instantiate abstract class
这是因为我们并没有为Person实现DoWork方法,而包含纯虚函数的类是一个抽象的类,抽象类不能被实例化。
于是我们在子类中对它实现如下:
【Demo7】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class
Teacher : public
Person
{
public :
//...
省略
virtual
void
DoWork()
{
cout
<< "教书..."
<< endl;
}
//...
省略
};
class
Student : public
Person
{
public :
//...
省略
virtual
void
DoWork()
{
cout
<< "学习..."
<< endl;
}
//...
省略
};
|
没用DoWork方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
void
test()
{
Person*
pTeacher = new
Teacher( "李四" ,
35, "副教授" );
Person*
pStudent = new
Student( "王五" ,
18, 20151653);
pTeacher->DoWork();
cout
<< endl;
pStudent->DoWork();
cout
<< endl;
delete
pTeacher;
delete
pStudent;
}
|
结果:
教书…
学习…
纯虚函数用法要点:
- 纯虚函数的声明方式:virtual RETURN_TYPE functionName(ARGS 参数列表) = 0;
- 含有纯虚函数的类是一个抽象的类,抽象类不能被实例化。
- 包含纯虚函数的抽象类常用来当作对外的接口,说明这个类有什么功能,而没有具体的实现,基体的实现交由子类完成。
通过以上对普通成员函数、虚函数以及纯虚函数的介绍,希望可以对大家有所帮助。