Overloading in the inheritance of C + +

Source: Internet
Author: User

In C + +, the overload between the parent class and the subclass, the function with the same name overrides, even if the parameter type is different and cannot be called directly, it must be called by the parent class's default generated object.

Test procedure:

#include <iostream>class a{public:void Fun () {std::cout<< ' A fun ' <<std::endl; void fun (int. num) {std::cout<< "A Fun" <<num<<std::endl;}}; Class B:public A{public:void Fun () {std::cout<< ' B fun ' <<std::endl;}}; void Main () {int num = 100; B B;b.fun ();//B.fun (num); Cannot invoke compile error, the same name function is overwritten even if the parameter type is different. B.a::fun (num);//Only Call Std::cin.get () through this method;}

void Fun () void fun (int num) in the parent class is overwritten with a  class void fun ().


Overloading in the inheritance of C + +

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.