- 1. Overloading
- 2. Overloading in a Class
- 4. Overloading Prog. Ans
- 5. Overloading Constructors
- 8. Overloading Operators
1. Overloading
下面的程序中,因为输入数据类型不同,针对同一个功能,实现了三个函数:
/*Goal: look at a program, and see if we can make it more versatile*/
#include<iostream>
int findSmallerInt(int input1, int input2);
float findSmallerFloat(float input1, float input2);
char findSmallerChar(char input1, char input2);
int main()
{
int a = 5;
int b = 4;
float f1 = 5.43;
float f2 = 6.32;
char c1 = 'c';
char c2 = 'z';
std::cout<<findSmallerInt(a,b)<<" is the smaller of "<<a<<" and "<<b<<"\n";
std::cout<<findSmallerFloat(f1,f2)<<" is the smaller of "<<f1<<" and "<<f2<<"\n";
std::cout<<findSmallerChar(c1,c2)<<" is the smaller of "<<c1<<" and "<<c2<<"\n";
return 0;
}
int findSmallerInt(int input1, int input2)
{
if(input1<input2)
return input1;
return input2;
}
float findSmallerFloat(float input1, float input2)
{
if(input1<input2)
return input1;
return input2;
}
char findSmallerChar(char input1, char input2)
{
if(input1<input2)
return input1;
return input2;
}
2. Overloading in a Class
Look at the program below, the findSmaller function now seems so versatile! It accepts and returns three different variable types.
main.hpp:
#include<iostream>
class Compare
{
public:
int findSmaller(int input1, int input2);
float findSmaller(float input1, float input2);
char findSmaller(char input1, char input2);
};
int Compare::findSmaller(int input1, int input2)
{
if(input1<input2)
return input1;
return input2;
}
float Compare::findSmaller(float input1, float input2)
{
if(input1<input2)
return input1;
return input2;
}
char Compare::findSmaller(char input1, char input2)
{
if(input1<input2)
return input1;
return input2;
}
main.cpp:
/*Goal: look at a program, and see if we can make it more versatile*/
#include "main.hpp"
int main()
{
Compare c;
int a = 5;
int b = 4;
float f1 = 5.43;
float f2 = 6.32;
char c1 = 'c';
char c2 = 'z';
std::cout<<c.findSmaller(a,b)<<" is the smaller of "<<a<<" and "<<b<<"\n";
std::cout<<c.findSmaller(f1,f2)<<" is the smaller of "<<f1<<" and "<<f2<<"\n";
std::cout<<c.findSmaller(c1,c2)<<" is the smaller of "<<c1<<" and "<<c2<<"\n";
return 0;
}
4. Overloading Prog. Ans
main.hpp:
#include<iostream>
//C++ allows us to 'overload' the same function name as long as
//the variable types in the function inputs are different.
class Compare
{
public:
int findSmaller(int input1, int input2);
float findSmaller(float input1, float input2);
char findSmaller(char input1, char input2);
int findSmaller(int input1, int input2, int input3);
float findSmaller(float input1, float input2, float input3);
char findSmaller(char input1, char input2, char input3);
int findSmaller(int arrayIn[], int size);
float findSmaller(float arrayIn[], int size);
char findSmaller(char arrayIn[], int size);
};
int Compare::findSmaller(int input1, int input2)
{
if(input1<input2)
return input1;
return input2;
}
float Compare::findSmaller(float input1, float input2)
{
if(input1<input2)
return input1;
return input2;
}
char Compare::findSmaller(char input1, char input2)
{
if(input1<input2)
return input1;
return input2;
}
int Compare::findSmaller(int input1, int input2, int input3)
{
if(input1 < input2)
{
if(input1 < input3)
return input1;
else
return input3;
}
else
{
if(input2 < input3)
return input2;
else
return input3;
}
}
float Compare::findSmaller(float input1, float input2, float input3)
{
if(input1 < input2)
{
if(input1 < input3)
return input1;
else
return input3;
}
else
{
if(input2 < input3)
return input2;
else
return input3;
}
}
char Compare::findSmaller(char input1, char input2, char input3)
{
if(input1 < input2)
{
if(input1 < input3)
return input1;
else
return input3;
}
else
{
if(input2 < input3)
return input2;
else
return input3;
}
}
int Compare::findSmaller(int arrayIn[], int size)
{
{
for (int i = 0; i < size; ++i)
for (int j = 0; j < size - i - 1; ++j)
if (arrayIn[j] > arrayIn[j + 1])
{
int temp = arrayIn[j];
arrayIn[j] = arrayIn[j + 1];
arrayIn[j + 1] = temp;
}
}
return arrayIn[0];
}
float Compare::findSmaller(float arrayIn[], int size)
{
{
for (int i = 0; i < size; ++i)
for (int j = 0; j < size - i - 1; ++j)
if (arrayIn[j] > arrayIn[j + 1])
{
float temp = arrayIn[j];
arrayIn[j] = arrayIn[j + 1];
arrayIn[j + 1] = temp;
}
}
return arrayIn[0];
}
char Compare::findSmaller(char arrayIn[], int size)
{
{
for (int i = 0; i < size; ++i)
for (int j = 0; j < size - i - 1; ++j)
if (arrayIn[j] > arrayIn[j + 1])
{
int temp = arrayIn[j];
arrayIn[j] = arrayIn[j + 1];
arrayIn[j + 1] = temp;
}
}
return arrayIn[0];
}
5. Overloading Constructors
上一章中也涉及过,构造函数也可以被重载:
main.hpp:
//header file for main.hpp
#include<iostream>
#include<string>
using namespace std;
class Square
{
private:
int length;
int width;
public:
Square();
Square(int lenIn, int widIn);
int getLength();
int getWidth();
};
Square::Square()
{
length = 0;
width = 0;
}
Square::Square(int lenIn, int widIn)
{
length = lenIn;
width = widIn;
}
int Square::getLength()
{
return length;
}
int Square::getWidth()
{
return width;
}
main.cpp:
/*Goal: learn about overloading constructors*/
#include "main.hpp"
int main()
{
Square s1;
Square s2(4,3);
cout<<"s1 dimensions are: "<<s1.getWidth()<<","<<s1.getLength();
cout<<"\ns2 dimensions are: "<<s2.getWidth()<<","<<s2.getLength();
return 0;
}
8. Overloading Operators
C++中可以自定义运算符,这个在自定义数据类型进行运算时非常有用。 比如下面两个对象直接相加。
#include <iostream>
using namespace std;
class Shape
{
private:
int length; // Length of a box
int width;
public:
// Constructor definition
Shape(int l = 2, int w = 2)
{
length = l;
width = w;
}
double Area()
{
return length * width;
}
int operator + (Shape shapeIn)
{
return Area() + shapeIn.Area();
}
};
int main(void)
{
Shape sh1(4, 4); // Declare shape1
Shape sh2(2, 6); // Declare shape2
int total = sh1 + sh2;
cout << "\nsh1.Area() = " << sh1.Area();
cout << "\nsh2.Area() = " << sh2.Area();
cout << "\nTotal = "<<total;
return 0;
}
sh1.Area() = 16
sh2.Area() = 12
Total = 28