C + + Overload related interview questions

Source: Internet
Author: User
C + + Overload related interview questions Table of Contents 1. What is an overload, what is the definition of the overload. 2. Overloaded functions cannot be distinguished by returning values. 3. What is the difference between overloading (overload) and overriding (override). 4. What is redefinition (redefining). 5. Please write the prefix (prefix) and suffix (postfix) overload function of the + + operator.1 What is an overload and what is the definition of the overload. Overloading refers to a set of functions within the same scope that have the same function name and different argument lists. A simple example is as follows:
int foo (int a);
int foo (int a, int b); The number of arguments is different from
int foo (double A);//parameter types are different
2 overloaded functions cannot be distinguished by returning a value. My personal understanding of this problem is that, by the return value, a context connection is introduced if the type of the return value is not specified at the time of the call, the compiler will not be able to choose which function to call
int foo (int a);
Double foo (int a);
The above two functions are only the return value of different

int a = foo (1);
Double b = foo (2); Which function is the context

-connected foo (3)//??? tune?
3 What is the difference between an overload (overload) and an override (override). The overload must be within the same scope, the override occurs between the base class and the derived class, and the base class function must have the virtual keyword overloaded list of function arguments must be different, the number of overridden function parameters is the same, type is compatible.

Looking at the example below, the Foo function is defined in the base class and the virtual keyword is added, and Foo in the derived class is the same as the base class parameter:

Class B
{public
:
	virtual void foo (int a)
	{
		cout << "b::foo (int)" << Endl;
	}
};

class C:public B
{public
:
	void foo (int a)
	{
		cout << "c::foo (int)" << Endl;
	}
};


int main ()
{
	b b;
	c C;
	B *PB;

	PB = &b;
	Pb->foo (1);
	PB = &c;
	Pb->foo (1);
	return 0;
}

The output results are:

B::foo (int)
C::foo (int)

modifying Foo in class C is defined as:

void foo (float a);

The output becomes:

B::foo (int)
B::foo (int)

Visible or the base class of the call Foo, Class C foo is not override, but was redefining. If you define Foo in Class C as:

void foo (const int a);

The output results are:

B::foo (int)
C::foo (int)

The

Override is successful again, and the const keyword is visible without affecting override. 4  What is a redefinition (redefining). A redefinition is a function in a derived class that masks a function in a derived class that has the same name in the base class as the function in the base class. However, the parameters are different, regardless of virtual, the functions in the base class are overridden in the derived class with the same name as the function in the base class, and the parameters are the same. But without the virtual keyword, the functions in the base class are overwritten 5  please write the prefix (prefix) and suffix (postfix) overloaded functions for the + + operator.

#include <iostream>
#include <stdio.h>
using namespace std;

Class A 
{public
:
	A (): a (0) {}
	a (int i): A (i) {}
	a& operator++ ();
	A operator++ (int);
	Friend ostream& operator << (ostream&, a&);
	Friend a operator + (a &a1, a &a2);
Private:
	int A;
};

ostream& operator << (Ostream &output, a &a)
{
	output << "A is" << a.a << en DL;
	return output;
}

Prefix, ++a
a& a::operator++ ()
{
	++a;
	cout << "++a" << Endl;
	return *this;
}
Postfix, a++
a a::operator++ (int)
{
	a t = *this;
	a++;
	cout << "a++" << Endl;
	return t;
}

A operator + (a &a1, a &a2)
{return
	a (a1.a + a2.a);
}

int main ()
{
	a A;
	a++;
	++a;
	cout << ++a;
	A A1 (1);
	A A2 (2);
	A = a1 + A2;
	cout << A;
	return 0;
}

created:2015-03-09 Monday 20:38

Emacs 24.4.1 (ORG mode 8.2.10)

Validate

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.