Programe Verificate
Programe Verificate
#include <iostream>
#include<string.h>
using namespace std;
class Animal {
public:
char nume[50];
char specie[50];
int varsta;
bool microcipat;
Animal(const char* nume, const char* specie, int varsta, bool microcip =
false) :varsta(varsta)
{
strcpy_s(this->nume, nume);
strcpy_s(this->specie, specie);
this->microcipat = microcip;
}
private:
float consultatie()
{
int costconsultatie;
if (strcmp(this->specie, "pisica") == 0) costconsultatie = 50;
else if (strcmp(this->specie, "caine") == 0) costconsultatie = 100;
else costconsultatie = 200;
if (microcipat) { cout << "Fiind"; costconsultatie -= 10; }
else cout << "Nefiind";
return costconsultatie;
}
public:
float consultatiepub() {
return consultatie();
};
void Afisare() {
cout<<" microcipat " << nume << " este un animal din specia " <<
specie << " in varsta de " << varsta << " ani " << " care va avea costul
consultatiei de " << consultatiepub() <<" lei" << endl;
}
};
int main()
{
Animal A1("Tom", "pisica", 2, true);
A1.Afisare();
return 0;
//Problema triunghi
//Problema triunghi
#include<iostream>
#include<math.h>
using namespace std;
class Triunghi {
public:
int latura1, latura2, latura3;
static int contor;
Triunghi()
{
this->latura1 = latura1;
this->latura2 = latura2;
this->latura3 = latura3;
this->contor++;
}
Triunghi(const int l1, const int l2, const int l3) :latura1(l1), latura2(l2),
latura3(l3) { this->contor++; };
~Triunghi() { this->contor--; };
float getPerimetru()
{
return this->latura1 + this->latura2 + this->latura3;
}
float getLatura1()
{
return this->latura1;
}
void setLatura1(int l1)
{
this->latura1 = l1;
}
float getLatura2()
{
return this->latura2;
}
void setLatura2(int l2)
{
this->latura2 = l2;
}
float getLatura3()
{
return this->latura3;
}
void setLatura3(int l3) {
this->latura3 = l3;
}
float getAria() {
int s = this->getPerimetru() / 2;
return sqrt(s * (s - this->latura1) * (s - this->latura2) * (s - this-
>latura3));
}
};
int Triunghi::contor = 0;
int main()
{//Alocare dinamica
Triunghi* t1 = new Triunghi(3, 4, 5);
cout << "Latura1: " << t1->getLatura1() << endl;
cout << "Latura2 :" << t1->getLatura2() << endl;
cout << "Latura3 : " << t1->getLatura3() << endl;
cout << "Perimetru : " << t1->getPerimetru() << endl;
cout << "Aria : " << t1->getAria() << endl;
cout<<"----------------------------------------------------------------------------
---" << endl;
//Alocare statica
Triunghi t2(2, 2, 2);
cout << "Latura1: " << t2.getLatura1() << endl;
cout << "Latura2 :" << t2.getLatura2() << endl;
cout << "Latura3 : " << t2.getLatura3() << endl;
cout << "Perimetru : " << t2.getPerimetru() << endl;
cout << "Aria : " << t2.getAria() << endl;
cout <<
"_________________________________________________________________________________"
<< endl;
t2.setLatura1(3);
t2.setLatura3(3);
cout << "Latura1: " << t2.getLatura1() << endl;
cout << "Latura2 :" << t2.getLatura2() << endl;
cout << "Latura3 : " << t2.getLatura3() << endl;
cout << "Perimetru : " << t2.getPerimetru() << endl;
cout << "Aria : " << t2.getAria() << endl;
return 0;
}
//Problema pasare
#include<iostream>
using namespace std;
class Pasare
{
public:
int inaltime;
bool canta;
Pasare()
{
this->inaltime = inaltime;
this->canta = canta;
}
Pasare(int h, bool c) :inaltime(h)
{
this->canta = c;
}
Pasare(const Pasare& p)
{
this->inaltime = p.inaltime;
this->canta = p.canta;
}
int getInaltime()
{
return this->inaltime;
}
void setInaltime(int a)
{
this->inaltime = a;
}
bool getCanta()
{
return this->canta;
}
void setCanta(bool b)
{
this->canta = b;
}
//Operatorul de atribuire
Pasare& operator = (const Pasare& p)
{
this->inaltime =p. inaltime;
this->canta =p. canta;
return *this;
}
void Afisare()
{
cout << "Pasarea canta :" << this->canta << "; Are inaltimea de : " <<
this->inaltime << "; Inaltimea este stocata la adresa : " << &this->inaltime <<
endl;
}
};
int main()
{
Pasare p1;
p1.setInaltime(30);
p1.setCanta(true);
Pasare p2(20, false);
Pasare p3(p2);
Pasare p4;
p4 = p2;
p2.setInaltime(55);
p1.Afisare();
p2.Afisare();
p3.Afisare();
p4.Afisare();
return 0;
}
//Poimteri multipli
#include <iostream>
using namespace std;
int main()
{
int x=5;
cout << "Valoarea lui x:" << x << " " << " adresa lui x " << &x<<endl;
int* q;
q = &x;
cout << "Valoarea lui q:" << q << " " << " adresa lui q " << &q<< endl;
int** p;
p = &q;
cout << "Valoarea lui p :" << p << " " << " adresa lui p " << &p << endl;
int*** r;
r = &p;
cout << "Valoarea lui r:" << r << " " << " adresa lui r " << &r << endl;
**p = 9;
cout << "Valoarea lui x:" << " " << " valoarea q " << *q << endl;
cout << "Valoarea lui x:" <<" " << " valoarea p " << **p << endl;
cout << "Valoarea lui x :" << " " << " adresa lui r " << ***r << endl;
//Operatorul =
Paralelipiped& operator = (const Paralelipiped& p)
{
this->lungime = p.lungime;
this->latime = p.latime;
this->inaltime = p.inaltime;
return *this;
}
//Calculeaza volumul
int CalculeazaVolum() const
{
return this->latime * this->lungime * this->inaltime;
}
//Operatorul +
int operator + (const Paralelipiped& p)
{
return this->CalculeazaVolum() + p.CalculeazaVolum();
}
//Operatorul -
int operator -(const Paralelipiped& p)
{
return abs(this->CalculeazaVolum() - p.CalculeazaVolum());
}
//Operatorul *
Paralelipiped operator *(const Paralelipiped& p)
{
Paralelipiped result;
result.lungime = this->lungime * p.CalculeazaVolum();
result.latime = this->latime * p.CalculeazaVolum();
result.inaltime = this->inaltime * p.CalculeazaVolum();
return result;
}
//Operatorul /
bool operator / (const Paralelipiped& p)
{
return this->lungime == p.lungime * 2 && this->latime == p.latime * 2 &&
this->inaltime == p.inaltime * 2;
}
//Operatorul ++ pe clasa
}
//Operator ++ echivalet mai scurt
Paralelipiped operator ++ (int)
{
Paralelipiped t = *this;
++*this;
return t;
}
//Operator --
Paralelipiped& operator --()
{
this->lungime--;
this->latime--;
this->inaltime--;
return*this;
}
//Operator -- echivalent mai scurt
Paralelipiped operator--(int)
{
Paralelipiped t = *this;
--*this;
return t;
}
//Supraincarcare operatori >>,<<,+,-,*,/,a++,++a,b++,++b
#include<iostream>
#include<math.h>
using namespace std;
class Paralelipiped
{
private:
int lungime, latime, inaltime;
public:
// Operatorul >>
friend istream& operator >>(istream& is, Paralelipiped& p)
{
cout << "Da-ti lungimea: "; is >> p.lungime;
cout << "Da ti latimea: "; is >> p.latime;
cout << "Da ti inaltimea: "; is >> p.inaltime;
return is;
}
//Operatorul <<
friend ostream& operator <<(ostream& os, const Paralelipiped& p);
//Calculeaza volumul
int CalculeazaVolum() const
{
return this->latime * this->lungime * this->inaltime;
}
//Operatorul +
int operator + (const Paralelipiped& p)
{
return this->CalculeazaVolum() + p.CalculeazaVolum();
}
//Operatorul -
int operator -(const Paralelipiped& p)
{
return abs(this->CalculeazaVolum() - p.CalculeazaVolum());
}
//Operatorul *
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;
}
//Operator /
bool operator / (const Paralelipiped& p)
{
return this->lungime == p.lungime * 2 && this->latime == p.latime * 2
&& this->inaltime == p.inaltime * 2;
}
//Operatorul ++ pe clasa
}
//Operator ++ echivalet mai scurt
Paralelipiped operator ++ (int)
{
this->lungime++;
this->latime++;
this->inaltime++;
return *this;
}
//Operator --
Paralelipiped& operator --()
{
this->lungime--;
this->latime--;
this->inaltime--;
return*this;
}
//Operator -- echivalent mai scurt
Paralelipiped operator--(int)
{
Paralelipiped t = *this;
--*this;
return t;
}
//Operatorii +=,-=
#include<iostream>
using namespace std;
class Numbers {
public:
int a;
Numbers(int b) :a(b) {};
Numbers (const Numbers& n)
{
this->a = n.a;
}
//Operatorul+=
Numbers& operator +=(int n)
{
this->a = this-> a + n;
return*this;
}
//Operatorul-=
Numbers& operator-=(const Numbers& n)
{
this->a = this->a - n.a;
return *this;
}
};
int main()
{
Numbers b(7);
b += 7;
cout << b.a << endl;
b -= 5;
cout << b.a << endl;
return 0;
}
};
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;
cin >> p2;
cout << p1;
cout<< p2;
cout << p1 + p2 << endl;
cout << p1 - p2 << endl;
cout << p1 * p2 << endl;
cout << p1 / p2 << endl<<endl;
cout << ++p1 << endl;
cout << --p2 << endl;
}
#include<iostream>
using namespace std;
class Numbers {
public:
int* p;
int size;
Numbers(int* t, int s)
{
this->p = new int[s];
this->size = s;
for ( int i = 0; i < s; i++)
{
p[i] = t[i];
}
}
~Numbers()
{
delete[] p;
}
void Afisare()
{
for (int i = 0; i <this->size; i++)
{
cout << p[i] << endl;
cout << endl;
}
}
//Operator []
int& operator[](int index)
{
if (index<0 || index> size)
{
cout << "Index of the bounds" << endl;
exit(0);
}
return p[index];
}
};
int main()
{
int p[] = { 1,2,3,4 };
Numbers n(p, 4);
n.Afisare();
cout << n[7];
}
#include<iostream>
using namespace std;
class Liniar {
public:
int a, b;
Liniar(int a, int b)
{
this->a = a;
this->b = b;
}
//Constructor de copiere
Liniar(const Liniar& p)
{
this->a = p.a;
this->b = p.b;
}
//Operator liniar
int operator()(int x)
{
return this->a * x + this->b;
}
//Operatorul =
Liniar& operator=(const Liniar& p)
{
this->a = p.a;
this->b = p.b;
return *this;
}
};
int main()
{
Liniar l1(5, 6);
cout<<l1(2);
}
#include<iostream>
using namespace std;
class Numbers {
public:
int a, b;
Numbers(int a, int b)
{
this->a = a;
this->b = b;
}
Numbers(const Numbers& p)
{
this->a = p.a;
this->b = p.a;
}
//Operatorul !
Numbers& operator !()
{
this->a = this->a * (-1);
this->b *= -1;
return *this;
}
};
int main()
{
Numbers l1(2, 3);
!l1;
cout << l1.a<<" "<<l1.b;
}
#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;
}