Lab #12. Object-oriented programming
Lab #12.1.
Transforming a simple class I
Given program
(lab12.1.cpp).
#include <iostream>
using namespace std;
class twointegers
{
public:
int
a;
int
b;
};
int main()
{
twointegers
t;
t.a = 5;
t.b = 7;
cout
<< t.a+t.b << endl;
return 0;
}
/*
int main()
{
twointegers
t (5, 7);
cout
<< t.sum() << endl;
return 0;
}
*/
It prints the
sum of numbers 5 and 7. Transform (complement) the program (class twointegers) according to the following terms:
·
fields
a and b should be transformed to private,
·
the
program should run fine, if existing main is replaced by the commented main,
·
the functionality should not change.
A correct solution: lab12.1a.cpp.
Lab #12.2.
Transforming a simple class II
Given program
(lab12.2.cpp).
#include <iostream>
using namespace std;
class threedoubles
{
public:
double x;
double y;
double z;
};
int main()
{
threedoubles
t;
t.x = 1.1;
t.y = 2.2;
t.z = 3.3;
cout
<< t.x+t.y+t.z << endl;
threedoubles
*tp = new threedoubles;
tp->x
= 0.2;
tp->y
= 0.3;
tp->z
= 0.4;
cout
<< tp->x+tp->y+tp->z << endl;
delete tp;
return 0;
}
/*
int main()
{
threedoubles
t;
t.set (1.1, 2.2,
3.3);
t.printsum ();
threedoubles
*tp = new threedoubles
(0.2, 0.3, 0.4);
tp->printsum();
delete tp;
return 0;
}*/
It prints the
sum of 1.1, 2.2 and 3.3, as well as the sum of 0.2, 0.3 and 0.4. Transform (complement)
the program (class threedoubles) according to
the following terms:
·
fields
x, y, z should be transformed to private,
·
the
program should run fine, if existing main is replaced by the commented main,
·
the functionality should not change.
A correct solution: lab12.2a.cpp.