类继承:一种用来扩展和修改类的方法。
继承是在原始类的基础上添加新的数据和方法。
可以省时间,不用更改类源码。
一、一个简单的基类
由一个类派生出另外一个类时,原始的类称作基类,继承类称作派生类。
以下是一个基类,表示打乒乓球的成员姓名以及是否在打乒乓球。
程序清单13.1 tabtenn0.h
//tabtenn0.h 乒乓球会员的基类
#ifndef TABTENN0_H_
#define TABTENN0_H_
#include <string>
using std::string;
//简单的基类
class TableTennisPlayer
{
private:
string firstname;
string lastname;
bool hasTable;
public:
TableTennisPlayer(const string & fn ="none",const string & ln ="none",bool ht = false);
void Name()const;
bool HasTable()const{return hasTable;};
void ResetTable(bool v){hasTable = v};
};
#endif // TABTENN0_H_
分析:构造函数使用了成员初始化列表语法
程序清单13.2 tabtenn0.cpp
//tabtenn0.cpp 简单基类的方法
#include <iostream>
#include "tabtenn0.h"
TableTennisPlayer::TableTennisPlayer(const string & fn,const string & ln, bool ht):firstname(fn),lastname(ln),hasTable(ht){}
void TableTennisPlayer::Name()const
{
std::cout<<lastname<<", "<<firstname;
}
分析:C++自带库使用“<>”,如果是当前文件夹编写的头文件则使用“”包含;函数定义书写 大类型+小类型::函数名()。。。。。
程序清单13.3 usett0.cpp
//主程序,使用基类
#include <iostream>
#include "tabtenn0.h"
int main()
{
using std::cout;
TableTennisPlayer player1("C","Wy",true);
TableTennisPlayer player2("W","Kc",false);
player1.Name();
if(player1.HasTable())
cout<<":has a table.\n";
else
cout<<":hasn't a table.\n";
player2.Name();
if(player2.HasTable())
cout<<":has a table.\n";
else
cout<<":hasn't a table.\n";
return 0;
}
以上程序是我们介绍的一个简单的基类,下面我们将派生一个类RatedPlayer。
派生类存储了基类的数据成员以及方法。派生类需要添加自己的构造函数,可以添加额外的数据成员和成员函数。派生类的构造函数必须使用基类的共有函数才能访问基类的私有数据。
使用派生类:
//tabtenn1.h 乒乓球成员 基类
#ifndef TABTENN1_H_
#define TABTENN1_H_
using std::string;
//简单的基类
class TableTennisPlayer
{
private:
string firstname;
string lastname;
bool hasTable;
public:
TableTennisPlayer(const string & fn, const string & ln, bool ht = false);
void Name()const;
bool HasTable() const{return hasTable;};
void ResetTable(bool v){hasTable = v;};
};
//简单的派生类 simple derived class
class RatedPlayer : public TableTennisPlayer
{
private:
unsigned int rating;
public:
RatedPlayer(unsigned int r = 0,const string & fn = "none",
const string & ln = "none", bool ht = false);
RatedPlayer(unsigned int r,const TableTennisPlayer & tp);
unsigned int Rating()const{return rating;};
void ResetRating(unsigned int r){rating = r;};
};
#endif // TABTENN1_H_
程序清单13.5 tabtenn1.cpp
//基类 类方法
#include <iostream>
#include "tabtenn1.h"
TableTennisPlayer::TableTennisPlayer(const string & fn, const string & ln,
bool ht):firstname(fn),lastname(ln),hasTable(ht){}
void TableTennisPlayer::Name() const
{
std::cout<<lastname<<", "<<firstname;
}
//派生类的方法
RatedPlayer::RatedPlayer(unsigned int r,const string & fn,const string & ln,
bool ht):TableTennisPlayer(fn,ln,ht)
{
rating = r;
}
RatedPlayer::RatedPlayer(unsigned int r,const TableTennisPlayer & tp):TableTennisPlayer(tp),rating(r)
{
}
//用户程序
#include <iostream>
#include "tabtenn1.h"
int main()
{
using std::cout;
using std::endl;
TableTennisPlayer player1("C","Wy",false);
RatedPlayer rplayer1(1140,"W","Kc",true);
rplayer1.Name(); //派生类使用基类方法
if(rplayer1.HasTable())
cout <<"has a table.\n";
else
cout <<"hasn't a table.\n";
player1.Name(); //基类使用基方法
if(player1.HasTable())
cout <<": has a table.\n";
else
cout <<": hasn't a table.\n";
cout <<"Name: ";
rplayer1.Name();
cout<<": Rating: "<<rplayer1.Rating()<<endl;
//使用基类方法初始化派生类
RatedPlayer rplayer2(1212, player1);
cout<<"Name:";
rplayer2.Name();
cout <<";Rating: "<<rplayer2.Rating()<<endl;
return 0;
}
C++有三种继承方法:公有继承、保护继承和私有继承
公有继承建立一种is-a的关系。这时,派生类也是一种基类,可以进行继续派生。
(后面的看不懂了,复习一下书本前面的)
........20170329........未完待续.......