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_int;
void display() {
cout << "base int: " << base_int << endl;
}
};
class derived_class: public base_class {
public:
int derived_int;
void display() {
cout << "base int: " << base_int << endl;
cout << "derived int: " << derived_int << endl;
}
};
int main()
{
base_class base_object;
base_class *base_ptr = &base_object;
base_ptr->base_int = 123;
base_ptr->display();
derived_class *derived_ptr = &base_object;
derived_class->base_int = 234;
derived_class->derived_int = 567;
derived_class->display();
return 0;
}
Comments
Leave a comment