Conversion rules for C + + derived classes and base classes _c language

Source: Internet
Author: User
Only a public derived class is a true subtype of the base class, and it inherits the functionality of the base class completely. There is an assignment-compatible relationship between the base class and the derived class object, because the derived class contains members that inherit from the base class, so the value of the derived class can be assigned to the base class object and can be substituted with its subclass object when the base class object is used.
the concrete manifestation in the following several aspects
A derived class object can assign a value to a base class object.
You can assign a value to its base class object with a subclass (that is, a public derived class) object. Such as
A A1; Define base Class A object A1
B B1; Object that defines the public derived class B of Class A B1
A1=B1; A1 assignment to a base class object B1 with a derived class B object
Discard the derived class's own members when assigning values.
In fact, assignment is only assigned to data members, and no assignment problems exist for member functions. Note: You cannot attempt to access a member of a derived class object B1 through an object A1, because B1 members are different from A1 members.
If age is a public data member that is added in derived class B, analyze the following usage:
a1.age=23;//error, A1 does not contain the added member in derived class
b1.age=21; Correct, the B1 contains the added members of the derived class
It should be noted that subtype relationships are one-way and irreversible. B is a subtype of a, and cannot say that a is a subtype of B.
You can only assign a value to its base class object with a subclass object, and you cannot assign a value to its subclass object with a base class object, because the base class object does not contain members of the derived class and cannot assign values to the members of the derived class. Similarly, it is not possible to assign values between different derived class objects of the same base class.
A derived class object can override a base class object to assign or initialize a reference to a base class object.
If you have defined a base class A object A1, you can define a reference variable for A1:
A A1; Define base Class A object A1
B B1; Define a public derived class B object B1
a& r=a1; Define the reference variable R for the base class A object and initialize it with A1
At this point, the reference variable r is the alias of A1, and R and A1 share the same segment of the storage unit. You can also initialize the reference variable R with a subclass object, and change the last line above to
a& r=b1;//defines the reference variable R for the base class A object and initializes it with a derived class B object b1//
Or to retain the 3rd line above "a& r=a1;", and to assign a value to r:
r=b1;//B1 Reference Variable r to A1 with derived class B object
Note: At this point R is not an alias for B1, nor does it share the same segment of storage with B1. It is just an alias to the base class part of the B1, and R shares the same segment with the base class in B1, and R and B1 have the same starting address.
If the parameter of a function is a reference to a base class object or a base class object, the corresponding argument can be in a subclass object. If you have a function
Copy Code code as follows:

Fun:void Fun (a& R)//parameter is a reference variable for object of class A
{
cout<<r.num<<endl;
//Output data member num of this reference variable
The formal parameter of a function is a reference variable of the object of Class A, and the argument should be an object of Class A. Because the subclass object is assignment-compatible with the derived class object, the derived class object can automatically convert the type, and the fun function can be invoked with the object B1 of derived class B as an argument: Fun (B1); Output Class B's Object B1 the value of the base class data member Num. As before, the value of a base class member in a derived class can only be output in the fun function.
The address of a derived class object can be assigned to a pointer variable that points to a base class object, that is, a pointer variable to a base class object can also point to a derived class object.
Example 11.10 defines a base class student (student), and then defines the common derived class graduate (graduate) of the student class, outputting the data with pointers to the base class object. This example focuses on pointing to a derived class object with a pointer to a base class object, and in order to reduce the length of the program, only a few members are set in each class. The student class only sets num (school number), name (name) and score (score) 3 data members, graduate class only adds one data member pay (salary).
procedures are as follows:
[Code]
#include <iostream>
#include <string>
Graduate::graduate (int n, string nam,float s,float p): Student (n,nam,s), Pay (p) {}
using namespace Std;
Class student//declaration Student classes
{
Public:
Student (int, string,float);//Declaration constructor
void display ()//Declaration output function
Private:
int num;
String name;
Float score;
};
student::student (int n, string nam,float s)//definition constructor
{
Num=n;
Name=nam;
Score=s;
}
void Student::d isplay ()//define output function
{
cout<<endl<<″num:″<<num<<endl;
cout<<″name:″<<name<<endl;
cout<<″score:″<<score<<endl;
}
Class Graduate:public student//declares a common derived class graduate
{
Public:
Graduate (int, string, float, float);/Declaration Constructor
void display ()//Declaration output function
Private:
Float pay;//Wages
};
Defining constructors
void Graduate::d isplay ()//define output function
{
Student::d isplay (); Call the display function of the student class
cout<<″pay=″<<pay<<endl;
}
int main ()
{
Student stud1 (1001,″li″,87.5); Define student class Objects Stud1
Graduate Grad1 (2001,″wang″,98.5,563.5); Define graduate class objects Grad1
Student *pt=&stud1;//defines a pointer to a Student class object and points to Stud1
Pt->display (); Calling the Stud1.display function
pt=&grad1; The pointer points to Grad1
Pt->display (); Calling the Grad1.display function
}

Many readers would argue that there are two display member functions with the same name in the derived class, and the call should be the display function of the derived class graduate object, based on the rules overridden by the same name, called student during the execution of the graduate::d isplay function:: Display function, output num,name,score, and then output the pay value.
in fact, this inference is wrong, first look at the output of the program:
num:1001
Name:li
score:87.5
num:2001
Name:wang
score:98.5
There is no output pay value.
The problem is that PT is a pointer variable pointing to a Student class object, even if it points to grad1, but actually PT points to the part of Grad1 that inherits from the base class.
By pointing to a pointer to a base class object, you can access only the base class members in the derived class, not the added members of the derived class. So Pt->display () instead of the display function added by the derived class graduate object, but the display function of the base class, output only the num,name,score3 data of the postgraduate Grad1.
If you want to pass the pay of graduate grad1 by pointer output, you can set a pointer variable ptr that points to the derived class object, point it to Grad1, and then call the display function of the derived class object with Ptr->display (). But it's not convenient.
You can see from this example that pointing a pointer variable to a base class object to a subclass object is legal, secure, and does not present a compilation error. But the application does not fully satisfy people's hopes, and people sometimes want to be able to call the base class and the members of the subclass object by using a base class pointer.
We will solve the problem in the next lecture by using virtual functions and polymorphism
Related Article

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.