This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:cprog:fall2021:projects:oop1 [2021/11/03 22:14] – [References] toverhi1 | notes:cprog:fall2021:projects:oop1 [2021/11/05 04:01] (current) – [References] hhemler | ||
---|---|---|---|
Line 24: | Line 24: | ||
Classes that are inherited from are called __base classes__, __parent classes__, or __superclasses__. | Classes that are inherited from are called __base classes__, __parent classes__, or __superclasses__. | ||
- | A class can inherit data or functions from multiple base classes. | ||
- | | ||
- | class derived class: access-specifier base class | ||
- | | ||
To inherit traits from another class use a colon after the class' | To inherit traits from another class use a colon after the class' | ||
<code cpp> | <code cpp> | ||
+ | |||
+ | A class can inherit data or functions from multiple base classes. | ||
+ | |||
+ | Problems can be created when a derived class inherits from more than one base class. | ||
+ | |||
+ | If you have a function expecting an argument of a base class' | ||
+ | |||
+ | For example: | ||
+ | <code cpp> | ||
+ | #include < | ||
+ | |||
+ | class Vehicle | ||
+ | { | ||
+ | public: | ||
+ | float gas{ 15.53f }; | ||
+ | }; | ||
+ | |||
+ | class Car : public Vehicle | ||
+ | { | ||
+ | public: | ||
+ | unsigned short tires{ 4 }; | ||
+ | }; | ||
+ | |||
+ | void print_gas(const Vehicle& | ||
+ | { | ||
+ | std:: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | You are able to pass an object of type Car to print_gas. | ||
+ | |||
+ | <code cpp> | ||
+ | Car myCar{}; | ||
+ | print_gas(myCar); | ||
+ | </ | ||
====Access Control implications==== | ====Access Control implications==== | ||
Inheriting from a base class using the __public__ access specifier means public members of the base class will become public members of the derived class, and protected members of the base class will become protected members of the derived class. Private members of the base class will still be private. | Inheriting from a base class using the __public__ access specifier means public members of the base class will become public members of the derived class, and protected members of the base class will become protected members of the derived class. Private members of the base class will still be private. | ||
Line 47: | Line 78: | ||
Since the Child simply inherits the properties of the Parent, one Parent can have multiple Children. | Since the Child simply inherits the properties of the Parent, one Parent can have multiple Children. | ||
Two Children of the same Parent will also have no direct relationship. | Two Children of the same Parent will also have no direct relationship. | ||
- | Children can themselves have Children that inherit from them, creating large inheritance chains. | + | Children can themselves have Children that inherit from them, creating large inheritance chains. |
+ | Children can also multi-inherit, | ||
Parent/ | Parent/ | ||
- | Each Child belongs | + | It’s also possible |
+ | ====A reflection on the concept of inheritance==== | ||
+ | (among other things) | ||
+ | |||
+ | It is easier to understand inheritance than it is most concepts in programming, because inheritance follows so closely to where it gets its name from. If you asked a non-computer programmer what a string is it would likely make them think of a string of fabric, whereas if you asked them what inheritance is they would probably think of a concept | ||
+ | |||
+ | Something Interesting | ||
+ | |||
+ | When something has multiple parents it is called multiple inheritance. When there is more than one tier of inheritance (grandparents) it is called multilevel inheritance. When something has multiple children it is called hierarchical inheritance. When multiple of these occur it is called hybrid inheritance. | ||
+ | |||
+ | Inheritance allows us to reuse classes by having other classes inherit their members. | ||
+ | |||
+ | It is possible to make a class non-subclassable (sterile) in C++ with the " | ||
+ | |||
+ | COOL BUG FACT: Inheritance was first created for the Simula programming languages | ||
=====Program===== | =====Program===== | ||
Write a program that creates a parent-child class structure. It can be anything, so long as you genuinely implement it and it works. A few possible examples for inspiration: | Write a program that creates a parent-child class structure. It can be anything, so long as you genuinely implement it and it works. A few possible examples for inspiration: | ||
Line 66: | Line 112: | ||
* https:// | * https:// | ||
* https:// | * https:// | ||
- | * https:// | ||
* https:// | * https:// | ||
* https:// | * https:// | ||
* https:// | * https:// | ||
- | * https:// | ||
=====Submission===== | =====Submission===== | ||
I'll be looking for the following: | I'll be looking for the following: |