Lab 05
Lab 05
TASK5-1 CONSTRUCTOR
Please try to compile and execute the program lab5-1-1, and observe the results.
// lab5-1-1.cpp
#include <iostream>
class Point2D
{
private:
int x;
int y;
double value;
public:
void assignPoint2D(int n1, int n2, double v);
void displayPoint2D();
};
void Point2D::displayPoint2D()
{
std::cout << "(" << x << "," << y << ") = ";
std::cout << value << std::endl;
}
int main()
{
Point2D ptArray[10];
for (int i=0;i<10;i++)
ptArray[i].displayPoint2D();
return 0;
}
1/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
We add constructor to class Point2D and make program lab5-1-2 work as expect.
// lab5-1-2.cpp
…
class Point2D
{
private:
int x;
int y;
double value;
public:
Point2D(); // default constructor
void assignPoint2D(int n1, int n2, double v);
void displayPoint2D();
};
Point2D::Point2D()
{
x = 0;
y = 0;
value = 0.0;
}
…
You can also use a parenthesized expression list to build your default constructor. In the
above example, you can replace the declaration and definition of default constructor as
Point2D():x(0), y(0), value(0.0) {}.
pt1.displayPoint2D();
pt2.displayPoint2D();
pt3.displayPoint2D();
pt3.assignPoint2D(2,1,0.0);
pt3.displayPoint2D();
return 0;
}
2/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
TASK5-2 DESTRUCTOR
In class Point2D, we do not specific the destructor ~Point2D() since the compiler will
generate one. However, if you use new or delete memory in the object, you need constructor
to allocate memory and destructor to release it.
Please modify the class Point2D as PointND, which is used to record the N-dimensional
coordinate using an integer array.
// lab5-2.cpp
#include <iostream>
#include <assert.h>
class PointND
{
private:
int *coord;
double value;
public:
PointND();
~PointND();
void assignValue(double v);
void assignCoord(int *vec, int len);
void displayPointND();
};
PointND::PointND()
{
value = 0.0;
coord = new int [num];
for (int i=0;i<num;i++) coord[i] = 0;
}
PointND::~PointND()
{
delete []coord;
}
void PointND::assignValue(double v)
{
value = v;
}
3/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
void PointND::displayPointND()
{
std::cout << "(";
for (int i=0;i<num;i++)
{
std::cout << coord[i];
if (i!=num-1)
std::cout << ", ";
}
std::cout << ") = " << value << std::endl;
}
int main()
{
PointND pt1;
pt1.displayPointND();
PointND pt2;
pt2.assignValue(1.0);
pt2.displayPointND();
PointND pt3;
pt3.assignValue(4.3);
pt3.assignCoord(vec,num);
pt3.displayPointND();
delete []vec;
return 0;
}
4/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
#include <assert.h>
int main()
{
int *vec = new int [num];
for (int i=0;i<num;i++) vec[i] = i;
PointND pt1;
pt1.assignValue(4.3);
pt1.assignCoord(vec,num);
pt1.displayPointND();
PointND pt2(pt1);
pt2.displayPointND();
delete []vec;
return 0;
}
// Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#endif
5/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
// Complex.cpp
#include <iostream>
using std::cout;
#include "Complex.h"
// ex5-1.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "Complex.h"
int main()
{
Complex a(1.0, 7.0), b(9.0, 2.0), c; // create three Complex objects
a.printComplex(); // output object a
cout << " + ";
b.printComplex(); // output object b
cout << " = ";
c = a.add(b); // invoke add function and assign to object c
c.printComplex(); // output object c
cout << endl;
return 0;
}
Please add the constructor, copy constructor and destructor to the class Matrix you defined in
program ex4-2. Note that you must use dynamic memory allocation to create the object.
The main structure of the program becomes,
// Matrix.h
6/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
#ifndef MATRIX_H
#define MATRIX_H
/* Write class definition for Matrix and add constructors and destructor*/
#endif
// Matrix.cpp
#include <iostream>
using std::cout;
#include "Matrix.h"
// ex5-2.cpp
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include "Matrix.h"
int main()
{
int n;
cout << “Enter n for n x n matrix: ” << endl;
cin >> n;
7/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
Matrix C;
C.multiplyMatrix(A,B); // C = A * B
cout << "A*B = ";
C.printMatrix(); // output object C
cout << endl;
return 0;
}
Vec vec1(array1,dim1);
Vec vec2;
vec2.assign(array2,dim2);
8/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
vec1.printVec();
/* print out the message if vec1 and vec2 are the same or not*/
Vec vec3;
vec3.unionSet(vec1,vec2); // vec3 is union set of vec1 and vec2
vec3.sort();
cout << “vec3(sorted): “;
vec3.printVec();
cout << “Min in vec1 and vec2: ” << vec3.min() << endl;
cout << “Max in vec1 and vec2: ” << vec3.max() << endl;
Vec vec4;
vec4 = vec3.inpendetSet();
cout << “vec4: “;
vec4.printVec();
delete [] array1;
delete [] array2;
return 0;
}
9/10
National Chiao Tung University Laboratory Manual 05
Department of Electrical and Computer Engineering Match 26, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin (Charles) Wen
10/10