Download
1 / 14

Inheritance - PowerPoint PPT Presentation


  • 269 Views
  • Updated On :

Inheritance. ITK 169 Fall 2003. Base Class. Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec Center decided to keep track of those members who play table tennis. Consider the base class Player listed on the next slide.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Inheritance ' - Sophia


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Inheritance l.jpg

Inheritance

ITK 169

Fall 2003


Base class l.jpg
Base Class

  • Also called the Parent Class

  • This is the class that later classes will be build on.

  • Suppose the Red Bird Rec Center decided to keep track of those members who play table tennis.

  • Consider the base class Player listed on the next slide.


Slide4 l.jpg

class Player

{

private:

string fName,

lName;

bool hasTable;

public:

// default and special constructor in one using default values…

Player(const string& first = "empty", const string& last = "empty", bool table = false);

void set_fName(const string& first) { fName = first; }

void set_lName(const string& last) { lName = last; }

void set_hasTable(bool table) { hasTable = table; }

string get_fullnameLF()const { return (lName + ", " + fName); }

string get_fullnameFL()const { return (fName + " " + lName); }

bool get_hasTable()const { return hasTable; }

void printNameFL(ostream& outs) { outs << fName << " " << lName; }

friend ostream& operator <<(ostream& outs, const Player& aPlayer);

};


Derived class l.jpg
Derived Class

  • Some members have played in local table tennis tournaments.

  • They would like the class to include the point ratings they’ve earned.

  • Rather than start from scratch, you can derive a class from the Player class.

  • RatedPlayer will be the derived class.


Notice public player l.jpg
Notice - :public Player

class RatedPlayer : public Player

{

private:

unsigned int rating;

public:

// default and special constructor in one using default values…

RatedPlayer(unsigned int rt = 0, const string& first = "empty", const string& last = "empty", bool table = false);

// constructor to add a rating to an existing player

RatedPlayer(unsigned int rate, const Player& aPlayer);

void set_rating(unsigned int num) { rating = num; }

unsigned int get_rating() { return rating; }

};


Public derivation l.jpg
Public Derivation

  • In the class header : public Player is added to indicate that

    • RatedPlayer is derived from Player,

    • and that Player is a public base class.

  • With public derivation, all public members of the base class become public members of the derived class.

  • The private portions of the base class become part of the derived class but they can only be accessed through public and protected methods of the base class.

    • (More on protected methods later.)


Slide8 l.jpg
So…

  • An object of type RatedPlayer has in store:

    • 3 data members of Player

      • fName, lName; hasTable;

    • plus a rating.

  • An object of type RatedPlayercan use the methods of the base type and its own class.

  • An object of type Player has only 3 data members and can only access Player methods.


Derived requirements l.jpg
Derived Requirements

  • A derived class can add additional data members and member functions as needed.

  • A derived class needs its own constructors.

    • Note the constructors must provide data for the any new data member(s) AND the base data members.


Access considerations l.jpg
Access Considerations

  • A derived class does not have direct access to the private members of the base class.

  • It has to work through the base class methods.

  • In particular, the derived constructors have to use the base class constructors.


Derived constructors l.jpg
Derived Constructors

  • Each constructor includes a call to a Player constructor at the end of the parameter list - note the use of a :

    RatedPlayer::RatedPlayer(unsigned int rate, const string& first, const string& last, bool table) : Player(first, last, table)

    {

    rating = rate;

    }

    RatedPlayer::RatedPlayer(unsigned int rate, const Player& aPlayer) : Player(aPlayer), rating(rate) { }

This is call an initializer list. Note: We can have more than just the Player constructor call here.


Key points to remember l.jpg
Key Points to Remember

  • The base class object is constructed first.

  • The derived class constructor should pass base class information to a base class constructor via a member initialization list.

  • The derived class constructor should initialize those data members that were added to the derived class.


Using our 2 classes l.jpg
Using our 2 Classes

Player mickey("Mickey", "Mouse");

RatedPlayer daffy(10, "Daffy", "Duck");

cout << mickey << " plays occasionally for fun.\n";

cout << daffy << " is a serious tournament player.\n";

daffy.printNameFL(cout);

cout << " is rated " << daffy.get_rating( )

<< " out of 50, in the city tournaments.\n";


Private members l.jpg
Private Members

  • Why would a derived class NOT have direct access to private data members?

  • What about private functions?

  • Is there a “go around”?

  • Is it a good idea to allow direct access to private members (data or functions)?


ad