Implement base class (parent class) and derived class (subclass), validate inheritance and transform--assignment compatibility rules:
Subclass objects can be assigned to parent class objects (cut/slice)
Parent class object cannot be assigned to a subclass object
A pointer/reference to a parent class can point to a child class object
A pointer/reference to a subclass cannot point to the parent class object (can be done by forcing the type conversion)
#include <iostream>using namespace std;class people //parent class or base class {public : void display () { cout << "_name" << endl; }protected: string _name;}; class student:public people //subclass or derived class {protected: int _num;}; Void test () { people p; student s; p = s; //slices //s = p; //cannot be passed, stating that the parent object cannot be assigned to a subclass object People* p1 = &s; //pointers and references to parent classes can point to subclasses People& p2 = s; student* s1 = &p; //A pointer and reference to a subclass cannot point to the parent class //student& s2 = p; student * s1 = (student*) &p; //can be implemented with a strong transfer Student& s2 = (student&) p; //p2->_num = 10; //_num is a subclass object to which the parent class object can be crossed to access the subclass object //s2._num = 20;} Int main () { test (); system ("Pause"); return 0;}
How do I write the default member functions of a base class and a derived class? such as: constructors, copy constructors, assignment operator overloading, destructors.
#include <iostream>using namespace std;class people{public: people ( Const char* name) :_name (name) { cout << "People ()" << Endl; } people (const people& p) :_name (P._name) { cout << "People (const people& p)" << endl; } people& operator= (const people& s) { if (&s != this) { cout << "People& operator= (const people& s) "<<endl; _name = s._name; } return *this; } ~people () { cout < < "~people ()" << endl; }protected: string _name;}; Class student:public people{public: student (const char* name, Int num) :P eople (name) , _num (num) { cout << "Student ()" << endl; } Student (consT student& s) :P eople (s) , _num (S._num) { cout << "Student (const student& s)" << endl; } Student& operator= (const Student& s) { if (this != &s) { cout << "student& opeartor= (const Student& s) " << endl; people::operator= (s); _num = s._num; } return *this; } ~student () { cout << "~student ()" &NBSP;<<&NBSP;ENDL;&NBSP;&NBSP;&NBSP;&NBSP;} protected: int _num;}; Void test () { student s1 ("Zhang San"), student s2 (S1); &NBSP;&NBSP;&NBSP;&NBSP;STUDENT&NBSP;S3 ("John Doe"); s3 = s1;} Int main () { test (); system ("Pause"); return 0;}
Virtual Functions & Polymorphism
virtual function:
Precede the member function of the class with virtual, which becomes the virtual function.
Override of virtual function:
When a virtual function with the same parent class is defined in a subclass, a function called a subclass overrides the virtual function of the parent class.
Polymorphic:
When calling a function using the parent class's function or pointer, if the virtual function of the parent class is called, the virtual function of the parent class is invoked and the virtual function of the child class is called.
Such as:
#include <iostream>using namespace std;class people{ Public: virtual void buytickets () { cout << "Buy Tickets" << endl; }}; class student :p ublic people{public: virtual void buytickets () { cout << "Buy tickets-half price" << endl; }};void fun (people& p) { p. Buytickets ();} Void test () { people p; student s; fun (p);//people is the parent class, then the virtual function of the parent class is called. fun (s);//Call virtual function of subclass. }int main () { test (); system ("Pause"); return 0;}
This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1749895
"Inheritance and polymorphism" C + +: Assignment compatibility rules in inheritance, member functions of subclasses, virtual functions (Overrides), polymorphic