Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
4003CEM-Exam-Practise/Classes and Inheritance/inheritance.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
39 lines (34 sloc)
1.14 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
/* | |
Inheritance. Like you do it with family. | |
Somewhat the same, unless we in a simulation... | |
*/ | |
class Dog { // defining class called Dog | |
public: // adding public variables/funcs | |
int x = 10; // public func assigned to 10 | |
void bark() { | |
cout << "Bark!" << endl; // prints "Bark" when called | |
} | |
}; | |
class Animals : public Dog { // defining class called Animals which inherits from the Dog class with public access | |
public: // adding the public func | |
void is_animal() { | |
cout << "yes" << endl; // simple func prints "yes" when called | |
} | |
}; | |
int main() { | |
// Defining animals object from class | |
Animals animals; | |
// Calling .is_animal() from class Animals | |
animals.is_animal(); | |
// Calling .bark() from class Dog which has been inherited | |
animals.bark(); | |
// Returning value of animals.x from class Dog which has been inherited | |
cout << animals.x << endl; | |
// Incrementing animals.x by + 1 | |
animals.x++; | |
// Returning new value of animals.x which has been incremented by + 1 now | |
cout << animals.x << endl; | |
return 0; | |
} |