Examples of the conversion of class object types in C++

  • 2020-05-27 06:49:59
  • OfStack

An example of an C++ class object type conversion

Preface:

Objects of classes with inheritance relationship can be transformed:

Subclass object types can be converted to superclass types,

For example, if the parameter of a function is the superclass object, and the parameter passed in is the subclass object, then the subclass object type automatically converts the superclass object:

But a superclass object cannot be subclassed.

Code:


# include <iostream>
using namespace std;

class A 
{
  public:
  void printm()
  {
  cout<<"A::print()"<<endl;
  }
};

class B:public A
{
public:
void printm()
{
cout<<"B::print()"<<endl;
}
};

void print(A a)
{
a.printm();
}

int main()
{
  A a;
  B b;
  a.printm();
  b.printm();
  print(a); 
  print(b);
  system("pause");
  return 0;  
}

Above is the C++ in the class object type conversion details, if you have any questions please leave a message or to the site community exchange discussion, thank you for reading, hope to help you, thank you for the support of the site!


Related articles: