虚函数
使用基类之指针,指向派生类的对象,调用虚函数的时候,最后调用的是派生类的函数!
程序1虚函数使用举例
#include <iostream>
#include <string>
using namespace std
;
enum EStudentType
{
EStudentType_Error
= 0,
EStudentType_Xiao
= 1,
EStudentType_Zhong
= 2,
};
class CStudent
{
public
:
char *p_name
;
char sex
;
int num
;
int age
;
static int master
;
EStudentType type
;
virtual
void shangke()
{
cout
<< "CStudent" << endl
;
}
CStudent(){ type
= EStudentType_Error
; };
CStudent(char* pname
, char t_sex
, int t_num
, int t_age
) :sex(t_sex
), num(t_num
), age(t_age
)
{
type
= EStudentType_Error
;
p_name
= NULL;
int n_len
= 0;
if (pname
)
{
n_len
= strlen(pname
);
}
if (n_len
> 0)
{
p_name
= new
char[n_len
+ 1];
memset(p_name
, 0, n_len
+ 1);
strcpy(p_name
, pname
);
}
}
CStudent(const CStudent
& stud
)
{
type
= EStudentType_Error
;
int n_len
= 0;
n_len
= strlen(stud
.p_name
);
p_name
= new
char[n_len
+ 1];
memset(p_name
, 0, n_len
+ 1);
strcpy(p_name
, stud
.p_name
);
num
= stud
.num
;
age
= stud
.age
;
sex
= stud
.sex
;
}
CStudent
& operator
=(const CStudent
& stud
)
{
type
= EStudentType_Error
;
int n_len
= 0;
n_len
= strlen(stud
.p_name
);
p_name
= new
char[n_len
+ 1];
memset(p_name
, 0, n_len
+ 1);
strcpy(p_name
, stud
.p_name
);
num
= stud
.num
;
age
= stud
.age
;
sex
= stud
.sex
;
return *this
;
}
bool operator
==(const CStudent
& stud
)
{
int n_len
= 0;
n_len
= strlen(stud
.p_name
);
if ((num
== stud
.num
) && (age
== stud
.age
) && (sex
== stud
.sex
) && (*p_name
== *stud
.p_name
))
{
for (int i
= 0; i
< n_len
;i
++)
{
if (*(p_name
+i
)!= *(stud
.p_name
+i
))
{
return false
;
}
}
return true
;
}
else
{
return false
;
}
}
~CStudent();
};
CStudent
::~CStudent()
{
if (p_name
)
{
delete
[] p_name
;
p_name
= NULL;
}
}
int CStudent
::master
= 0;
class CXiaoStudent
: public CStudent
{
public
:
int yuwen_score
;
int shuxue_score
;
int english_score
;
void shangke()
{
cout
<< "CXiaoStudent" << endl
;
}
CXiaoStudent() : CStudent("zhangsan", 'm', 1001, 20)
{
yuwen_score
= 2;
shuxue_score
= 0;
english_score
= 0;
type
= EStudentType_Xiao
;
}
~CXiaoStudent()
{
cout
<<"析构了" << endl
;
}
};
class CZhongStudent
: public CXiaoStudent
{
public
:
int wuli_score
;
int huaxue_score
;
void shangke()
{
cout
<< "CZhongStudent" << endl
;
}
CZhongStudent()
{
type
= EStudentType_Zhong
;
wuli_score
= 0;
huaxue_score
= 0;
}
~CZhongStudent()
{
cout
<< "析构了" << endl
;
}
};
void average_age(CStudent
* p_arr_stud
, int n_size
)
{
if (!p_arr_stud
|| n_size
<= 0) return;
int total_age
= 0;
EStudentType type
= p_arr_stud
[0].type
;
for (int idx
= 0; idx
< n_size
; ++idx
)
{
switch (type
)
{
case EStudentType_Xiao
:
total_age
+= ((CXiaoStudent
*)p_arr_stud
)[idx
].age
;
break;
case EStudentType_Zhong
:
total_age
+= ((CZhongStudent
*)p_arr_stud
)[idx
].age
;
break;
default:
break;
}
}
int aver_age
= total_age
/ n_size
;
switch (type
)
{
case EStudentType_Xiao
:
cout
<< "小学生的平均年龄是:" << aver_age
<< endl
;
break;
case EStudentType_Zhong
:
cout
<< "中学生的平均年龄是:" << aver_age
<< endl
;
break;
default:
break;
}
}
int main(int argc
, char* argv
[])
{
CZhongStudent stud_zhong
;
stud_zhong
.shangke();
CStudent
*pStud
= &stud_zhong
;
pStud
->shangke();
system("pause");
return 0;
}
例子2不同的狗
#include<iostream>;
using namespace std
;
class CDog
{
public
:
int num
;
virtual
void wangwang()
{
cout
<< "wangwang"<< endl
;
}
CDog(){};
~CDog(){ cout
<< "CDong析构了" << endl
; };
};
class jinmao
:public CDog
{
public
:
void wangwang()
{
cout
<< "jinmaowangwang" << endl
;
}
jinmao(){};
~jinmao(){ cout
<< "jinmao析构了" << endl
; };
};
class jiwawa
:public CDog
{
public
:
void wangwang()
{
cout
<< "jiwawawangwang" << endl
;
}
jiwawa(){};
~jiwawa(){ cout
<< "jiwawa析构了" << endl
; };
};
class zhangao
:public CDog
{
public
:
void wangwang()
{
cout
<< "zhangaowangwang" << endl
;
}
zhangao(){};
~zhangao(){ cout
<< "zhangao析构了" << endl
; };
};
int main(int argc
,char* argv
[])
{
jinmao wang1
;
wang1
.wangwang();
jiwawa wang2
;
wang2
.wangwang();
zhangao wang3
;
wang3
.wangwang();
CDog
* Pwang1
;
Pwang1
= &wang1
;
Pwang1
->wangwang();
CDog
* Pwang2
;
Pwang2
= &wang2
;
Pwang2
->wangwang();
CDog
* Pwang3
;
Pwang3
= &wang3
;
Pwang3
->wangwang();
system("pause");
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-15824.html