For the questions below, assume that initially baseClass has only an integer x a
ID: 3695199 • Letter: F
Question
For the questions below, assume that initially baseClass 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 he appropriate access modifier i.e. public, private, protected, static, virtual, friend, etc.
3) Complete the following:
a) (1 point) Write the base and derived class definition as well as mutators and accessors
b) (2 point) Modify the base and derived classes in such a way that the statement: derivedClass *m = new derivedClass( 56, 14 ); will produce the output This is class 56 This is class 14 and the statement baseClass *n = new baseClass( 79 ); will produce the output: This is class 79
c) (2 point) Based on part b) modify the classes and write the functions such that, m- >print() will display 14, and n->print() will display 79
4) (3 points) Add an static data member count, write the initialization and increment it in such a way that when calling getCounter() after the statements in b) are executed, 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. Write the code needed for the function getCounter().
5) (2 points) Write a function printParentValue() in the derived class that will access directly the value of x, and will print it on the screen; and a function printChildValue() which is not a member function of either base or derive class, but can access y directly and display its value on the screen.
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
// base class
class baseClass{
// protected members, so that it can be accessed by child class directtly
protected:
int x;
public:
static int count; // static member to keep count of number of objects
// constructor
baseClass(int x){
this->x = x;
cout<<"This is class "<<x<<endl;
count++;
}
int getCount(){
return count;
}
void print(){
cout<<x<<endl;
}
// getter and setter
void setX(int x){
this->x = x;
}
int getX(){
return x;
}
};
// initializing static count variable
int baseClass::count = 0;
// derived class
class derivedClass: public baseClass{
private:
int y;
public:
derivedClass(int x, int y):baseClass(x){
this->y = y;
cout<<"This is class "<<y<<endl;
}
void print(){
cout<<y<<endl;
}
void setY(int y){
this->y = y;
}
int getY(){
return y;
}
// printing x directly
void printParentValue(){
cout<<x<<endl;
}
// friend function
friend void printChildValue(derivedClass* d);
};
// defination of friend function
void printChildValue(derivedClass* d){
cout<<d->y<<endl;
}
int main(){
derivedClass *m = new derivedClass( 56, 14 );
baseClass *n = new baseClass( 79 );
m->print();
n->print();
cout<<"Count value: "<<m->getCount()<<endl;
printChildValue(m);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.