[学习笔记] C++ primer plus 第十三章 类继承

类继承:一种用来扩展和修改类的方法。

继承是在原始类的基础上添加新的数据和方法。

可以省时间,不用更改类源码。

一、一个简单的基类

由一个类派生出另外一个类时,原始的类称作基类,继承类称作派生类。

以下是一个基类,表示打乒乓球的成员姓名以及是否在打乒乓球。

程序清单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)
{
}


程序清单13.6
//用户程序
#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........未完待续.......


























                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值