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

Don\'t just give the answer but explain your answer. a. What is the base class?

ID: 3536727 • Letter: D

Question

Don't just give the answer but explain your answer.

a. What is the base class? Why is it the base?
b. What is the derived class in this hierarchy? How can you tell?
c. What is the base constructor and how/when is it called?
d. What is the derived constructor and how/when is it called?
e. What is printed if you call the base version of show()?
f. What is printed if you call the derived version of show()?
g. What happens when you create a base object with an argument?
h. What happens when you create a derived object with an argument?
i. When you create a pointer, is the constructor called? Give your reason.
j. What does the notation -> mean? Is there a simpler way of writing that?

The following questions should be commented upon
starting from "Line 1" through "Line 15". Specify the line number first.
Explain what the "Line" in question is doing,
what will it print, if any, and the reasoning behind your answer.


#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
}


Explanation / Answer

a)The parent class of a derived class. Classes may be used to create other classes. A class that is used to create (or derive) another class is called the base cla
Line 1 is the base class.

b)In this hierarchy the class "Exempt" is the derrives class because it has been inherited publically form the base class "Employee"
Line 5 is the derived class.

c)Base constructor is a contructor of the base class which is in this case "Employee" class. Base contructor is called automatically when the object of either base class or object of derived class is created.
Line 1 will denote the base constuctor calling.

d)Derived constructor is a contructor of the derived class which is used to initialize data members of derived class. Derived constuctor will be called automatically when object of derived class will be created.
LINE 6 will denote the derived class contuctor calling.


e)If you call the base version of show() the statement {cout << "Name " << name << endl;} will be executed.
LINE 11,13 will denote calling of base version of show()

f)if you call the derived version of show() initially the base version of show() will be executed then derived version of show() will be executed.
thus execution will be in sequence:
cout << "Name " << name << endl;
cout << "Salary " << salary << endl;
LINE 12,14,15 will denote calling of derived version of show()


g)when you create a base object with an argument it will invoke the base constuctor : Employee(char *n) { strcpy( name, n); }
LINE 3 will create base object with an arguement.

h)when you create a derived object with an argument it will initially invoke base constructor will argument. Then it will invode derived consturctor.
Thus sequence will be:
Employee(char *n) { strcpy( name, n); }
{ salary = s;     }
LINE 6 will create derived object will arguements.

i)No, when you create pointer of a class it will not invoke the contructor.When are creating a pointer, you are NOT creating an actual object. If you don't initialize it, it will be pointing to garbage and dereferencing it will be an error.Remember that just because you make a pointer does not mean it is pointing to anything.
However if you make a copy contructor it will invoke the constuctor of a class.
LINE 4 will denote declaring simple pointer.
LINE 7,9,10 will invoke constuctor if needed.

j)the notation -> mean that it is pointing to some elment of the class. Yes there is a simpler way to avoid -->. Instead of --> you can use (*p1).
   for instance the following statments will result same
   (*p1).show();
   p1-->show();

   LINE 13 will simple the use of -->

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote