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

The class derivedCL inherits base class baseCL , using public inheritance. Both

ID: 3580069 • Letter: T

Question

The class derivedCL inherits base class baseCL, using public inheritance. Both classes have a version of the function f(). for simplicity, all functions are implemented inline (i.e., code for all member functions have been provided). The interfaces of two classes are provided below:

class baseCL

{

public:

    baseCL(int x = 1) : baseX(x), baseY(4)

    {}

    int f()

    { return baseX + baseY;}

private:

    int baseX;

protected:

    int baseY;

};

class derivedCL : public baseCL

{

public:

    derivedCL() : x(10), y(11), baseCL(5)

    {}

    int f()

    {

       y = baseCL :: baseY * 2;

       x = 3;

       return x * y;

    }

private:

    int x, y;

};

Assume the given declarations for objects base1Obj, base2Obj, and derivedObj in the main function with base1Obj initialized to 2.

Predict the output for the following statements. If a statement is not valid, indicates that as the output.

       cout << base1Obj.f() << endl;              // output = ___________________________

       cout << base2Obj.f() << endl;              // output = ___________________________

       cout << derivedObj.f() << endl;            // output = ___________________________

       cout << derivedObj.baseCL::f() << endl;    // output = ___________________________

       base2Obj = derivedObj;

       cout << base2Obj.f() << endl << endl;      // output = ___________________________

cout << base2Obj.derivedCL.f() << endl;    // output = ___________________________

Implement all member functions outside the scope of the class then write the main function to test all member functions. Display the output and compare the results with those obtained in part (a).

Remove “protected” from baseCL class and run the program again. Does the program compile? If not, what is the error message?

What happens when “public” inheritance (in the header of the derived class) in replaced by “private” or “protected” inheritance?

P10.2 When XYZ Company started two years ago, the below payroll program was written to handle the payrolls of only salaried and hourly employees. As business expands, the company finds the need to hire sales people. For each pay period, a sales person‘s pay includes a base pay of $500.00 plus commission, which is five percent of the total sales generated during the pay period. Since the original program is written in C++, an OOP language that supports inheritance, the existing program can be easily extended to meet the new business need of the company without much change to the original program. Write the extended version (i.e., created a new derived classes named salesEmployee that contains data members basePay, totalSales, and commissionRate; it also contains an additional member functions that compute and display the pay payroll). Write a main function to generate the payrolls of all three types of employees. F your convenience, the code for the original program that handles only salariedEmployee and hourlyEmployee is provided below:

class Employee

    {

    public:

        Employee( );

        Employee(string the_name, string the_ssn);

        string get_name( ) const;

        string get_ssn( ) const;

        double get_net_pay( ) const;

        void set_name(string new_name);

        void set_ssn(string new_ssn);

        void set_net_pay(double new_net_pay);

        void print_check( ) const;

    private:

        string name;

        string ssn;

        double net_pay;

    };

// Implementation for the Base Class Employee

Employee::Employee( ) : name("No name yet"), ssn("No number yet"), net_pay(0)

    {

        //deliberately empty

    }

Employee::Employee(string the_name, string the_number)

       : name(the_name), ssn(the_number), net_pay(0)

    {

        //deliberately empty

    }

string Employee::get_name( ) const

    {

        return name;

    }

string Employee::get_ssn( ) const

    {

        return ssn;

    }

double Employee::get_net_pay( ) const

    {

        return net_pay;

    }

void Employee::set_name(string new_name)

    {

        name = new_name;

    }

  

void Employee::set_ssn(string new_ssn)

    {

        ssn = new_ssn;

    }

void Employee::set_net_pay (double new_net_pay)

    {

        net_pay = new_net_pay;

    }

void Employee::print_check( ) const

    {

        cout << " ERROR: print_check FUNCTION CALLED FOR AN "

             << "UNDIFFERENTIATED EMPLOYEE. Aborting the program. "

             << "Check with the author of the program about this bug. ";

        exit(1);

    }

//}

// Interface for the Derived Class HourlyEmployee

class HourlyEmployee : public Employee

    {

    public:

        HourlyEmployee( );

        HourlyEmployee(string the_name, string the_ssn,

                           double the_wage_rate, double the_hours);

        void set_rate(double new_wage_rate);

        double get_rate( ) const;

        void set_hours(double hours_worked);

        double get_hours( ) const;

        void print_check( ) ;

    private:

        double wage_rate;

        double hours;

    };

// Implementation for the Derived Class HourlyEmployee

HourlyEmployee::HourlyEmployee( ) : Employee( ), wage_rate(0), hours(0)

    {

        //deliberately empty

    }

HourlyEmployee::HourlyEmployee(string the_name, string the_number,

                                   double the_wage_rate, double the_hours)

    : Employee(the_name, the_number), wage_rate(the_wage_rate), hours(the_hours)

    {

        //deliberately empty

    }

void HourlyEmployee::set_rate(double new_wage_rate)

    {

        wage_rate = new_wage_rate;

    }

double HourlyEmployee::get_rate( ) const

    {

        return wage_rate;

    }

void HourlyEmployee::set_hours(double hours_worked)

    {

        hours = hours_worked;

    }

double HourlyEmployee::get_hours( ) const

    {

        return hours;

    }

void HourlyEmployee::print_check( )

    {

        set_net_pay(hours * wage_rate);

        cout << " ________________________________________________ ";

        cout << "Pay to the order of " << get_name( ) << endl;

        cout << "The sum of " << get_net_pay( ) << " Dollars ";

        cout << "________________________________________________ ";

        cout << "Check Stub: NOT NEGOTIABLE ";

        cout << "Employee Number: " << get_ssn( ) << endl;

        cout << "Hourly Employee. Hours worked: " << hours

             << " Rate: " << wage_rate << " Pay: " << get_net_pay( ) << endl;

        cout << "_________________________________________________ ";

    }

// Interface for the Derived Class SalariedEmployee

class SalariedEmployee : public Employee

    {

    public:

        SalariedEmployee( );

        SalariedEmployee (string the_name, string the_ssn,

                                  double the_weekly_salary);

        double get_salary( ) const;

        void set_salary(double new_salary);

        void print_check( );

    private:

        double salary;//weekly

    };

// Implementation for the Derived Class SalariedEmployee

SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)

    {

        //deliberately empty

    }

SalariedEmployee::SalariedEmployee(string the_name, string the_number,

                                  double the_weekly_salary)

                     : Employee(the_name, the_number), salary(the_weekly_salary)

    {

        //deliberately empty

    }

double SalariedEmployee::get_salary( ) const

    {

        return salary;

    }

void SalariedEmployee::set_salary(double new_salary)

    {

        salary = new_salary;

    }

void SalariedEmployee::print_check( )

    {

        set_net_pay(salary);

        cout << " __________________________________________________ ";

        cout << "Pay to the order of " << get_name( ) << endl;

        cout << "The sum of " << get_net_pay( ) << " Dollars ";

        cout << "_________________________________________________ ";

        cout << "Check Stub NOT NEGOTIABLE ";

        cout << "Employee Number: " << get_ssn( ) << endl;

        cout << "Salaried Employee. Regular Pay: "

             << salary << endl;

        cout << "_________________________________________________ ";

    }

Explanation / Answer

Outputs:

base1Obj.f() -- 5

base2Obj.f() -- 5

derivedObj.f() -- 24

derivedObj.baseCL::f() -- 9

base2Obj.f() -- 9

cout << base2Obj.derivedCL.f() << endl; -- WRONG

After removing protected it gives error- 'int baseCL::baseY' is private

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