Example of inheritance in C++ (part 2)

Written by Hamza on. Posted in C++

We continue on with our example of inheritance in C++. Now we look at the codes of the rest of the classes, namely the scientists, managers and technicians classes, and we will subdivide the scientists class into a further two sub-classes. I want you to pay particular attention to the syntax used for inheritance and the logical flow of the program. Keep in mind the main parent class from part 1 of this example. The scientist class will be derived from the CryptEmployee class and will have all the functions that are relevant to the scientists category.

#include “cryptoemployee.h”
class scientist : public cryptoemployee{

char *key;
int taxrate;
char* get_key();
void generate_key();
void enteremployeedata();
void displayemployeedata();
int get_taxrate();


As you can see, the enteremployeedata() and displayemployeedata() functions are implemented again. The reason for this will be evident when I explain these codes. The technicians and the manager classes will be based along the same lines and will contain the different data fields and functions that are relevant to their jobs. Thus, for example, a manager may have a field called ‘department’ signifying which department he belongs to. I will not be explaining in detail the code of each of these classes as they should be easy enough for those following my articles closely!

Rather, as I want to focus on inheritance more, I will move on to the two subcategories of the scientists class: The securityanalyst and the cryptanalyst. Let us focus on the securityanalyst class.

#include “scientist.h”
class securityanalyst : public scientist{
int diagnostics;
securityanalyst(const securityanalyst&);
int get_diagnostics();
void set_diagnostics(int);
void enteremployeedata();
void displayemployeedata();


Again the two basic functions are repeated.

Trackback from your site.

Leave a comment