Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <string> using namespace std; class Employee { priv

ID: 3536588 • Letter: #

Question

#include <iostream>

#include <string>

using namespace std;

class Employee

{

private: char name[20];

public: Employee( )    {strcpy(name, "(no name)"); }

          Employee(char *n) { strcpy( name, n); }

   void show( )       { cout << "Name " << name << endl; }

   ~Employee( )       { cout << "Employee destructor "; }

};

class Exempt : public Employee

{

private: float salary;

public: Exempt( )          { salary 10000.0; }

          Exempt(char *n, float s) : Employee(n)

        { salary = s;     }

   void show( )       { Employee::show( );

          cout << "Salary " << salary << endl;

        }

   ~Exempt( )         { cout << "Exempt Dtor " ; }

};

void main()

{

Employee e;             // LINE 1

e.show();               // LINE 2

Employee e1("Jackson"); // LINE 3

Employee *p1, *p2;      // LINE 4

Exempt staff1;          // LINE 5

Exempt x1("Alexander", 30000.0); // LINE 6

Exempt *px = &x1;       // LINE 7

Employee e2 = e1;       // LINE 8

p1 = &e2;               // LINE 9

p2 = &x1;               // LINE 10

e1.show();              // LINE 11

staff1.show();          // LINE 12

(*p1).show();           // LINE 13

p2 -> show();           // LINE 14

px -> show();           // LINE 15

}

What is the base constructor and how/when is it called?

Explanation / Answer

These are the base constructors:


Employee( ) {strcpy(name, "(no name)"); }

Employee(char *n) { strcpy( name, n); }


They are called whenever an variable of type Employee is intantiated.

Since class Exempt is a derived class of Employee, Employee's constructor is also called by Exempt's constructor.

These lines call the constructors:


Employee e; // LINE 1

Employee e1("Jackson"); // LINE 3

Exempt staff1; // LINE 5

Exempt x1("Alexander", 30000.0); // LINE 6

Employee e2 = e1; // LINE 8