闲云潭影日悠悠,物换星移几度秋
你既然已经做出了选择, 又何必去问为什么选择。鬼谷绝学的要义, 从来都不是回答, 而是抉与择
普通类
#ifndef TABTENN0_H_ #define TABTENN0_H_ #include <string> using namespace std; 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
#include <iostream> #include <string> #include "tabtenn0.h" using namespace std; TableTennisPlayer::TableTennisPlayer(const string & fn, const string & ln, bool ht): firstname(fn), lastname(ln), hasTable(ht) {} void TableTennisPlayer::Name () const { std::cout << lastname << "," << firstname ; }
#include <iostream> #include "tabtenn0.h" using namespace std; int main (int argc, char const *argv[]) { TableTennisPlayer player1("Chuck", "Blizzard", true); TableTennisPlayer player2("Tara", "Boomdea", false); player1.Name(); if (player1.HasTable()){ std::cout << ":has a table." << '\n'; }else{ std::cout << ":hasn't a table." << '\n'; }; player2.Name(); if (player2.HasTable()){ std::cout << ":has a table" << '\n'; }else{ std::cout << ":hasn't a table." << '\n'; }; return 0; }
继承类
分为三种:
{
公有继承,
保护继承,
扫描二维码关注公众号,回复:
1867220 查看本文章
私有继承
}
从一个类派生出另一个类时, 原始类成为基类, 继承类成为派生类。粗俗的讲:子继承父, 父就是基类, 子就是派生类
这说明继承, 首先需要一个基类。
对于公有派生, 派生类对象包含基类对象, 基类的公有成员将成为派生类的公有成员;
基类的私有部分也将成为派生类的一部分, 但只能通过基类的公有和保护方法访问。换句话说: "派生类不能访问基类的私有成员, 必须通过基类的方法进行"
简单写法
class RatedPlayer: public TableTennisPlayer { private: public: };
构造函数
RatedPlayer::RatedPlayer( unsigned int r, const string & fn, const string & fn, cosnt string & ln, bool ht ): TableTennisPlayer(fn, ln, ht) // 初始化基类构造函数, 或者说成员初始化列表 { rating = r; }
// 如果赋值RatedPlayer rplayer(1140, "Mallory", "Duck", true),
// 同时将“Mallory”, "Duck" 和 true赋给fn、ln和ht作为实参传递给TableTennisPlayer构造函数
如果省略成员初始化列表, 将会怎么样?
RatedPlayer::RatedPlayer( unsigned int r, const string & fn, const string & fn, cosnt string & ln, bool ht ) { rating = r; } 如果不调用基类构造函数, 与下面代码等效, 程序将使用默认的基类构造函数 RatedPlayer::RatedPlayer( unsigned int r, const string & fn, const string & fn, cosnt string & ln, bool ht ): TableTennisPlayer() // 程序将使用默认的基类构造函数, 上面代码与此等效 { rating = r; }
注意: 除非要使用默认构造函数, 否则应显式调用正确的基类构造函数。
RatedPlayer::RatedPlayer( unsigned int r, const string & fn, const string & fn, cosnt string & ln, bool ht ): TableTennisPlayer(tp) { rating = r; }
未完待续...