Private Versus Protected

You may have noticed that a new access keyword, protected, has been introduced on lines 23 and 43 of Listing 16.1. Previously, class data had been declared private. However, private members are not available to derived classes. You could make itsAge and itsWeight public, but that is not desirable. You don't want other classes accessing these data members directly.

What you want is a designation that says, “Make these visible to this class and to classes that derive from this class.” That designation is protected. Protected data members and functions are fully visible to derived classes, but are otherwise private.

There are, in total, three access specifiers: public, protected, and private. If a function has an instance of a class, it can access all of the public member data and functions of that class. The member functions of a class, however, can access all the private data members and functions of any class from which they derive.

Therefore, the function Dog::WagTail() can access the private data itsBreed and can access the protected data in the Mammal class.

Even if other classes are layered between Mammal and Dog (for example, DomesticAnimals), the Dog class will still be able to access the protected members of Mammal, assuming that these other classes all use public inheritance.

Listing 16.2 demonstrates how to create objects of type Dog and access the data and functions of that type.

Listing 16.2. Using a Derived Object
 0:  //Listing 16.2 Using a derived object
 1:  #include <iostream>
 2:
 3:  enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };
 4:
 5:  class Mammal
 6:  {
 7:  public:
 8:      // constructors
 9:      Mammal():itsAge(2), itsWeight(5){ }
10:      ~Mammal(){}
11:
12:      //accessors
13:      int GetAge()const { return itsAge; }
14:      void SetAge(int age) { itsAge = age; }
15:      int GetWeight() const { return itsWeight; }
16:      void SetWeight(int weight) { itsWeight = weight; }
17:
18:      //Other methods
19:      void Speak()const { std::cout << "Mammal sound!
"; }
20:      void Sleep()const { std::cout << "shhh. I'm sleeping.
"; }
21:
22:
23:  protected:
24:      int itsAge;
25:      int itsWeight;
26:  };
27:
28:  class Dog : public Mammal
29:  {
30:  public:
31:      // Constructors
32:      Dog():itsBreed(YORKIE){}
33:      ~Dog(){}
34:
35:      // Accessors
36:      BREED GetBreed() const { return itsBreed; }
37:      void SetBreed(BREED breed) { itsBreed = breed; }
38:
39:      // Other methods
40:      void WagTail() { std::cout << "Tail wagging...
"; }
41:      void BegForFood() { std::cout << "Begging for food...
"; }
42:
43:  private:
44:      BREED itsBreed;
45:  };
46:
47:  int main()
48:  {
49:      Dog fido;
50:      fido.Speak();
51:      fido.WagTail();
52:      std::cout << "Fido is " << fido.GetAge() << " years old
";
53:      return 0;
54:  }


Mammal sound!
Tail wagging...
Fido is 2 years old

On lines 5–26, the Mammal class is declared (all its functions are inline to save space here). On lines 28–45, the Dog class is declared as a derived class of Mammal. Thus, by these declarations, all Dogs have an age, a weight, and a breed.


On line 49 a Dog is declared, Fido. Fido inherits all the attributes of a Mammal, as well as all the attributes of a Dog. Thus Fido knows how to WagTail(), but also knows how to Speak() and Sleep().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.198.81