C + +-inherit virtual function call code

Source: Internet
Author: User

inheriting virtual function call code


This address: Http://blog.csdn.net/caroline_wendy


When a derived class inherits a base class , it is dynamically bound when using virtual , and is overwritten without the virtual keyword .

When you use a base class pointer (base*) to point to a derived class , the function is called by a dynamically bound function that calls the derived class, and the function that is not dynamically bound calls the base class .


Code:

/* * main.cpp * *  Created on:2014.9.12 *      author:spike *//*eclipse CDT, gcc 4.8.1*/#include <iostream>using Namespace Std;class Base {public:base (int J): I (j) {}virtual ~base () {}void func1 () {i*=10; Func2 ();} int GetValue () {return i;} protected:virtual void Func2 () {i++;} Protected:int i;}; Class Child:public Base {public:child (int J): Base (j) {}void func1 () {i*=100; Func2 ();} Protected:void Func2 () {i+=2;}}; int main (void) {base* PB = new Child (1);p b->func1 (); cout << pb->getvalue () << Endl;return 0;}

Output:

12



C + +-inherit virtual function call code

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.