VC + + Programming the second lesson notes

Source: Internet
Author: User

Lesson two C + + inheritance encapsulation polymorphism Simple Type conversions
    • type int to char, truncation, loss of precision (3Bytes), warning.

    • Char-type conversion bit int, no truncation, no warning.

Type conversions for parent subclass subclasses

#include  <iostream>using namespace std;class Animal{public:     Animal () {}  //constructor overload     animal (Int height, int weight) {}     //constructor Parameters     void eat ()     {         cout<< "Animal eat" <<endl;    }     void sleep ()     {        cout< < "Animal sleep" <<endl;    }    void breathe ()      {        cout<< "Animal breathe" << endl;    }};class fish : public animal{public:     Fish (): Animal (150,200), a (1) {}   //can assign an initial value to a constant in a class and pass the parameters of the base class constructor     void  Breathe ()   //overwrite of function (subclass parent Class)     {//      animal::breathe ();   // You can continue calling the parent class function in this way         cout<< "fish bubble" <<endl;     }private:    const int a;}; Void fun (Animal* pan) {    pan->breathe ();} Void main () {    fish fh;    animal* pan;     pan = &fh;  //Parent class Subclass type conversion     fun (PAN);}

Run the result (call the function of the parent class breathe() ):

Animal breathepress any key to continue

Fish Object Memory Layout:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "alt=" Fish object Memory Layout "title=" Fish object Memory Layout "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "/>650) this.width=650, "src=" http://s3.51cto.com/wyfs02/M02/53/C9/wKioL1RwHiyBtKCWAAF5zjx6tP0555.jpg "title=" 2_ Fish object Memory layout. PNG "alt=" Wkiol1rwhiybtkcwaaf5zjx6tp0555.jpg "/>

It can be concluded that when the pointer of a fish object is converted to a pointer to a animal object, truncation occurs and the subsequent "Fish Inheritance part" memory is lost.

Virtual functions (polymorphism)

At this point, if you want to display the results for the breath of fish, you can precede the previous code with the breathe() Virtual identifier in front of the animal function:

virtual void Breathe () {cout<< "Animal breathe" <<ENDL;}

Results:

Fish bubblepress any key to continue

virtual , breathe () This function is a virtual function.

polymorphism

breathe () function is virtual function, this time C + + will use late binding (late binding) The technique of , at run time, is called C + + polymorphism, depending on the type of object (the address of the fish class object we pass in the program) to confirm which function is called.

Pure virtual function and abstract class

If you modify the above code again:

virtual void breathe () = 0;

Then this function is pure virtual function, and the corresponding animal class is abstract class.

    • An abstract class cannot instantiate an object, only as a base class for a derived class service.

VC + + Program compiling link principle and process

Icon:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/53/C9/wKioL1RwHkzDLqeqAALtY0T9W_Y145.jpg "title=" 2_vc++ The program compiles the link principle and the process. PNG "alt=" Wkiol1rwhkzdlqeqaalty0t9w_y145.jpg "/>

This article is from the "Zero4eva" blog, make sure to keep this source http://zero4eva.blog.51cto.com/7558298/1581244

VC + + Programming the second lesson notes

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.