Using base pointers on DerivedClass class objects. : Base Class « Class « C++






Using base pointers on DerivedClass class objects.

Using base pointers on DerivedClass class objects.
  
// 
#include <iostream>
#include <cstring> 
using namespace std;

class BaseClass {
  char author[80];
public:
  void put_author(char *s) { 
     strcpy(author, s); 
  }
  void show_author() { 
     cout << author << endl; 
  }
} ;

class DerivedClass : public BaseClass {
  char title[80];
public:
  void put_title(char *num) {
    strcpy(title, num);
  }
  void show_title() {
    cout << "Title: ";
    cout <<  title << endl;
  }
};

int main()
{
  BaseClass *p;
  BaseClass baseObject;

  DerivedClass *dp;
  DerivedClass derivedObject;

  p = &baseObject;  

  p->put_author("Tom Clancy");

  p = &derivedObject;
  p->put_author("William Shakespeare");

  baseObject.show_author();
  derivedObject.show_author();
  cout << endl;

  dp = &derivedObject;
  dp->put_title("The Tempest");
  p->show_author(); 
  dp->show_title( );

  return 0;
}


           
         
    
  








Related examples in the same category

1.Base class and derived classBase class and derived class
2.Pass arguments to base classPass arguments to base class
3.Init member variables from base classInit member variables from base class
4.class to represent a book
5.Use Base keyword to call method in parent class from subclass