The ability to have a function/method to do different thinkgs depending on the involved data types
Two or more functions with the same name but with different types and number of arguments
Programming feature through which algorithms and data types are written in terms of types (parameters) to be specified later
Two stages:
In C++, it needs to be completely on a header file.
There are many generic data types in C++:
std::vector<T>
std::list<T>
std::unique_ptr<T>
std::shared_ptr<T>
Relationship between two classes that enables one of them (superclass) to represent another (subclass)
|
|
SubC
inherits superattribute
and supermethod()
private:
Cannot access except from within the classpublic:
Can access from everywhereprivate:
Cannot access except from within the classprotected:
Can access from within the class and its subclassespublic:
Can access from everywhereA pointer / reference to a superclass can represent any of its subclasses
void functionc(SuperC* c) {
...
}
int main() {
SuperC superc;
SubC subc;
functionc(&superc);
functionc(&subc); //This is also legal
}
functionc
works with both types
|
|
void functionc(SuperC* c) {
c->polymethod();
}
functionc
calls either
SuperC::polymethod
or SubC::polymethod
|
|
|
|
SuperC::polymethod
is not implemented.
functionc
calls either
SubC1::polymethod
or SubC2::polymethod
shape.h
|
fence.h
fence.cpp
|
circle.h
circle.cpp
|
rectangle.h
rectangle.cpp
|
main.cpp