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 03:20] – [Parent-Child Relationships] hhemler | notes:cprog:fall2021:projects:oop1 [2021/11/05 04:01] (current) – [References] hhemler | ||
---|---|---|---|
Line 27: | Line 27: | ||
<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. | ||
+ | |||
+ | A class with the keyword struct, and unions, will have public access by default, for their members and base classes. | ||
Inheriting from a base class using the __protected__ access specifier means both public 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 __protected__ access specifier means both public 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 __private__ access specifier means all members of the base class will become private members of the derived class. | Inheriting from a base class using the __private__ access specifier means all members of the base class will become private members of the derived class. | ||
+ | |||
+ | A class with the keyword class will have private access for its members and base classes by default. | ||
====Parent-Child Relationships==== | ====Parent-Child Relationships==== | ||
+ | The Child class is being referred to the class that inherits from another class, and the Parent class is being referred to the class that is being inherited from. | ||
+ | |||
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, | ||
- | Each Child belongs | + | Parent/Child inheritance chains are a beneficial way to add specifications to classes without changing the base class functions. |
+ | |||
+ | It’s also possible | ||
+ | ====A reflection on the concept of inheritance==== | ||
+ | (among other things) | ||
+ | |||
+ | It is easier | ||
+ | |||
+ | Something Interesting to note is that child class cannot access | ||
+ | |||
+ | 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 53: | Line 112: | ||
* https:// | * https:// | ||
* https:// | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
=====Submission===== | =====Submission===== | ||
I'll be looking for the following: | I'll be looking for the following: |