• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Classes » Pointer to C++ classes
Next →
← Prev

Pointer to C++ classes

By Dinesh Thakur

Virtual functions play an important role in object oriented programming. Ordinary functions use the concept of early binding. However virtual functions help in late binding. The concept of pointers playa vital role in Virtual functions.

 Pointer to a class

Consider a class Person

class Person {   
   member data   
   void input() // member function {}
};

A pointer to the class Person is defined as follows:

Person *o1;
o1->input();

o1 is a pointer to the class Person and the second statement is used for executing the member function input(). Now instead of a dot operator, -> symbol is used. Consider an example in which Teacher is a derived class of the base class Person and has a member function of the same name in both classes as shown below.

class Person {
   member data
   void input() // member function {}
};
class Teacher: public Person {
     member data
     void input() // member function {}
};

and the main function contains the following statements

Person *o1;
ol=&Teacher;
ol->input();

Although o1 has been given the address of Teacher, the compiler will always invoke the member function of the base class (Person class). This is because binding takes place at the time of compilation when the contents of the pointer are not known. This is known as early binding.

Following is a program to illustrate the anomalies which occur due to early binding.

 Example

consumer is a base class having member data name, code and telephone number( tel) and member functions getdata() and display(). transaction is a derived class having member data quantity( qty), price and total and member functions with the same names as the base class.

#include
#include 
const int size=10;
class consumer {
  protected:               
    char name[size];                
    int code;               
    char tel[size];
  public:           
    consumer() {            
    strcpy(name," ");               
    code=0;                
    strcpy(tel," ");
  }
  void getdata() {
     cout<<" enter name";  
     cin>>name;
     cout<<"enter code "; 
     cin>>code;   
     cout<<"enter telephone number";      
     cin>>tel;
   }
   void display() {
      cout<<"\nname "<<name<<endl;   
      cout<<" code" <<code<<endl;      
      cout<<"telephone "<<tel<<endl;
} 
};
class transaction: public consumer {    
       protected:
            int qty;
            float price;
            float total;
       public:           
       transaction() {    
            qty=0;      
            price=0;          
            total=0;       
       }         
       void getdata() {
            consumer::getdata();         
            cout<<"enter quantity ";cin>>qty;          
            cout<<"enter price ";cin>>price;
       }
       void display()  {
            consumer::display();                  
            cout<<"quantity "<<qty<<endl;                 
            cout<<"price "<<price<<endl;                         
            cout<<"\ntotal price "<<qty*price<<endl;             
       }
};
 void main( ) {   
      consumer* o1;    
      transaction o2;       
      o1=&o2;        
      o1->getdata();          
      o1->display();
}

 o1 is a pointer to the base class customer. o2 is a derived class transaction object and its address is passed on to pointer o1. When the overriding member functions getdata() and display() are invoked by the object (of pointer type) o1, the base class functions are invoked and not the derived class functions.

On the execution of the above program, the following screen is displayed and the data is entered.

enter name Sharma

enter code o1

enter telephone number 678234

Output 

name Sharma

code 1

telephone 678234

In order to implement late binding, the concept of virtual functions have to be employed.

You’ll also like:

  1. Write A C++ Program For Pointer To Classes Example.
  2. Write C++ Example to illustrate two dimensional array implemented as pointer to a pointer.
  3. Two-Dimensional Arrays Using a Pointer to Pointer
  4. Write a C++ program illustrates the use of this pointer
  5. Storage Classes
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW