How to Learn C++ Inheritance Quickly
Learning inheritance in C++ requires understanding fundamental concepts. Inheritance occurs in all object-oriented programming languages, including C++. The easiest way to learn inheritance is through a basic example. Assume that there is a C++ class that resembles a person. The person class consists of data and operations that relate to a person who works in a large corporation. Write a new employee class that extends from the person class.
Instructions
-
-
1
Write the parent class that the new class will inherit from. This class may already be implemented. If it is, then review all of the member functions in this class.
-
2
Implement the employee class. Define the inheritance from the person class in your employee.h file. This definition should look like the following:
class Employee: Person {
};
-
-
3
Add additional member functions and any private data to your employee class. With inheritance, you don't have to implement any data and functions from the person class, just implement additional functions for the employee class.
-
4
Test your employee class. Be certain that all of the new functions that you wrote are adequately tested. This is the best opportunity to find critical errors in your software.
-
5
Integrate the employee class into the entire program. This step should be easy if you fully tested the new employee class.
-
1
Tips & Warnings
To get any C++ program to work on a different operating system, you must recompile your source code specifically for that platform.