Sunteți pe pagina 1din 13

//Laboratorul 2 problema consultatiei

#include <iostream>
#include<string.h>
using namespace std;
class Animal {
public:
char nume[50];
char specie[50];
int varsta;
bool Aremicrocip;
private:
int consultatie()
{
int costconsultatie;
if (strcmp(specie,"pisica")==0) costconsultatie = 50;
else if (strcmp(specie,"caine") == 0 ) costconsultatie = 100;
else costconsultatie = 200;
if (Aremicrocip)
costconsultatie -= 10;
return costconsultatie;
}
public:
Animal(const char nume[50],const char specie[50], int varsta, bool
Aremicrocip = false) :varsta(varsta)
{
strcpy_s(this->nume,nume);
strcpy_s(this->specie,specie);
this->Aremicrocip = Aremicrocip;
}
int consultatiepub() { return consultatie(); };
const char* getnume() const
{
return nume;
}
const char* getspecie() const
{
return specie;
}
const int getvarsta()const
{
return varsta;
}
};

int main()
{
Animal A1("Tom", "pisica", 2, true);
cout << "Constul consultatiei pentru " << A1.getnume() << " specia " <<
A1.getspecie() << " in varsta de " << A1.getvarsta() << " ani va avea costul
consultatiei de " << A1.consultatiepub()<<"lei";

};

//Laboratorul 3
#include<iostream>
#include<math.h>
using namespace std;
class Triunghi
{

private:
float latura1, latura2, latura3;
public:
static int counter;
Triunghi(float l1, float l2, float l3) : latura1(l1), latura2(l2),
latura3(l3)
{
this->counter++;
}

~Triunghi()
{
this->counter--;
}

float GetPerimetru()
{
return this->latura1 + this->latura2 + this->latura3;
}

void setLatura1(float l1)


{
this->latura1 = l1;
}

float getLatura1()
{
return this->latura1;
}

void setLatura2(float l2)


{
this->latura2 = l2;
}

float getLatura2()
{
return this->latura2;
}

void setLatura3(float l3)


{
this->latura3 = l3;
}

float getLatura3()
{
return this->latura3;
}

//s - semiperimetru
//radical(s * (s - latura1) * (s - latura2) * (s - latura3))
float GetAria()
{
float s = this->GetPerimetru() / 2;
return sqrt(s * (s - this->latura1) * (s - this->latura2) * (s - this-
>latura3));
}
};

int Triunghi::counter = 0;

int main()
{
cout << "Triunghi alocat dinamic" << endl;
cout << "-------------------------------" << endl;
//triunghi alocat dinamic
Triunghi* t = new Triunghi(3, 4, 5);

cout << "Perimetrul: " << t->GetPerimetru() << endl;


cout << "Aria: " << t->GetAria() << endl;
cout << "Instante: " << t->counter << endl<<endl;

cout << "Triunghi alocat static" << endl;


cout << "-------------------------------" << endl;

//ATENTIE, valori invalide pentru laturile unui triunghi!


//Laturile sunt modificate cu ajutorul functiilor setter, mai jos
Triunghi t2(1, 3, 5);

t2.setLatura1(3);
t2.setLatura2(5);
cout << "Perimetrul: " << t2.GetPerimetru() << endl;
cout << "Aria: " << t2.GetAria() << endl;
cout << "Instante: " << t2.counter << endl<<endl;

cout << "Dealocarea triunghiului alocat dinamic" << endl;


cout << "-------------------------------" << endl;
delete t;
cout << "Instante: " << t2.counter << endl << endl;

return 0;
}

//Laboratorul 4 part 1
#include <iostream>
using namespace std;

int main()
{
int x = 5;
cout << "Valorea lui x: " << x << "; Adresa lui x: " << &x << endl;

int* p;
p = &x;

cout << "Valoarea lui p: " << p << "; Adresa lui p: " << &p << endl;

int** q;
q = &p;

cout << "Valoarea lui q: " << q << "; Adresa lui q: " << &q << endl;
int*** r;
r = &q;

cout << "Valoarea lui r: " << r << "; Adresa lui r: " << &r << endl;

cout << "Valoarea lui x: " << x << endl;


cout << "Valoarea lui x folosind p: " << *p << endl;
cout << "Valoarea lui x folosind q: " << **q << endl;
cout << "Valoarea lui x folodind r: " << ***r << endl;

**q = 9;

cout << "Valoarea lui x: " << x << endl;


cout << "Valoarea lui x folosind p: " << *p << endl;
cout << "Valoarea lui x folosind q: " << **q << endl;
cout << "Valoarea lui x folodind r: " << ***r << endl;
}

