This is an old revision of the document!
Corning Community College
CSCS1320 C/C++ Programming
Collaborate in the creation of a useful, originally-voiced informational source on the various aspects of C++ programming, specifically inheritance, and the implementation of a simple program that has and uses a class with inheritance.
Additionally, the entire class will be participating in documenting and filling out this project page. It is the responsibility of EACH class member to:
Classes that inherit from another class acquire the attributes and behaviors of the class they inherit from.
Classes that inherit from another are called derived classes, child classes, or subclass. Classes that are inherited from are called base classes, parent classes, or superclasses.
To inherit traits from another class use a colon after the class's declaration, followed by the name of the base class (and optionally an access specifier).
class Derived : public Base
A class can inherit data or functions from multiple base classes. In order to do this, we would create a class derivation list, which names the base classes. The form would be the same as above, however, each class would be separated with a colon.
Problems can be created when a derived class inherits from more than one base class. An example of this is when two or more base classes have a member with the same name. The problem here is the derived class cannot distinguish between the two members.
If you have a function expecting an argument of a base class's type, you can pass objects of a type that derived from said base class.
For example:
#include <cstdio> class Vehicle { public: float gas{ 15.53f }; }; class Car : public Vehicle { public: unsigned short tires{ 4 }; }; void print_gas(const Vehicle& vehicle) { std::printf("The vehicle has %.2f gallons of gas left\n", vehicle.gas); }
You are able to pass an object of type Car to print_gas.
Car myCar{}; print_gas(myCar);
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 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.
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. 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.
Parent/Child inheritance chains are a beneficial way to add specifications to classes without changing the base class functions. An example of this may be Animals→Dogs: An animal would have a size, age, living/death status, which are all properties that a dog could inherit, however, a dog could have it's own functions like fur length, temperament, so on and so forth. This could be extended by creating a dog breed class.
Each Child belongs to one Parent, and the Child needs to know the Parent to which it belongs. The construction of the Parent logically includes the construction of the Child.
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:
Be sure to make use of and follow proper conventions having public, protected, and private access control (no cheating by making everything public), and demonstrate inheritance.
Have a main() function that instantiates an instance of your chosen theme of class with inheritance, and runs that instantiated object through a range of tests demonstrating the class works as intended (likely also prompting the user to input any needed values to configure the attributes of the object).
I'll be looking for the following:
104:oop1:final tally of results (104/104) *:oop1:no compiler messages, program compiles and runs without issue [26/26] *:oop1:specified functionality is implemented [26/26] *:oop1:project page contributions as per project specifications [52/52]
Additionally: