Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ inheritance

#include <iostream>
 
// Base class
class Animal {
public:
  std::string gender;
  int age;
};
 
// Derived class
class Dog: public Animal  {
public:
  std::string breed;
 
  void sound() {
    std::cout << "Woof
";
  }
};
 
int main() {
  Dog buddy;
  buddy.gender = "male";
  buddy.age = 8;
  buddy.breed = "husky";
 
  buddy.sound(); // Outputs: Woof
 
  return 0;
}
Source by www.codecademy.com #
 
PREVIOUS NEXT
Tagged: #inheritance
ADD COMMENT
Topic
Name
6+4 =