C++ Primer Plus学习:第十三章 类继承(1)

基础类
  eg

    tabtenn0.h

#ifndef TABTENN0_H_
#define TABTENN0_H_
class TableTennisPlayer
{
      private:
              enum{LIM = 20};
              char firstname[LIM];
              char lastname[LIM];
              bool hasTable;
      public:
             TableTennisPlayer(const char * fn = "none",const char * ln = "none",bool ht = false);
             void Name()const;
             bool HasTable()const{return hasTable;};
             void ResetTable(bool v){hasTable = v;};
      };
#endif


    tabtenn0.cpp

#include "tabtenn0.h"
#include <iostream>
#include <cstring>

TableTennisPlayer::TableTennisPlayer(const char * fn,const char * ln,bool ht)
{
                                           std::strncpy(firstname,fn,LIM-1);
                                           firstname[LIM-1] = '\0';
                                           std::strncpy(lastname,ln,LIM-1);
                                           lastname[LIM-1] = '\0';
                                           hasTable = ht;
                                           }
                                           
void TableTennisPlayer::Name()const
{
     std::cout<<lastname<<", "<<firstname;
     }
     


    useten.cpp

#include <iostream>
#include "tabtenn0.h"

int main()
{
    using std::cout;
    //true=HasTable() 
    TableTennisPlayer player1("Chuck","Blizzard",true);
    //false=!HasTable()
    TableTennisPlayer player2("Tara","Booddea",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";
        
    system("pause");
    return 0;
    }


派生类
  构造函数要点
    基类对象首先被创建
    派生类构造函数应通过成员初始化列表将基类信息传递给基类构造函数
    派生类构造函数应初始化派生类新增的数据成员 
  eg

    tabtenn1.h

#ifndef TABTENN1_H_
#define TABTENN1_H_
class TableTennisPlayer
{
      private:
              enum{LIM = 20};
              char firstname[LIM];
              char lastname[LIM];
              bool hasTable;
      public:
             TableTennisPlayer(const char * fn = "none",const char * ln = "none",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 char * fn = "none",const char * ln = "none",bool ht = false);
             RatedPlayer(unsigned int r,const TableTennisPlayer & tp);
             unsigned int Rating(){return rating;}
             void ResetRating(unsigned int r){rating = r;}
      };
      
#endif


    tabtenn1.cpp

#include "tabtenn1.h"
#include <iostream>
#include <cstring>

TableTennisPlayer::TableTennisPlayer(const char * fn,const char * ln,bool ht)
{
                                           std::strncpy(firstname,fn,LIM-1);
                                           firstname[LIM-1] = '\0';
                                           std::strncpy(lastname,ln,LIM-1);
                                           lastname[LIM-1] = '\0';
                                           hasTable = ht;
                                           }
                                           
void TableTennisPlayer::Name()const
{
     std::cout<<lastname<<", "<<firstname;
     }
     

//RatedPlayer methods
//TableTennisTable(fn,ln,ht)从基类中全传值 
RatedPlayer::RatedPlayer(unsigned int r,const char * fn,const char * ln,bool ht):TableTennisPlayer(fn,ln,ht)
{
                                  rating = r;
                                  }
                                  
RatedPlayer::RatedPlayer(unsigned int r,const TableTennisPlayer & tp):TableTennisPlayer(tp),rating(r)
{
                                  
                                  }
                                  


    usett2.cpp

#include <iostream>
#include "tabtenn1.h"

int main()
{
    using std::cout;
    using std::endl;
    //true=HasTable() 
    TableTennisPlayer player1("Tara","Booddea",false);
    RatedPlayer rplayer1(1140,"Mallory","Duck",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;
        
    system("pause");
    return 0;
    }


  关系
    派生类对象可以使用基类的方法,条件是方法不是私有的
    不可以将基类对象和地址赋给派生类引用和指针
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值