Lab 07
Lab 07
using std::cout;
using std::endl;
return 0;
}
In this function, int sum(int *array, int len) and double sum(double *array, int
len) have the same name in global scope, but the types of argument lists are different.
1/9
National Chiao Tung University Laboratory Manual 07
Department of Electrical and Computer Engineering April 16, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
The different number of argument lists is also one kind of function overloading.
// lab7-1-2.cpp
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "min(4,3) = " << min(4,3) << endl;
cout << "min(1,3,2) = " << min(1,3,2) << endl;
return 0;
}
Notice that, the function overloading can be achieved by different data type and different
number of argument list, but it cannot be different return type. For example, int sum(int
*array, int len) and double sum(int *array, int len) cannot exist at the same time.
class Point2D
{
private:
int x;
int y;
double value;
public:
Point2D();
Point2D(int n1, int n2);
2/9
National Chiao Tung University Laboratory Manual 07
Department of Electrical and Computer Engineering April 16, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
Point2D::Point2D()
{
x = 0;
y = 0;
value = 0;
}
int main()
{
Point2D pt1(3,4,3.9);
Point2D pt2;
pt1.displayPoint2D();
pt2.displayPoint2D();
3/9
National Chiao Tung University Laboratory Manual 07
Department of Electrical and Computer Engineering April 16, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
pt1.assignPoint2D(1,3);
pt2.assignPoint2D(2,3,1.1);
pt1.displayPoint2D();
pt2.displayPoint2D();
return 0;
}
class Point2D
{
private:
int x;
int y;
double value;
public:
Point2D();
Point2D(int n1, int n2);
Point2D(int n1, int n2, double v);
4/9
National Chiao Tung University Laboratory Manual 07
Department of Electrical and Computer Engineering April 16, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
double distPoint2D(const Point2D &pt1, const Point2D &pt2, const Point2D &pt3)
{
double n1 = distPoint2D(pt1, pt2);
double n2 = distPoint2D(pt1, pt3);
double n3 = distPoint2D(pt2, pt3);
Point2D::Point2D()
{
x = 0;
y = 0;
value = 0;
}
5/9
National Chiao Tung University Laboratory Manual 07
Department of Electrical and Computer Engineering April 16, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
x = n1;
y = n2;
value = v;
}
int main()
{
Point2D pt1(3,4,4.1);
Point2D pt2(3,2,4.5);
if (pt1 == pt2) std::cout << "pt1 is equal to pt2 " << std::endl;
else std::cout << "pt1 is not equal to pt2 " << std::endl;
pt1.displayPoint2D();
pt2.displayPoint2D();
Point2D pt3;
pt3 = pt1 + pt2;
pt3.displayPoint2D();
return 0;
}
// ex7-1.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "Complex.h"
int main()
6/9
National Chiao Tung University Laboratory Manual 07
Department of Electrical and Computer Engineering April 16, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
{
Complex a(1.0, 7.0), b(9.0, 2.0), c; // create three Complex objects
printMeg(a,b,’+’); // output (1.0, 7.0) + (9.0, 2.0) =
c = a + b; // invoke operator + and assign to object c
printComplex(c); // output object c
cout << endl;
return 0;
}
7/9
National Chiao Tung University Laboratory Manual 07
Department of Electrical and Computer Engineering April 16, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
2. *STRING OPERATION
Please implement the string class defined as follows,
// my_string.h
#include <iostream>
class string
{
private:
char *p;
public:
string (const char *s);
string (const string &s);
~string() {if(p) { delete [] p; p = NULL; } }
friend std::ostream& operator << (std::ostream&, const string &s);
friend string operator + (const string &s, const string &t);
bool operator <= (const string &s);
};
/*
definition of class string.
*/
Main function
int main()
{
string t1("New");
cout << "t1=" << t1 << endl;
string t2 = "York";
cout << "t2=" << t2 << endl;
string t3 = t1 + " " + t2;
cout << "t3=" << t3 << endl;
if ( t1 <= t2 )
cout << t1 << " is smaller than " << t2 << endl;
else
cout << t1 << " is bigger than " << t2 << endl;
return 0;
}
8/9
National Chiao Tung University Laboratory Manual 07
Department of Electrical and Computer Engineering April 16, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
Output
t1 = New
t2 = York
t3 = New York;
New is smaller than New York
9/9