Assignment-compatible with the rules (C ++)

Under certain conditions, between different types of data may be typecast as an integer data type can be assigned to a double variable. Before the assignment, integer data is first converted to double-precision data type, and then assign it to a double variable. This data between different types of automatic conversion and assignment, called assignment compatibility. Between the base and derived class object and there are also compatible relationship assignment, assignment-compatible rules between base and derived class object refers to wherever you need a base class object, you can use a subclass object instead.

The following is mainly about the assignment between the product base and derived class object is compatible with
1- object of a derived class can be assigned to a base class object.

   A a1; //定义基类A对象a1
   B b1; //定义类A的公用派生类B的对象b1
   a1=b1; //用派生类B对象b1对基类对象a1赋值

  When the derived class assignment to abandon its own members, were assigned only data members.

  In fact, the so-called assignment just data members assigned to a member function assignment problem does not exist, in-memory data members and member functions are separated.

 Note: Do not attempt to access the a1 b1 members of the derived class object through the object after the assignment, because the members of the members of the a1 b1 is different.  

假设age是派生类B中增加的公用数据成员,分析下面的用法:

  a1.age=23;//错误,a1中不包含派生类中增加的成员
b1.age=21; //正确,b1中包含派生类中增加的成员

Only its base class object assigned sub-class object, rather than using the base class object assigned to its sub-class object, the reason is obvious, the size of two objects are different, because the base class object does not include members of the derived class It can not be assigned to members of the derived class. Similarly, between different objects of the same base class derived class can not be assigned
Assignment-compatible with the rules (C ++)
the object 2-derived class can refer to initialize the base class.
A base class is defined objects a1, a1 reference variable may be defined:

    A a1; //定义基类A对象a1
    B b1; //定义公用派生类B对象b1
    A &r=a1; //定义基类A对象的引用变量r(A的别名是r),并用a1对其初始化

In this case, r and some share the same storage unit a1. With reference to the variable r may be derived class object is initialized to the last line above

A& r=b1;//定义基类A对象的引用变量r,并用派生类B对象b1//对其初始化

NOTE: At this point b1 r is not an alias, not sharing the same memory cell section b1. It is just an alias part base class b1

R is defined herein as a reference class A, so its effective range is only so large class A, r and b1 in the base part share the same storage unit period, r and b1 having the same starting address. 
If the parameter is a function of the base or base class object reference to the object class corresponding arguments can subclass object.
3. address derived class objects can be assigned to the pointer to the base class. That is, a base class object pointer variable point may be derived class objects.
Example define a base class Student (Student), and then the common definition of a derived class Graduate Student class (students), output data pointer to a base class object.

#include <iostream>
#include <string>

using namespace std;
class Student//声明Student类
{
   public :
   Student(int, string,float );//声明构造函数
   void display( );//声明输出函数
   private :
   int num;
   string name;
   float score;
};
Student::Student(int n, string nam,float s)  //定义构造函数
{
   num=n;
   name=nam;
   score=s;
}
void Student::display( )//定义输出函数
{
   cout<<endl<<″num:″<<num<<endl;
   cout<<″name:″<<name<<endl;
   cout<<″score:″<<score<<endl;
}
class Graduate:public Student//声明公用派生类Graduate
{
   public :
   Graduate(int, string ,float ,float );//声明构造函数
   void display( );//声明输出函数
   private :
   float pay;//工资
};
Graduate::Graduate(int n, string nam,float s,float p):Student(n,nam,s),pay(p){ }//定义构造函数
void Graduate::display() //定义输出函数
{
   Student::display(); //调用Student类的display函数
   cout<<″pay=″<<pay<<endl;
}
int main()
{
   Student stud1(1001,″Li″,87.5); //定义Student类对象stud1
   Graduate grad1(2001,″Wang″,98.5,563.5); //定义Graduate类对象grad1
   Student *pt=&stud1;//定义指向Student类对象的指针并指向stud1
   pt->display( ); //调用stud1.display函数
   pt=&grad1; //指针指向grad1
   pt->display( ); //调用grad1.display函数
}

Many readers will think: There are two display member function with the same name in a derived class, according to the same name hidden rules should be invoked display function Graduate derived class object,
call the Student during the execution of Graduate display function :: :: display function, the output num, name, score, then the output value of the pay.

In fact this line of reasoning is wrong, take a look at the output of the program:

NUM: 1001
name: of Li
Score: 87.5
NUM: 2001
name: Wang
Score: 98.5
and no output value of the pay.

The problem is that pt Student class object is a pointer to a pointer variable, which refers to a class Student class, even if it points to grad1, but in fact are directed pt grad1 class inherited from the base portion (the space it points only the base class data members so much space). By pointer to a base class object, you can only access the derived class base class members, but can not access the derived class to increase membership . Therefore pt-> display () is not a derived class Graduate object calls the display function of the increase, but the display of the base class, only the output of the graduate grad1 num, name, score3 data.

In fact, the cast can also be assigned to the Student class address pointer Graduate class defined, but unsafe to do so, they might be confused can call the Graduate category increased membership, it is not true, it is not recommended for use

To sum up, mainly because of the base class and derived class members share different space size, compatibility problems caused by the assignment, such as int type assigned to the double type is assignment-compatible with the problem, and double type assigned to an int, is not compatible, it must turn stronger, or will be error

Original link: https://blog.csdn.net/ilovekobemusic/article/details/8839371

Guess you like

Origin blog.51cto.com/14233078/2457617