C + +: Assignment compatibility relationship between a base class and a derived class object

Source: Internet
Author: User

4.5 Assignment compatibility relationship between a base class and a derived class object
Under certain conditions, type conversions can be made between different types of data, such as the ability to assign integer data to a double type variable.
Before assigning a value, convert the integer data to double-precision data and then double-type the variable. This automatic conversion between different types is called assignment-compatible. There is also an assignment compatibility relationship between the base class and the derived class object, where the assignment compatibility rule between the base class and the derived class object refers to the use of the object of the public derived class wherever the base class object is needed. Because, through public inheritance, in addition to constructors and destructors, derived classes retain all other members of the base class. Then, the derived class has all the functions of the base class, and all the functions that the base class can implement can be implemented by the public derived classes. We can assign the value of the derived class object to the base class object, which can be substituted with its subclass object when using the base class object.

For example, the following two classes are declared:

class base{          //         ...      };        class Derived: public base{   //         ...      };  

According to the compatibility rules, the objects of the base class base can be used anywhere, and can be substituted with the object of the derived class derived.
However, only members that inherit from the base class can be used. Specific performance in the following areas:

(1) A derived class object can assign a value to a base class object, that is, a data member inherited from a base class in a derived class object, assigned to the base class one by one the data member of the object.

For example:

Base B;//object that defines base class base B
Derived D; //define the base class base of the public derived class derived object D
B=d; //Assignment to object B of base class base using the object D of the derived class derived

the effect of this assignment is that all data members in object B will have the value of the corresponding data member in Object D.

(2) A derived class object can initialize a reference to a base class object. For example:
Base B; //Define object B of base class base
Derived D; //define the base class base of the public derived class derived object D
Base &br=d; The reference BR for the object that defines the base class base, and initializes it with the object derived the derived class

(3) A derived class object address can be assigned a pointer to a base class object. For example:
Derived D; //define the base class base of the public derived class derived object B
Base *bp=&d; //assigns the address &d of the derived class object to a pointer to the base class BP, that is, to point to the base class
//object Pointer BP can also point to a derived class object D

(4) If a function's formal parameter is a reference to a base class object or a base class object, the derived class object can be used as an argument when the function is called. For example:
class base{ //Declaration base class Base
Public :
int i;
               ...
};
class Derived:public base{ //declares Base's public derived class Derived
              ...
};
void Fun (Base &bb)
{
cout<<bb.i<<endl; //Output The data member of the object represented by the reference I
}

when calling a function fun, you can use the object D4 of the derived class derived as the argument:
Fun (D4);
The output derived class derived object D4 assigns the value to the base class data member I.

exchange between a base class and a derived class object

#include <iostream>using namespacestd;classbase{//declaring base class base  Public:    inti; Base (intX//constructor for base class base{i=x; } Base (ConstBase &b) {cout<<"Base Copyconstructor"<<Endl; }    voidShow ()//member functions{cout<<"i="<<i<<Endl; }};classDerived: Publicbase{//declares a public derived class of base class base derived   Public: Derived (intx): Base (x)//constructors for derived classes{} Derived (ConstDerived &d): Base (d) {cout<<"Derived Copyconstructor"<<Endl; }     }; voidFun (Base &bb)//normal function, formal parameter is a reference to a base class object{cout<<bb.i<<Endl;} intMain () {Base B1 ( -);//defines a base class object B1b1.show (); Derived D1 ( One);//defines a derived class object D1B1=D1;//assigning values to base class objects B1 with derived class object D1b1.show (); Derived D2 ( A);//defines a derived class object D2Base &b2=d2;//Initializes a reference b2 of a base class object with the derived class object D2b2.show (); Derived D3 ( -);//defines a derived class object D3Base *b3=&d3;//assigns the address &d3 of a derived class object to a pointer to a base class object B3B3->Show (); Derived D4 ( -);//defines a derived class object D4Fun (D4);//derived class object D4 as an argument to a function funDerived d6 (D4); return 0;}/*The result of the program operation is as follows: Base 100Base one base 3344Base copyconstructorderived copyconstructor

Description

(1) A pointer that is declared to a base class object can point to an object of its public derived class, but does not allow a private derived object to point to it. For example:classbase{...}; classDerived:Privatebase{...}; intMain () {Base OP1,*ptr;//defines the base class base object OP1 and its pointer to base class base PTRDerived OP2;//defines the object of the derived class derived OP2PTR = &op1;//point the pointer ptr to the base class base Object Op1PTR = &op2;//error, the pointer to base class base ptr is not allowed to point to its private derived class object Op2            ......            return 0; } ( 2) Allows an object that is declared as a pointer to a base class to point to its public-derived class, but one that is declared as pointing to a derived class object is pointing to an object of its base class. For example:classbase{...}; classDerived: Publicbase{...}; intMain () {Base obj1; //defines the base class base Object Obj1Derived obj2,*ptr;//defines a derived class derived object Obj2 and its pointer to a derived class object PTRPTR = &obj2;//point the pointer ptr to the derived class derived object Obj2PTR = &op2;//error, not allowed to point to a derived class derived pointer ptr points to its base class base Object Obj1            ......            return 0; } 

C + +: Assignment compatibility relationship between a base class and a derived class object

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.