Answer to Question #240510 in C++ for Aman

Question #240510

Write a program to assign the values to base class members at the time of creation of derived class object. The base class members are private members. Display the values of both base and derived class using function overriding concept.


1
Expert's answer
2021-09-22T03:48:48-0400


#include <iostream>
using namespace std;


class base_class {
public:
    int base;
 
    
    void display()
    {
        cout << "Outputting the variable of the base class: " << base << endl;
    }
};
 


class derived_class : public base_class {
public:
    int derived;
 
    
    void display()
    {
        cout << "Outputting the variable of the base class:  " << base << endl;
        cout << "Outputting the variable of the derived class : " << derived << endl;
    }
};
 


int main()
{
   
    base_class* base_ptr;
    base_class obj_base;
    derived_class object;
 
    
    base_ptr = &object;
 
    base_ptr->base = 134;
 
    
    base_ptr->display();
 
    base_ptr->base = 13400;
    base_ptr->display();
 
    derived_class* derived_ptr;
    derived_ptr = &object;
    derived_ptr->base = 9448;
    derived_ptr->derived = 98;
    derived_ptr->display();
 
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS