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.
#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;
}
Comments
Leave a comment