Using pointers on derived class objects. : Derived « Class « C++






Using pointers on derived class objects.

  
#include <iostream>  
#include <string.h>  
using namespace std;
class B_class {  
  char name[80];  
public:  
  void put_name(char *s) {strcpy(name, s); }  
  void show_name(void) {cout << name << " ";}  
} ;  
     
class D_class : public B_class {  
  char phone_num[80];  
public:  
  void put_phone(char *num) {  
    strcpy(phone_num, num);  
  }  
  void show_phone() {cout << phone_num << "\n";}  
};  
     
main(void)  
{  
  B_class *p;  
  B_class B_ob;  
     
  D_class *dp;  
  D_class D_ob;  
     
  p = &B_ob;
     
  p->put_name("Thomas Edison");  
     
  p = &D_ob;  
  p->put_name("Albert Einstein");  
     
  B_ob.show_name();  
  D_ob.show_name();  
  cout << "\n";  
     
  dp = &D_ob;  
  dp->put_phone("555 555-1234");  
  p->show_name();
  dp->show_phone();  
  return 0;  
}
  
    
  








Related examples in the same category

1.A base pointer to access derived objectsA base pointer to access derived objects
2.Demonstrate pointer to derived class.Demonstrate pointer to derived class.
3.Simple class with its derived classSimple class with its derived class
4.call constructor from parent for three level extending
5.how to pass arguments to the base classes of a derived class by modifying the preceding program