Conversion between child classes and parent classes in C ++ (Code tutorial), neutron tutorial

Source: Internet
Author: User

Conversion between child classes and parent classes in C ++ (Code tutorial), neutron tutorial

Conversion between child classes and parent classes in C ++ (Code tutorial)

// EX_EXAM.cpp: defines the entry point of the console application.

//

# Include "stdafx. h"

# Include <iostream>

Using namespace std;

Class

{

Public:

Virtual void foo () {cout <"A foo" <endl ;}

Void pp () {cout <"A pp" <endl ;}

};

Class B: public

{

Public:

Void foo () {cout <"B foo" <endl ;}

Void pp () {cout <"B pp" <endl ;}

Void FunctionB () {cout <"Excute FunctionB! "<Endl ;}

};

Int _ tmain (int argc, _ TCHAR * argv [])

{

A;

B B;

A * pa = & B;

Pa-> foo ();

Pa-> pp ();

If (B * pb = dynamic_cast <B *> (pa ))

{

Cout <"Convert from B to A successfully" <endl;

(Dynamic_cast <B *> (pa)-> FunctionB ();

(Dynamic_cast <B *> (pa)-> foo ();

(Dynamic_cast <B *> (pa)-> pp ();

(* Pa). foo ();

}

Else

Cout <"Cannot convert from B to A" <endl;

Return 0;

}

Class Base {

Public:

Int;

Base ()

{

A = 15;

}

Virtual void test (){}

Void write (){}

};

Class Derived: public Base {

Public:

Derived ()

{

A = 19;

}

Void test (){

Cout <"xiao go \ n" <a <endl;

}

Void test1 (){

}

Void write (){

Cout <"write" <endl;

}

};

The following conversions are incorrect:

Base * base = new Base

Derived * derive = new Derived

Base = derive;

The correct "Conversion" is as follows:

Base * base = new Derived

1

Base can implement all methods defined in the parent class. If it is a virtual function, it will implement sub-classes,

For example, test () is a virtual function.

The execution result of base. test is

Xiao go 19

During the execution, the BASE constructor is called first, and then the Derived constructor is called. Therefore, if a is 19, the result of accessing base. a is still 19.

In the base variable, except the virtual and constructor functions that can be executed by sub-classes, other sub-class functions cannot be executed, such as base-> test1 ().

Parent class to subclass

Generally, the parent class cannot be converted to a subclass. The following special cases can be:

Base * base = new Base ()

Dervie * derive = Base * base;

Here, only the parent class constructor is called, and the sub-class constructor is not called. The virtual function calls the implementation of the parent class instead of the implementation of the sub-class. Other sub-classes are added.

Can be called in derive.

Note:

Pure virtual Function (method: virtual ReturnType Function () = 0;) has a pure virtual Function as an abstract class until all pure virtual functions

After all are implemented, this class can be embodied.

C ++ supports two types of polymorphism: compile-time polymorphism and Runtime polymorphism.

A. polymorphism during compilation: implemented through overload functions and operator overloading.

B Runtime polymorphism: implemented through virtual functions and inheritance.

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.