C++中类的继承与派生语法:
(子类继承于父类,父类派生出子类,这两个是一个意思)
class 子类名:[继承方式] 父类名{
子类新增加的成员
};
C++中的三种继承方式:
C++继承时的名字遮蔽问题:
如果子类中的成员(包括成员变量和成员函数)和父类中的成员重名,那么就会遮蔽从子类继承过来的成员。所谓遮蔽,就是覆盖,子类成员覆盖了父类成员,当然就不是重载。
一个代码展示类的继承与派生:
/* 类的继承与派生 */
/**************************************************************
* Includes
***************************************************************/
#include <iostream>
/**************************************************************
* Class Father lv1
***************************************************************/
class Game{
private:
/*----------------成员变量--------------*/
char* game_name;
int game_age;
public:
/*----------------构造函数--------------*/
Game(){
this->game_name = "Dota2";
this->game_age = 10;
}
Game(char* name,int age){//构造函数重载
this->game_name = name;
this->game_age = age;
}
/*----------------成员函数--------------*/
int setGameName(char* name){
this->game_name = name;
return 0;
}
int setGameAge(int age){
this->game_age = age;
return 0;
}
char* getGameName(void){
return this->game_name;
}
int getGameAge(void){
return this->game_age;
}
void show(void){
std::cout<<game_name<<"-"<<game_age<<"年"<<std::endl;
}
};
/**************************************************************
* Class Child lv2
***************************************************************/
class Hero: public Game{
private:
/*----------------成员变量--------------*/
char* hero_name;
int hero_skill_num;
public:
/*----------------构造函数--------------*/
//值得注意的是此处的子类的构造函数写法:
Hero():Game(){
this->hero_name = "Invoker";
this->hero_skill_num = 11;
}
Hero(char* hero_name,int hero_skill_num):Game(){//构造函数重载
this->hero_name = hero_name;
this->hero_skill_num = hero_skill_num;
}
Hero(char* game_name,int game_age,char* hero_name,int hero_skill_num)
:Game(game_name,game_age){//构造函数重载
this->hero_name = hero_name;
this->hero_skill_num = hero_skill_num;
}
/*----------------成员函数--------------*/
int setGameName(char* name){
Game::setGameName(name);
return 0;
}
int setGameAge(int age){
Game::setGameAge(age);
return 0;
}
int setHeroName(char* name){
this->hero_name = name;
return 0;
}
int setHeroSkillNum(int num){
this->hero_skill_num = num;
return 0;
}
void show(void){
Game::show();
std::cout<<hero_name<<"-"<<hero_skill_num<<"个技能"<<std::endl;
}
};
/**************************************************************
* Class Child lv3
***************************************************************/
class Skill: public Hero{
private:
char* skill_name;
public:
/*----------------构造函数--------------*/
Skill():Hero(){
this->skill_name = "哈雷克之火葬魔咒";
}
Skill(char* name):Hero(){
this->skill_name = name;
}
Skill(char* hero_name,int hero_skill_num,char* name)
:Hero(hero_name,hero_skill_num){
this->skill_name = name;
}
Skill(char* game_name,int game_age,char* hero_name,int hero_skill_num,char* name)
:Hero(game_name,game_age,hero_name,hero_skill_num){
this->skill_name = name;
}
/*----------------成员函数--------------*/
void show(void){
Hero::show();
std::cout<<skill_name<<std::endl;
}
/*....*/
};
/**************************************************************
* Main Function
***************************************************************/
int main(int argc, char** argv) {
Game A;
A.show();
std::cout<<std::endl;
Hero B("Lina",4);
B.show();
std::cout<<std::endl;
Skill C;
C.show();
std::cout<<std::endl;
Skill D("Jugg",4,"无敌斩");
D.show();
std::cout<<std::endl;
return 0;
}