C++ overloading
Def: the same class , the same method, but different argument .
function , friend function , constructor , member function can implement "Overloading".
#include<iostream>
using namespace std;
class Base{
friend void Base_friend( Base &T ,int a);
friend void Base_friend( Base &T ,int a , int b);
private:
int x;
public:
Base()
{
cout << "Base Construster" << endl;
}
Base (int _x) //Base Constructor overloading
{
cout << "Base Constructor overloading" << endl;
x=_x;
cout << "x=" << x << endl;
}
void People()
{
cout<< "Base People" << endl;
}
void People(int a) // member function overloading
{
cout << "People" <<" "<< a << endl;
}
};
//friend function
void Base_friend( Base &friend_obj ,int a)
{
friend_obj.x = a;
cout << "Base_friend x="<<friend_obj.x << endl;
}
//friend function
void Base_friend( Base &friend_obj ,int a , int b)
{
friend_obj.x = a;
cout << "Base_friend overloading x="<<friend_obj.x << endl;
cout << "b =" << b << endl;
}
///////////////////////////////////////////////////////////////////
// function overloading
void People()
{
cout<< "People" << endl;
}
void People (int x)
{
cout << "People" << " "<< x << endl;
}
////////////////////////////////////////////////////////////////////
int main()
{
People();
People (30);
Base obj;
obj.People();
obj.People(7);
Base obj1(2);
Base_friend(obj1 ,8);
Base_friend(obj1 ,8 , 10);
system("PAUSE");
return 0;
}

沒有留言:
張貼留言