The difference of overload, overwrite and override in C + +

Source: Internet
Author: User

Overload (Overload): in a C + + program, you can represent several functions that are semantically and functionally similar to the same name, but the parameters or return values are different, including type, order, or function overload.

Tag:overload Overwrite Override

Overload (Overload): in a C + + program, you can represent several functions that are semantically and functionally similar to the same name, but the parameters or return values are different, including type, order, or function overload. (1) The same range (in the same Class), (2) The function name is the same, (3) The parameter is different, (4) The virtual keyword is dispensable.

Override (overwrite): Refers to a derived class function that overrides the base class function, characterized by: (1) different ranges (in the derived class and base class), (2) the same name, (3) the same parameter, (4) The base class function must have the virtual keyword.

Overwrite (Overrides): A function of a derived class masks a base class function with the same name as the following: (1) If the function of the derived class has the same name as the function of the base class, the arguments are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that you are not confused with overloading). (2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the functions of the base class are hidden (note that you are confused with overrides).

#include <stdio.h>
#include <iostream>
Class Parent
{
Public
void F ()
{
printf ("Parent.f ()/n");
}
virtual void G ()
{
printf ("PARENT.G ()/n");
}
int Add (int x, int y)
{
return x + y;
}
Overload (overload) Add function
float ADD (float x, float y)
{
return x + y;
}
};

Class Childone:parent
{
overriding (overwrite) parent class functions
void F ()
{
printf ("childone.f ()/n");
}
Overwrite (override) The virtual function of the parent class, mainly realizing polymorphism
void G ()
{
printf ("childone.g ()/n");
}
};


int main ()
{
Childone childone;//= new Childone ();
parent* p = (parent*) &childOne;
Call PARENT.F ()
P->f ();
Realizing polymorphism
P->g ();
parent* P2 = new Parent ();
Overload (Overload)
printf ("%d/n", P2->add (1, 2));
printf ("%f/n", P2->add (3.4f, 4.5f));
Delete P2;
System ("PAUSE");
return 0;
}

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.