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

C++ For the questions below, assume that initially base class has only an intege

ID: 657386 • Letter: C

Question

C++

For the questions below, assume that initially base class has only an integer x as a private data member, and the derived class has only an integer y as a private data member. You need to provide the class definition and any function member needed i.e. set/get for each data member, constructor, destructor, etc., as well as the appropriate access modifier i.e. public, private, protected, static, virtual, etc.

a) Prepare and include the testing scenarios (i.e. examples that you used to test your code) in your main function. i.e.

int main(){

//TASK 6: This code shows parametrized constructor call of the derived class derivedClass *m = new derivedClass( 14 ); baseClass *n = new baseClass( 56 ); . . .

return 0;

}

6) Complete the following:

a) Write the base and derived class definition, data members, including mutators and accessors

b) Modify the base and derived classes in such a way that the statement:

derivedClass *m = new derivedClass( 14 ); will produce the output

This is class 0

This is class 14

and the statement baseClass *n = new baseClass( 56 );

will produce the output:

This is class 56

c) Based on part b) write the functions such that, m->print() will display 14, and n->print() will display 56

Add an static data member count and increment it in such a way that when calling getCounter() after 6.b) the function will print 2. Note: the count variable should increase when either a new instance of the base or the derived class is created.

Explanation / Answer

#include<iostream>

using namespace std;
static int counter=0;   //declare global static counter to store no. of objects created

class baseClass{
    private:
        int x;
    public:
        baseClass(){    //no argument base class constructor
            x=0;
            cout<<" This is class "<<this->x; //print value of x
        }
        baseClass(int x){   //parameterized base class constructor
            this->x=x;
            cout<<" This is class "<<this->x; //print value of x
            extern int counter; //tell the compiler to use the global variable counter within the scope of base class
            counter++; //increment counter
        }
        int getX(){
            return x;   //return the value of
        }
        void setX(int x){
            this->x=x; //set the value of present object's x to the value of parameter x
        }
        void print(){
            cout<<endl<<x; //print x
        }
};

class derivedClass : public baseClass{
    private:
        int y;
    public:
        derivedClass(){ //no argument derived class constructor
            y=0;
        }
        derivedClass(int y){    //parameterized derived class constructor
            this->y=y; //set the value of present object's y to the value of parameter y
            cout<<" This is class "<<this->y; //print y
            counter++; //increment counter
        }
        int getY(){
            return y;   //return the value of y
        }
        void setY(int y){
            this->y=y; //set the value of present object's y to the value of parameter y
        }
        void print(){
            cout<<endl<<y; //print y
        }
};

void getCounter() {
    cout<<" Counter:"<<counter;    //print no of objects created
}

int main(){
    derivedClass *m = new derivedClass( 14 );
    baseClass *n = new baseClass( 56 );
    getCounter();
    m->print();
    n->print();
}

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