1 base class pointers and derived class pointers may match 4 of base class objects and derived class objects:
Reference a base class object directly with a base class pointer, referencing a derived class object directly with a derived class pointer, referencing a derived class object with a base class pointer, and referencing a base class object with a derived class pointer. 2. Base-class pointers refer to derived class objects
For example:
A * p; Pointer to an object of type A
A A_obj; Object of type A
B B_obj; Object of type B
p = & a_obj; P points to the object of type A
p = & b_obj; P refers to the object of type B, which is a derived class
With P, all elements inherited from class A can be accessed through b_obj,
However, you cannot use p to access a class B custom element (unless an explicit type conversion is used)
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 6 //referencing a derived class object using a base-class pointer7 classA8 {9 Charname[ -];Ten Public: One voidSetName (Char*s) A { - strcpy_s (name,s); - } the voidprint () - { -cout <<"name ="<< name <<Endl; - } + }; - + classB: PublicA A { at Charphone_num[ -]; - Public: - voidSetnum (Char*num) - { - strcpy_s (phone_num,num); - } in voidPrintnum () - { tocout <<"Phone_num ="<< Phone_num <<Endl; + } - }; the * voidMain () $ {Panax Notoginseng //base class pointers -A *ap; the //base class Object + A; A //derived class object the b b; + //base-class pointers point to base class objects -AP = &A; $ //to call a member function of a base class using a base class pointer $Ap->setname ("Tangxiaoxue"); -Ap->print (); - //base-class pointers point to derived class objects theAP = &b; - //to invoke a member function inherited from a base class using a base class pointerWuyiAp->setname ("Dujingui"); theAp->print (); - //to invoke a member function of a derived class using a derived class object WuB.setnum ("15110711517"); - //strongly-typed conversions to base-class pointers, calling member functions of derived classes About((b*) AP)printnum (); $}
3. Derived class pointers refer to base class objects
1#include <iostream>2 using namespacestd;3 4 classData5 {6 Public:7Data (intYintMintd)8 {9 SetData (y,m,d);Ten } One voidSetData (intYintMintd) A { -Year =y; -mouth =m; theDay =D; - } - voidprint () - { +cout << Year <<"-"<< Mouth <<"-"<< Day <<";"; - } + protected: A intYear,mouth,day; at }; - - classDatatime: PublicData - { - Public: -Datatime (intYintMintDintHintMiints):D ata (y,mi,d) in { - settime (h,mi,s); to } + voidSetTime (intHintMints) - { theHours =h; *minutes =m; $Second =s;Panax Notoginseng } - voidprint () the { + //make a type conversion to the this pointer, call the base class member function A //( (data*) This)->print (); equivalent to Data::p rint () the Data::p rint (); +cout << hours <<":"<< minutes <<":"<< Second <<Endl; - } $ Private: $ intHours,minutes,second; - }; - the voidMain () - {WuyiDatatime D (1995, A, -, A, -, -); the d.print (); -}
The relationship of pointers such as C + +