Translation rules for C + + derived classes and base classes

Source: Internet
Author: User

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, in the following respects, the required friend can refer to the only common derived class that is the true subtype of the base class, which completely inherits the functionality of the base class. 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, which can be substituted with its subclass object when used with the base class object.
specific performance in the following several aspects
Derived class objects can assign values to base class objects.
You can assign a value to its base class object with a subclass (that is, a common derived class) object. Such as
A A1; Defines a base class A object A1
B B1; An object that defines the common derived class B of Class A B1
A1=B1;//Assignment of base class object A1 with derived class B object B1
Discards the members of the derived class itself when assigning values.
In fact, the so-called assignment only assigns values to the data member, and there is no assignment problem with the member function. Note: You cannot attempt to access the members of the derived class object B1 through object A1, because B1 members are different from the members of A1.
Assuming that age is a common data member added in derived class B, analyze the following usage:
a1.age=23;//error, A1 does not contain added members from derived classes
b1.age=21; Correct, the B1 contains the added members in the derived class
It should be noted that subtype relationships are unidirectional and irreversible. B is a subtype of a and cannot be said to be a sub-type of B.
a subclass object can only be assigned a value on its base class object, and its subclass object cannot be assigned a value using a base class object, because it is obvious that the base class object does not contain members of the derived class and cannot assign a value to the members of the derived class. Similarly, you cannot 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; Defines a base class A object A1
B B1; Defines a common derived class B object B1
a& r=a1; Defines the reference variable R for the base class A object and initializes it with A1.
At this point, the reference variable r is the alias of A1, and R and A1 share the same storage unit. You can also initialize a reference variable R with a subclass object, changing the last line above to
a& r=b1;//defines a reference variable R for a base class A object and initializes it with a derived class B object b1//.
or leave the 3rd line above "a& r=a1;" and re-assign values to r:
r=b1;//Assigning a reference variable r to A1 with a derived Class B object B1
Note: R is not an alias for B1 at this time, nor does it share the same storage unit with B1. It is just the alias of the base class part of B1, and R shares the same storage unit with the base class part in B1, 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 used with the child class object。 If there is a function
Copy CodeThe code is as follows:
Fun:void Fun (a& R)//parameter is a reference variable for the object of class A
{
cout<<r.num<<endl;
}//Output the reference variable's data member num
The formal parameter of a function is a reference variable of the object of Class A, which should be an object of Class A. Because the subclass object is assignable to the derived class object, the derived class object can automatically convert the type, and when the fun function is called, the object of the derived class B can be b1 as an argument: Fun (B1); The value of the base class data member num for the Output Class B object B1. As before, only the values of the base class members in the derived class can be exported 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 pointing 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 of the Student class graduate (postgraduate), outputting data with pointers to base class objects. The main example of this is that the pointer to the base class object points to the derived class object, in order to reduce the length of the program, only a few members are set in each class. The student class only has num (number), name (name) and score (score) 3 data members, and the graduate class only adds one data member pay (payroll).
The procedure is 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//declaring 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)//define Constructors
{
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;//Salary
};
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); Defines the student class object Stud1
Graduate Grad1 (2001,″wang″,98.5,563.5); Defines the Graduate class object Grad1
Student *pt=&stud1;//defines a pointer to the Student class object and points to Stud1
Pt->display (); Call the Stud1.display function
pt=&grad1; Pointer pointing to Grad1
Pt->display (); Call the Grad1.display function
}

Many readers will think that there are two display member functions with the same name in the derived class, according to the rule overridden by the same name, the display function of the derived class graduate object should be called, and student is called during the execution of 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 value for the output pay.
The problem is that PT is a pointer variable that points to the 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, and you cannot access the added members of the derived class. So Pt->display () does not call the display function added by the derived class graduate object, but the display function of the base class, so it only outputs Num,name,score3 data for graduate grad1.
If you want to output graduate Grad1 pay through the pointer, you can set a pointer variable ptr to a derived class object, point it to Grad1, and then invoke the display function of the derived class object with Ptr->display (). But it's not very convenient.
As you can see in this example, pointing a pointer variable to a base class object to a subclass object is legal and secure, and there is no compilation error. But the application does not fully satisfy people's hopes, and people sometimes want to be able to invoke the members of the base class and subclass object by using a base class pointer.
We will solve this problem in the next way, by using virtual functions and polymorphism

Translation rules for C + + derived classes and base classes

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.