//partea 2
#include <iostream>
using namespace std;

class Pasare {
private:
int inaltime;
bool canta;

public:
Pasare() {
this->inaltime = 0;
this->canta = false;
}

Pasare(int inaltime,bool c):canta(c)


{
this->inaltime = inaltime;
/*this->canta = canta;*/
}
//Constructor copiere
Pasare(const Pasare& p)
{
this->inaltime = p.inaltime;
this->canta = p.canta;
}

int getInaltime() {
return this->inaltime;
}

bool getCanta(){
return this->canta;
}

void setInaltime(int inaltime) {


this->inaltime = inaltime;
}
void setCanta(bool canta) {
this->canta = canta;
}

Pasare& operator = (const Pasare& p)


{
this->inaltime = p.inaltime;
this->canta = p.canta;
return *this;
}

void Afisaza()
{
cout << "Pasarea canta: " << this->canta << "; Are inaltimea: " <<
this->inaltime
<< "; Adresa inaltime: " << &this->inaltime << endl;
}
};

int main()
{
Pasare p1;
p1.setCanta(false);
p1.setInaltime(15);

Pasare p2(20, true);

Pasare p3 = p2; //Pasare p3(p2);

Pasare p4;
p4 = p2;

p2.setInaltime(55);

p1.Afisaza();
p2.Afisaza();
p3.Afisaza();
p4.Afisaza();
}

//Laboratorul 5
#include <iostream>
#include <math.h>
using namespace std;

class Paralelipiped
{
private:
int lungime, latime, inaltime;
public:
friend istream& operator >> (istream& is, Paralelipiped& p)
{
cout << "Dati lungime: ";
is >> p.lungime;
cout << "Dati latime: ";
is >> p.latime;
cout << "Dati inaltime: ";
is >> p.inaltime;
return is;
}

friend ostream& operator << (ostream& os, const Paralelipiped& p);

int CalculeazaVolum() const


{
return this->lungime * this->latime * this->inaltime;
}

int operator + (const Paralelipiped& p)


{
return this->CalculeazaVolum() + p.CalculeazaVolum();
}

int operator - (const Paralelipiped& p)


{
return abs(this->CalculeazaVolum() - p.CalculeazaVolum());
}

Paralelipiped operator*(const Paralelipiped& p)


{
Paralelipiped result;
int volum = p.CalculeazaVolum();
result.lungime = this->lungime * volum;
result.latime = this->latime * volum;
result.inaltime = this->inaltime * volum;
return result;
}

bool operator / (const Paralelipiped& p)


{
return this->lungime == p.lungime * 2 &&
this->latime == p.latime * 2 &&
this->inaltime == p.inaltime * 2;
}

Paralelipiped& operator++()
{
this->lungime++;
this->latime++;
this->inaltime++;
return *this;
}

Paralelipiped operator++(int)
{
Paralelipiped temp = *this;
++*this;
//echivalent cu:
//this->lungime++;
//this->latime++;
//this->inaltime++;
return temp;
}
Paralelipiped& operator--()
{
this->lungime--;
this->latime--;
this->inaltime--;
return *this;
}

Paralelipiped operator--(int)
{
Paralelipiped temp = *this;
--* this;
//echivalent cu:
//this->lungime--;
//this->latime--;
//this->inaltime--;
return temp;
}
};

ostream& operator << (ostream& os, const Paralelipiped& p)


{
os << "Lungime: " << p.lungime << endl;
os << "Latime: " << p.latime << endl;
os << "Inaltime: " << p.inaltime << endl;
os << "Volum: " << p.CalculeazaVolum() << endl << endl;
return os;
}

int main()
{
Paralelipiped p1, p2;
cin >> p1 >> p2;
cout << p1 << p2;
cout << p1 + p2 << endl;
cout << p1 - p2 << endl;
cout << p1 * p2 << endl;
cout << p1 / p2 << endl;
cout << ++p1 << endl;
cout << p1++ << endl;
cout << p1;
}

//Laboratorul 6
//Operatorul cast
#include <iostream>
using namespace std;

class Cent
{
public:
int v;
Cent(int v)
{
this->v = v;
}

operator int()
{
return this->v;
}
};

