An example tutorial for converting between base and derived classes in C++

  • 2020-04-02 02:40:19
  • OfStack

This article illustrates the conversion between base and derived classes in C++. It is helpful to deeply understand C++ object-oriented programming. Note here: the premise of this article's example is that derived classes inherit from base classes in a way that is public. Specific analysis is as follows:

The following program is an example of the explanation:


#include<iostream>
using namespace std;

class A
{
public:
  A(int m1, int n1):m(m1), n(n1){}
  void display();
private:
  int m;
  int n;
};

void A::display()
{
  cout << "m = " << m << endl;
  cout << "n = " << n << endl;
}

class B :public A
{
public:
  B(int m1, int n1, int p1) :A(m1, n1), p(p1){}
  void display();
private:
  int p;
};

void B::display()
{
  A::display();
  cout << "p = " << p << endl;
}

void print1(A& a)
{
  a.display();
}

void print2(B& b)
{
  b.display();
}

void print3(A a)
{
  a.display();
}

void print4(B b)
{
  b.display();
}

int main()
{
  A a(3, 4);
//  a.display();
  B b(10, 20, 30);
//  b.display();

  A * pa;
  B * pb;
  pa = &a;
//  pa->display();
  pb = &b;
//  pb->display();

//  pa = &b;
//  pa->display();

//  pb = &a;       // Error. Derived class Pointers cannot point to base class objects. 

//  print1(b);
//  print2(a);      // Error. A derived class reference cannot be assigned with a base class object. 
//  print3(b);
//  print4(a);      // Error. A derived class object cannot be assigned with a base class object. 

//  pb = pa;       // A base class pointer cannot be assigned to a derived class pointer. 

  pb = (B*)pa;     //It can be cast, but it is very insecure.
  pb->display();    //Security issues arise and p cannot be accessed because there is no p member in a
  system("pause");
  return 0;
}

Remember: derived class objects are base class objects, and derived classes contain members of the base class. A base class object is not a derived class object and cannot contain members of a derived type.

One, derived class to the conversion of the base class

1. The derived class object address is assigned to the base class pointer

The following code executes in the main function


A a(3, 4);
//  a.display();
  B b(10, 20, 30);
//  b.display();

  A * pa;
//  B * pb;
//  pa = &a;
//  pa->display();
//  pb = &b;
//  pb->display();

  pa = &b;
  pa->display();     //It's going to print 10, 20

Pa is a pointer to the base class, and it is legal to point to a derived class object because a derived class object is also a base class object. Statement outputs the base class portion of the derived class object.

Note: Instead of calling the display function of the derived class, we call the display function of the base class, because the pointer pa is a pointer to the base class, and the compiler only knows the type of pa at compile time. If you want to implement a call to the display function of a derived class, you need a virtual function to implement polymorphism. We'll talk about that in a later article.

Explain further the difference between compile time and run time.

At compile time the compiler knows that the pa is of type A *, but it does not know which object it points to, given the following statement


A a(3, 4);
B b(10, 20, 30);
A* pa;
int number;
cin >> number;
if (number >= 0)
  pa = &a;
else
  pa = &b;

The type of object the pa points to depends on the input and is entered at runtime, so the compiler has no way of knowing which type the pa points to.

2. Assign derived class objects to base class references

Reference and pointer basically no difference, reference is essentially a pointer, is a pointer constant, specific can refer to my another C++ reference and pointer connection and difference

The following code executes in the main function


A a(3, 4);
B b(10, 20, 30);
print1(b);      //It's going to print 10, 20

The formal parameter is the base class reference, the argument is the derived class object, and the derived class object is also the base class object, which can be assigned to the base class reference. Outputs the base class part of the derived class.

Note: The object itself is not copied, and b is still a derived class object .

3. Assign derived class objects to base class objects.


A a(3, 4);
B b(10, 20, 30);
print3(b);

The base class part of the derived class object is copied to the parameter.

Note: there is actually no direct translation from derived class objects to base class objects. An assignment or initialization to a base class object actually calls the function, the constructor at initialization, and the assignment operator at assignment.

Second, the conversion of the base class to the derived class

Remember: this transformation can cause serious security problems and should not be used when writing code. There is no automatic conversion from a base class to a derived class because a base class object can only be a base class object and cannot contain members of a derived type.

If you allow the base class object to be assigned to a derived class object, you can attempt to use that derived class object to access a nonexistent member.


A a(3, 4);
B b(10, 20, 30);
A * pa;
B * pb;
//  print2(a);      // Error. A derived class reference cannot be assigned with a base class object. 
//  print4(a);      // Error. A derived class object cannot be assigned with a base class object. 
//  pb = &a;       // Error. Derived class Pointers cannot point to base class objects. 

pa = &a;
pb = &b;

//pb = pa;           // Error. A base class pointer cannot be assigned to a derived class pointer. 

pb = (B*)pa;     //It can be cast, but it is very insecure.
pb->display();    //Security issues arise and p cannot be accessed because there is no p member in a

Notice that when we use casts, security issues arise when derived classes add members that do not exist in the base class.

Pb - > The display (); The display function of the derived class is called, but the memory it points to is the memory of the base class object a, and p does not exist. There will be serious consequences.


Related articles: