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/polymorphism.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
47 lines (41 sloc)
1021 Bytes
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; | |
/* | |
Polymorphism. Uses existing functions to do different stuff. | |
Like a magpie, copies but this time changes very slightly. | |
*/ | |
class Parent { // defining class Parent | |
public: // public access functions | |
void say() { // creating it's own say function | |
cout << "Parent!" << endl; | |
} | |
}; | |
class Child : public Parent { | |
public: | |
void say() { // creating it's own say function | |
cout << "Child from Parent!" << endl; | |
} | |
}; | |
class Childs_Child : public Child { | |
public: | |
void say() { // creating it's own say function | |
cout << "Child's Child from Child!" << endl; | |
} | |
}; | |
int main() { | |
/* | |
Defining all objects per each class made above | |
*/ | |
Parent p; | |
Child c; | |
Childs_Child cc; | |
/* | |
Calling the say function from each class. | |
Note how it does not replace the function body. | |
*/ | |
p.say(); | |
c.say(); | |
cc.say(); | |
// return 0 | |
return 0; | |
} |