class Dolar
{
public:
int d;
Dolar(int d)
{
this->d = d;
}

operator Cent()
{
return Cent(this->d * 100);
}
};

int main()
{
Cent c(40);
cout << c;

Dolar d(11);
cout << (Cent)d;
}

//operatorul ->
#include <iostream>
using namespace std;

class BaseClass
{
public:
int a;
BaseClass(int a)
{
this->a = a;
}
};

class Level1
{
public:
BaseClass* baseClass;
BaseClass* operator ->()
{
return baseClass;
}
};

class Level2
{
public:
Level1* level1;
Level1& operator ->()
{
return *level1;
}
};

class Level3
{
public:
Level2* level2;
Level2& operator ->()
{
return *level2;
}
};

int main()
{
BaseClass w(6);
cout << "Din w: " << w.a << endl;

Level1 x;
x.baseClass = &w;
cout << "Din x: " << x->a << endl;

Level2 y;
y.level1 = &x;
cout << "Din y: " << y->a << endl;

Level3 z;
z.level2 = &y;
cout << "Din z: " << z->a << endl;

y->a = 11;

cout << "Din w: " << w.a << endl;


cout << "Din x: " << x->a << endl;
cout << "Din y: " << y->a << endl;
cout << "Din z: " << z->a << endl;
}

//operator !
#include <iostream>
using namespace std;

class SelfChanging
{
public:
int a, b;
SelfChanging(int a, int b)
{
this->a = a;
this->b = b;
}

SelfChanging& operator !()


{
this->a = this->a * (-1);
this->b *= -1;
return *this;
}
};

class Returnable
{
public:
int a, b;
Returnable(int a, int b)
{
this->a = a;
this->b = b;
}

Returnable operator !()


{
Returnable temp(this->a * (-1), this->b * (-1));
return temp;
}
};

int main()
{
SelfChanging s(2, 6);
cout << s.a << " " << s.b << endl;
!s;
cout << s.a << " " << s.b << endl << endl;

Returnable r1(5, 7);


cout << r1.a << " " << r1.b << endl;
Returnable r2 = !r1;
cout << r1.a << " " << r1.b << endl;
cout << r2.a << " " << r2.b << endl;
}
//opetor ()
#include <iostream>
using namespace std;

class Liniar
{
public:
int a, b;
Liniar(int a, int b)
{
this->a = a;
this->b = b;
}

int operator()(int x)
{
return this->a * x + this->b;
}
};

int main()
{
Liniar l1(5, 6);
cout << l1(2);
}
//Index operator
#include <iostream>
using namespace std;

class Numbers
{
public:
int* p;
int size;
Numbers(int* ptr, int s)
{
this->p = new int[s];
this->size = s;
for (int i = 0; i < s; i++)
{
p[i] = ptr[i];
}
}

~Numbers()
{
delete[] p;
}

void Print()
{
for (int i = 0; i < this->size; i++)
cout << p[i] << " ";
cout << endl;
}

int& operator[](int index)


{
if (index < 0 || index > this->size)
{
cout << "Index out of bounds" << endl;
exit(0);
}
return p[index];
}
};

int main()
{
int v[] = { 1,2,3,4 };
Numbers n(v, 4);
n.Print();
cout << n[7];
}

//operatorul []
#include <iostream>
using namespace std;

class Numbers
{
public:
int* p;
int size;
Numbers(int* ptr, int s)
{
this->p = new int[s];
this->size = s;
for (int i = 0; i < s; i++)
{
p[i] = ptr[i];
}
}

~Numbers()
{
delete[] p;
}

void Print()
{
for (int i = 0; i < this->size; i++)
cout << p[i] << " ";
cout << endl;
}

int& operator[](int index)


{
if (index < 0 || index > this->size)
{
cout << "Index out of bounds" << endl;
exit(0);
}
return p[index];
}
};

int main()
{
int v[] = { 1,2,3,4 };
Numbers n(v, 4);
n.Print();
cout << n[7];
}

//operatorul +=,-=,-
#include <iostream>
using namespace std;

class Numbers
{
public:
int a;
Numbers(int a)
{
this->a = a;
}

Numbers& operator+=(int x)
{
this->a = this->a + x;
return *this;
}

Numbers& operator-=(const Numbers& x)


{
this->a -= x.a;
return *this;
}
};

int main()
{
Numbers x(6);
cout << x.a << endl;
x += 7;
cout << x.a << endl;

Numbers y(3);
x -= y;
cout << x.a << endl;
}

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