Answer to Question #240609 in C++ for Ram

Question #240609

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-22T06:28:35-0400
#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;
}

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