C++之通俗成員函數、虛函數和純虛函數的差別與用法要點。本站提示廣大學習愛好者:(C++之通俗成員函數、虛函數和純虛函數的差別與用法要點)文章只能為提供參考,不一定能成為您想要的結果。以下是C++之通俗成員函數、虛函數和純虛函數的差別與用法要點正文
通俗成員函數是靜態編譯的,沒有運轉時多態,只會依據指針或援用的“字面值”類對象,挪用本身的通俗函數;虛函數為了重載和多態的須要,在基類中界說的,即使界說為空;純虛函數是在基類中聲明的虛函數,它可以再基類中有界說,且派生類必需界說本身的完成辦法。
假定我們有三個類Person、Teacher、Student它們之間的關系以下:
類的關系圖
通俗成員函數
【Demo1】
依據這個類圖,我們有上面的代碼完成
#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__
測試代碼:
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】
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】
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
虛函數用法要點:
【Demo4】:針對第4點解釋:
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點停止解釋:
設計我們的類以下界說。
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:
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:
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】
class Person
{
public:
//... 省略
virtual void DoWork() = 0;
//... 省略
};
但此時我們編譯
Person* pPerson = new Person("張三", 22);
這句話時會報錯:error C2259: ‘Person' : cannot instantiate abstract class
這是由於我們並沒無為Person完成DoWork辦法,而包括純虛函數的類是一個籠統的類,籠統類不克不及被實例化。
因而我們在子類中對它完成以下:
【Demo7】
class Teacher : public Person
{
public:
//... 省略
virtual void DoWork()
{
cout << "教書..." << endl;
}
//... 省略
};
class Student : public Person
{
public:
//... 省略
virtual void DoWork()
{
cout << "進修..." << endl;
}
//... 省略
};
沒用DoWork辦法:
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;
}
成果:
教書…
進修…
純虛函數用法要點:
經由過程以上對通俗成員函數、虛函數和純虛函數的引見,願望可以對年夜家有所贊助。