Conversion instances between base and derived classes in C + + tutorial _c language

Source: Internet
Author: User

The example in this article explains the transformations between the base class and the derived class in C + +. It will help to understand the object-oriented programming of C + +. Note here: The premise of this example is that the derived class inherits the base class in the form of public inheritance, with the keyword publicly. The specific analysis is as follows:

The following procedure explains the example:

#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::d isplay () {cout << "m =" << m << Endl;

cout << "n =" << n << Endl;}

Class B:p ublic a {public:b (int m1, int n1, int p1): A (M1, N1), p (p1) {} void display (); Private:int p;};
  void B::d isplay () {A::d isplay ();

cout << "p =" << p << Endl;}

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

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

void Print3 (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.

A derived class pointer cannot point to a base class object.
Print1 (b);      Print2 (a); Error.
You cannot use a base class object to assign a value to a derived class reference.
Print3 (b);      Print4 (a); Error. Cannot give derived class with base class objectThe object is assigned a value.       PB = PA;

  You cannot assign a value to a derived class pointer with a base class pointer.     PB = (b*) pa;
  can be cast, but is very unsafe.    Pb->display ();
  There is a security issue and p cannot be accessed because there is no P member system ("pause") in A;
return 0;

 }

Remember: the derived class object is a base class object that contains members of the base class. A base class object is not a derived class object and cannot contain a member of a derived type.

The transformation of a derived class to a base class

1. Derived class object address assignment to base class pointer

The following code is executed in the main function

A A (3, 4);  A.display ();
  b (a);  B.display ();

  A * PA;  B * PB;  pa = &a;  Pa->display ();  PB = &b;  Pb->display ();

  PA = &b;
  Pa->display ();     Will output 10 20

The PA is the base class pointer, and it is legal to point to the derived class object because the derived class object is also the base class object. Statement outputs the base class part of the derived class object.

Note: The display function of the derived class is not invoked here, and the display function of the base class is invoked because the pointer pa is the base class pointer and the compiler knows only the type of PA at compile time. If you want to implement a display function that invokes a derived class, you need to use a virtual function to achieve polymorphism. then the article will talk about.

Further explain the differences between compile-time and run-time.

At compile time the compiler can know the type of PA is a *, but do not know which object it points to, if the following statement

A A (3, 4);
b (a);
A * PA;
int number;
CIN >> number;
if (number >= 0)
  pa = &a;
else
  PA = &b;

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

2. Derived class object assignment to base class reference

Reference is basically the same as the pointer, the reference is essentially a pointer, is a pointer constant, you can refer to my other C + + references and pointers and the difference between

The following code is executed in the main function

A A (3, 4);
b (a);
Print1 (b);      Will output 10 20

A formal parameter is a base class reference, an argument is a derived class object, and a derived class object is also a base class object that can be assigned to a base class reference. Outputs the base class portion of a derived class.

Note: The object itself is not replicated at this time, and B is still a derived class object, which is mentioned as a pointer .

3. Derived class objects are assigned to the base class object.

A A (3, 4);
b (a);
Print3 (b);

The derived class object base class is partially copied to the formal parameter.

Note: there is actually no direct conversion from the derived class object to the base class object. An assignment or initialization of a base class object, which is actually called when the function is initialized, the constructor is invoked, and the assignment operator is invoked when the value is assigned.

Ii. transformation of base classes to derived classes

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

If you allow a derived class object to be assigned a value with a base class object, you can attempt to access a nonexistent member by using the derived class object.

A A (3, 4);
b (a);
A * PA;
B * PB;  Print2 (a);      Error. You cannot use a base class object to assign a value to a derived class reference.
//  print4 (a);      Error. You cannot assign a value to a derived class object with a base class object.
//  PB = &a;       Error. A derived class pointer cannot point to a base class object.

pa = &a;
PB = &b;

PB = PA;           Error. You cannot assign a value to a derived class pointer with a base class pointer.

PB = (b*) pa;     can be cast, but is very unsafe.
Pb->display ();    A security issue has occurred and P cannot be accessed because there are no P members in a

Note that when we use casts, security issues arise when a derived class adds a member that does not exist in the base class.

Pb->display (), the display function of the derived class is invoked, 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.