Catedra FCIM
Raport
Lucrarea de laborator Nr.3
Programarea orientată pe obiecte
Varianta 2
Chișinău 2022
Sarcina
a) Să se creeze clasa 2-D de coordonate în plan. Să se definească operatorii "+"
şi "-"ca funcţii prietene, iar operatorii de atribuireşi de comparare –ca
metode ale clasei. De prevăzut posibilitatea efectuării operaţiilor atît
între coordonate, cît şi între coordonate şi numere obişnuite
Rezolvare
a)
#include <iostream>
#include <string>
using namespace std;
class Coordinates
{
friend Coordinates operator+(const Coordinates &obj1, const Coordinates
&obj2);
friend Coordinates operator-(const Coordinates &obj1, const Coordinates
&obj2);
public:
int x;
int y;
bool operator == (const Coordinates &obj1){
return (x == obj1.x);
}
bool operator > (const Coordinates &obj1){
return (x > obj1.x);
}
bool operator >= (const Coordinates &obj1){
return (x >= obj1.x);
}
bool operator < (const Coordinates &obj1){
return (x < obj1.x);
}
bool operator <= (const Coordinates &obj1){
return (x <= obj1.x);
}
bool operator != (const Coordinates &obj1){
return (x != obj1.x);
}
return *this;
};
void setX(int x)
{
this->x = x;
}
void setY(int y)
{
this->y = y;
}
Coordinates()
{
x = 0;
y = 0;
};
Coordinates(int x, int y){
this->x = x;
this->y = y;
};
Coordinates(const Coordinates &obj){
this->x = obj.x;
this->y = obj.y;
};
};
Coordinates coord;
Coordinates coord;
cout << "yy.x = " << yy.x << " yy.y = "<< yy.y << endl;
cout << "tt.x = " << tt.x << " tt.y = "<< tt.y << endl;
if (yy == tt){
cout << "yy is equal with tt"<< endl;
}
else {
cout << "yy is not equal with tt" << endl;
}
Output:
class Stack
{
public:
int top;
int *myStack; //stack array
void showStack()
{
for (int i=0; i<=this->top; i++)
{
cout << this->myStack[i]<< endl;
}
}
Stack& operator()()
{
int scazatorul;
if (this->top <5){
scazatorul = this->top;
}
else{
scazatorul = 5;
}
Stack() {
top = -1;
myStack = new int[MAX];
}
~Stack(){
delete[] myStack;
}
};
}
return true;
}
else{
return false;
}
}
}
return false;
}
else{
return true;
}
}
bool operator<(const Stack &d1, const Stack &d2)
{
if (d1.top < d2.top){
return true;
}
else{
return false;
}
}
bool operator>(const Stack &d1, const Stack &d2)
{
if (d1.top > d2.top){
return true;
}
else{
return false;
}
}
}
istream& operator >> (istream& in, Stack &obj1)
{
int nr;
}
return in;
temp.push(7);
return 0;
}
Concluzie:
În urma acestei lucrări de laborator am avut ocazia să lucrăm cu paradigma POO,
astfel am utilizat clase. De asemenea am supraîncărcat operatorii prin diferite
metode - prietene ale clasei și membre ale clasei.