C++ 派生类

定义派生类的基类

tabtenn.h

  1. #ifndef PRO1_TABTENN_H
  2. #define PRO1_TABTENN_H
  3. class TableTennisPlayer {
  4. private:
  5. enum {LIM = 20};
  6. char firstname[LIM];
  7. char lastname[LIM];
  8. bool hasTable;
  9. public:
  10. TableTennisPlayer(const char* fn = "none", const char* ln = "none", bool ht = false);
  11. void Name()const ;
  12. bool HasTable()const { return hasTable;};
  13. void ResetTable(bool v){hasTable = v;};
  14. };
  15. #endif //PRO1_TABTENN_H

tabtenn.cpp

  1. #include "tabtenn.h"
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5. TableTennisPlayer::TableTennisPlayer(const char *fn, const char *ln, bool ht) {
  6. strncpy(firstname, fn, LIM - 1);
  7. firstname[LIM - 1] = '\0';
  8. strncpy(lastname, ln, LIM -1);
  9. lastname[LIM - 1] = '\0';
  10. hasTable = ht;
  11. }
  12. void TableTennisPlayer::Name() const {
  13. cout << lastname << " , " << firstname;
  14. }

main.cpp

  1. #include <iostream>
  2. #include "module6_extends/tabtenn.h"
  3. using namespace std;
  4. void usett(){
  5. TableTennisPlayer player1("Chuck", "Blizzard", true);
  6. TableTennisPlayer player2("Tara", "Boomdea", false);
  7. player1.Name();
  8. if (player1.HasTable()) {
  9. cout << ":has a table.\n";
  10. } else{
  11. cout << ":has't a table.\n";
  12. }
  13. player2.Name();
  14. if (player2.HasTable()) {
  15. cout << ":has a table.\n";
  16. } else{
  17. cout << ":has't a table.\n";
  18. }
  19. }
  20. int main() {
  21. usett();
  22. return 0;
  23. }

使用派生类

tabtenn.h

  1. class RatedPlayer:public TableTennisPlayer{
  2. private:
  3. unsigned int rating;
  4. public:
  5. RatedPlayer(unsigned int r = 0, const char* fn = "none", const char* ln = "none", bool ht = false);
  6. RatedPlayer(unsigned int r, const TableTennisPlayer & tp);
  7. unsigned int Rating(){ return rating; }
  8. void ResetRating(unsigned int r){rating = r;}
  9. };

tabtenn.cpp

  1. RatedPlayer::RatedPlayer(unsigned int r, const char *fn, const char *ln, bool ht):TableTennisPlayer(fn, ln, ht){
  2. rating = r;
  3. }
  4. RatedPlayer::RatedPlayer(unsigned int r, const TableTennisPlayer &tp):TableTennisPlayer(tp), rating(r) {}

main.cpp

  1. #include <iostream>
  2. #include "module6_extends/tabtenn.h"
  3. using namespace std;
  4. void usett(){
  5. TableTennisPlayer player1("Chuck", "Blizzard", true);
  6. TableTennisPlayer player2("Tara", "Boomdea", false);
  7. RatedPlayer ratedPlayer(1140, "Mallory", "Duck", true);
  8. ratedPlayer.Name();
  9. if (ratedPlayer.HasTable()) {
  10. cout << ":has a table.\n";
  11. }else{
  12. cout << ":has't a table.\n";
  13. }
  14. player1.Name();
  15. if (player1.HasTable()) {
  16. cout << ":has a table.\n";
  17. } else{
  18. cout << ":has't a table.\n";
  19. }
  20. player2.Name();
  21. if (player2.HasTable()) {
  22. cout << ":has a table.\n";
  23. } else{
  24. cout << ":has't a table.\n";
  25. }
  26. cout << "Name: ";
  27. ratedPlayer.Name();
  28. cout << ":Rating: " << ratedPlayer.Rating() << endl;
  29. RatedPlayer ratedPlayer1(1221, player1);
  30. cout << "Name: ";
  31. ratedPlayer1.Name();
  32. cout << ":Rating :" << ratedPlayer1.Rating() << endl;
  33. }
  34. int main() {
  35. usett();
  36. return 0;
  37. }