Facebook
From asd, 5 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 154
  1. #include <iostream>
  2. using namespace std;
  3. class Car {
  4.     public: string name,color="White";
  5.     Car() {
  6.         cout << "Car class\n";
  7.     }
  8.     void displayColor() {
  9.         cout << color << endl;
  10.     }
  11. };
  12. class Volkswagen: public Car {
  13.  
  14.     public: string color;
  15.     Volkswagen(string colorName) {
  16.         color = colorName;
  17.     }
  18.     void displayColor() {
  19.         cout << "The color is Volkswagen is: " << color << endl;
  20.     };
  21. };
  22. class Mercedes: public Car {
  23.     public: string color;
  24.     Mercedes(string colorName) {
  25.         color = colorName;
  26.     }
  27.     void displayColor() {
  28.         cout << "The color is Mercedes is: " << color << endl;
  29.     }
  30. };
  31. int main () {
  32.     Car c;
  33.     c.displayColor();
  34.     Volkswagen v = Volkswagen("Red");
  35.     Car * ptr1 = & v;
  36.     v.displayColor();
  37.     v.Car::displayColor();
  38.     ptr1 -> displayColor();
  39.     Mercedes m = Mercedes("Black");
  40.     Car * ptr2 = & m;
  41.     m.displayColor();
  42.     m.Car::displayColor();
  43.     ptr2 -> displayColor();
  44.     return 0;
  45. } //page180-181