Sunteți pe pagina 1din 12

Ministerul Educaţiei al Republicii Moldova

Universitatea Tehnică a Moldovei

Catedra FCIM

Raport
Lucrarea de laborator Nr.3
Programarea orientată pe obiecte

Varianta 2

A efectuat: Grosu Maxim

A verificat: Pasat Natalia

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

b) Să se creeze clasa Matrix–matrice. Clasa conţine pointer spre int, numărul de


rînduri şi de coloane şi o variabilă –codul erorii. Să se definească
constructorul fără parametri (constructorul implicit), constructorul cu un
parametru –matrice pătrată şi constructorul cu doi parametri –matrice
dreptunghiulară ş. a. Să se definească funcţiilemembru de acces:
returnarea şi setarea valorii elementului (i,j). Să se definească funcţiile de
adunare şi scădere a două matrice; înmulţirea unei matrice cu alta; înmulţirea
unei matrice cu un număr. Să se testeze funcţionarea clasei. În caz de
insuficienţă de memorie, necorespondenţă a dimensiunilor matricelor,
depăşire a limitei memoriei utilizate să se stabilească codul erorii

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);

// Nu inteleg cum sa adun Coordonatele si numerele obisnuite

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);
}

Coordinates operator+(int value){


Coordinates obj1;
obj1.x = this->x + value;
obj1.y = this->y + value;
return obj1;
}

Coordinates& operator=(const Coordinates &obj1){


this->x = obj1.x;
this->y = obj1.y;

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 operator+(const Coordinates &obj1, const Coordinates &obj2){

Coordinates coord;

coord.x = obj1.x + obj2.x;


coord.y = obj1.y + obj2.y;
return coord;
};
Coordinates operator-(const Coordinates &obj1, const Coordinates &obj2){

Coordinates coord;

coord.x = obj1.x - obj2.x;


coord.y = obj1.y - obj2.y;
return coord;
};
int main(){
Coordinates first(1,4);
Coordinates second(2,5);

Coordinates third = first + second;


cout <<"X = "<<third.x<< " Y = "<<third.y<< endl;
Coordinates yy = third;
Coordinates tt = third + 1;

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:

b) Să se creeze clasa Stack – stivă, utilizînd memoria dinamică. Să se


definească următorii operatori ca metode ale clasei: "+" – de adunare a două
stive, "=" – de atribuire, "()" – de returnare a unei stive noi care conţine
ultimele n elemente ale stivei date. Să se definească operatorii de comparare:
"==", "!=", "<", ">" ca funcţii prietene. Pentru realizarea ultimilor doi
operatori să se definească funcţia de calcul a normei elementelor stivei. Să se
definească operatorii "<<" şi ">>" pentru ieşiri/intrări de obiecte. Clasa
trebuie să fie absolut funcţională, adică să conţină toţi constructorii necesari şi
destructorul.
#include<iostream>
using namespace std;

#define MAX 1000 //max size for stack

class Stack
{

public:
int top;
int *myStack; //stack array

bool push(int x);


int pop();
bool isEmpty();

void showStack()
{
for (int i=0; i<=this->top; i++)
{
cout << this->myStack[i]<< endl;
}
}

friend Stack operator+(const Stack &d1, const Stack &d2);


friend bool operator==(const Stack &d1, const Stack &d2);
friend bool operator!=(const Stack &d1, const Stack &d2);
friend bool operator<(const Stack &d1, const Stack &d2);
friend bool operator>(const Stack &d1, const Stack &d2);
friend ostream& operator << (ostream& os, const Stack &obj1);
friend istream& operator >> (istream& in, Stack &obj1);

Stack& operator=(const Stack &obj1)


{
this->top = obj1.top;
for (int i = 0; i<=obj1.top; i++)
{
this->myStack[i] = obj1.myStack[i];
}
}

Stack& operator()()
{
int scazatorul;

if (this->top <5){
scazatorul = this->top;
}
else{
scazatorul = 5;
}

for (int i=this->top-scazatorul; i<=this->top; i++){


cout << this->myStack[i]<< endl;
}
}

Stack(const Stack &obj){


this->top = obj.top;
for (int i=0; i<=obj.top; i++){
this->myStack[i] = obj.myStack[i];
}

Stack() {
top = -1;
myStack = new int[MAX];
}
~Stack(){
delete[] myStack;
}
};

bool operator==(const Stack &d1, const Stack &d2)


{
if (d1.top == d2.top){
for (int i=0; i<=d2.top; i++){
if (d1.myStack[i] != d2.myStack[i]){
return false;
}

}
return true;

}
else{
return false;
}
}

bool operator!=(const Stack &d1, const Stack &d2)


{
if (d1.top == d2.top){
for (int i=0; i<=d2.top; i++){
if (d1.myStack[i] != d2.myStack[i]){
return true;
}

}
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;
}
}

ostream& operator << (ostream& os, const Stack &obj1)


{
for (int i=0; i<=obj1.top; i++){
os << obj1.myStack[i]<< endl;
}
return os;

}
istream& operator >> (istream& in, Stack &obj1)
{
int nr;

cout << "Introduceti numarul de elemente ce vor fi insertate\n";


cin >> nr;
for (int i=0; i<nr; i++){
cout << "["<< i+1<<"] = ";
obj1.top ++;
in >> obj1.myStack[obj1.top];

}
return in;

//pushes element on to the stack


bool Stack::push(int item)
{
if (top >= (MAX-1)) {
cout << "Stack Overflow!!!";
return false;
}
else {
myStack[++top] = item;
return true;
}

//removes or pops elements out of the stack


int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow!!";
return 0;
}
else {
int item = myStack[top--];
return item;
}
}
//check if stack is empty
bool Stack::isEmpty()
{
return (top < 0);
}

// main program to demonstrate stack functions


int main()
{
class Stack stack;
class Stack temp;
class Stack temp2;
cout<<"The Stack Push "<<endl;
stack.push(4);
stack.push(5);
stack.push(6);

temp.push(7);

cout << "previos \n";


temp.showStack();

temp2 = stack + temp;

cout << "After :\n";


temp2.showStack();

cout << "\n\n\n";


temp2();

cout << "\n\n\n";


cout << temp2;

cout << "Din nou\n";


cin >> temp2;
cout << "Stack-ul nou\n";
cout << temp2;

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.

S-ar putea să vă placă și