C++中类继承的示例分析

发布时间:2021-08-30 11:40:05 作者:小新
来源:亿速云 阅读:114

这篇文章给大家分享的是有关C++中类继承的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

类继承示例

场景如下:现需要记录乒乓球运动成员的信息,包括姓名和有无空余桌台。其中有一部分成员参加过比赛,需要将这一部分单独提出并记录他们在比赛中的比分。因此,参加过比赛的成员所属的类就是素有成员所属类的派生类对象了。

类声明:

#ifndef TABTENN_H_
#define TABTENN_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;};
};

//derived class
class RatedPlayer:public TableTennisPlayer //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

tabtenn.h

类方法定义:

#include <iostream>
#include "tabtenn.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 methods
//派生类构造函数必须调用基类构造函数
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)
{}

tabtenn.cpp

上述代码将基类TableTennisPlayer和派生类RatedPlayer放在了一起。RatedPlayer类声明中使用:public name_of_base_class 表示公有派生。在派生类的声明中要添加自己的构造函数和额外的成员函数和方法。这里派生类构造函数中学问很大。

在创建派生类对象之前必须先创建基类对象,这是因为派生类的方法无法直接访问基类的私有成员。那问题来了:当创建新的派生类对象时,会自动调用派生类构造函数。如何实现在调用派生类构造函数之前就调用基类构造函数创建基类对象呢?(好绕口)这里需要使用构造函数的特有语法——初始化列表。在程序指针指向派生类构造函数大括号内第一行之前即在初始化列表中完成了基类构造函数的调用。为了方便从基类对象中直接选出派生类对象(基类对象包含派生类对象),使用第二个构造函数,直接为基类对象添加比分信息获得。

三、应用程序示例

应用程序代码:

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

using std::endl;
using std::cout;

int main()
{
  TableTennisPlayer player1("Tara","Boomdea",false);//创建基类对象
  RatedPlayer rplayer1(1140,"Mallory","Duck",true);//创建派生类对象
  player1.Name();
  if(player1.HasTable())
    cout << ": has a table.\n";
  else
    cout << ": hasn't a table.\n";
  rplayer1.Name();
  if(rplayer1.HasTable())
    cout << ": has a table.\n";
  else
    cout << ": hasn't a table.\n";

  //initialize RatedPlayer using TableTennisPlayer object
  RatedPlayer rplayer2(1212,player1);
  cout << "Name: ";
  rplayer2.Name();
  cout << ";Rating: " << rplayer2.Rating() << endl;
  return 0;
}

usett.cpp

 player和rplayer分别代表基类对象和派生类对象。rplayer2和player1其实是同一个人,本来参加过比赛的成员就是从所有成员中挑选出来的。应用该程序比较简单,这里就不过多描述了。

感谢各位的阅读!关于“C++中类继承的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. PHP中类继承关系的示例分析
  2. ECMAScript 6中类继承解析的示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:Hadoop伪分布式环境的搭建步骤

下一篇:如何使用JWT对SpringCloud进行认证和鉴权

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》