PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 10, 2022

[FIXED] what do you mean by base class reference or derived class reference?

 July 10, 2022     c++, casting, class, reference     No comments   

Issue

I am confused about base class reference and derived class reference in the context of upcasting and downcasting.

In the following code, what is the use of &ref? In the reference, it was marked as a base class reference, to which a derived class obj was assigned.

What is the concept behind this?

#include <iostream>  
using namespace std;  
class Base  
{  
    public:  
        void disp()  
    {  
        cout << " It is the Super function of the Base class ";  
    }  
};  
  
class derive : public Base  
{  
    public:  
        void disp()  
        {  
            cout << "\n It is the derive class function ";  
        }  
      
};  
  
int main ()  
{  
    // create base class pointer  
    Base *ptr;  
      
    derive obj; // create object of derive class  
    ptr = &obj; // assign the obj address to ptr variable  
      
    // create base class's reference  
     Base &ref = obj;   
    // Or  
    // get disp() function using pointer variable  
      
    ptr->disp();  
    return 0;  
}  

Solution

Even though it is not clear what you're asking, i will try to clear some basics regarding this topic.

First, a reference refers to some other object. A reference is not an object by itself. So lets looks at some examples:

int n = 10;
int &r = n; // Here r is a reference to an int object

Similarly in your code snippet, when you wrote:

derive obj; //obj is an object of type derive
Base &ref = obj;  // ref is bound to the Base part of derive
Base *ptr = &obj; // ptr points to the Base part of derive

In the above example, there are 2 important things to note:

  1. Because a derived object contains subparts corresponding to its base class(es), we can use an object of a derived type as if it were an object of its base type(s). In particular, we can bind a base-class reference or pointer to the base-class part of a derived object as done above. This conversion is called derived-to-base conversion.
  2. The static type of object named ptr is Base*. While the dynamic type of the same object ptr is derive*. So the static and dynamic type differs.

Also, note that we are able/allowed to write:

Base *ptr = &obj;

because as $11.2/5 states -

If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class.

Now coming back to your question:

what is the use of &ref?

class Base  
{  
    public:  
        virtual void disp()  //VIRTUAL kewword USED HERE
    {  
        cout << " It is the Super function of the Base class ";  
    }  
};

If in your code snippet, the method disp() was virtual as shown in the above modified snippet, then we could've used ref and ptr to call the method disp() of the derive class. This is because:

According to virtual function documentation

a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object.

So when we write:

ref.disp(); //this calls the derive class method disp() at runtime
ptr->disp(); //this calls the derive class method disp() at runtime

The result of the above is that the derive class' function named disp is called at run-time and we see It is the derive class function printed on the console.

So basically the use is that we can decide at runtime which method(whether Base's or derive's) to call.

Check out the program's output here.



Answered By - Anoop Rana
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing