Documente Academic
Documente Profesional
Documente Cultură
RAPORT
Lucrare de laborator nr.4
la
Programarea orientată pe obiecte
Chișinău 2020
Lucrare de laborator nr.4
b) Să se creeze clasa roata, care conţine rază. Determinaţi constructorii şi metodele de acces.
Creaţi clasa automobil, care conţine roţi şi un câmp care reprezintă firma producătoare. Creaţi o
clasă derivată autocamion care se deosebeşte prin tonaj. Determinaţi constructorii, destructorul şi
alte funcţii necesare
Listingul programului (a)
#include <iostream>
#include <utility>
class Joc {
public:
enum TipJoc {
FIZIC,
INTELECTUAL
};
private:
TipJoc tipJoc;
public:
explicit Joc(TipJoc tipJoc = INTELECTUAL) : tipJoc(tipJoc) {
std::cout << "Obiect Joc construit!" << std::endl;
}
~Joc() {
std::cout << "Obiect Joc distrus!" << std::endl;
}
TipJoc getTipJoc() const {
return tipJoc;
}
void setTipJoc(TipJoc tipJoc) {
Joc::tipJoc = tipJoc;
}
friend std::ostream &operator<<(std::ostream &os, const Joc &joc) {
os << "tipJoc: ";
if (joc.tipJoc == FIZIC)os << "FIZIC";
if (joc.tipJoc == INTELECTUAL)os << "INTELECTUAL";
os << std::endl;
return os;
}
};
class JocSportiv : public Joc {
public:
enum TipJocSportiv {
MASA,
TEREN
};
private:
TipJocSportiv tipJocSportiv;
public:
explicit JocSportiv(TipJocSportiv tipJocSportiv = MASA) : Joc(FIZIC), tipJocSport
iv(tipJocSportiv) {
std::cout << "Obiect JocSportiv construit!" << std::endl;
}
~JocSportiv() {
std::cout << "Obiect JocSportiv distrus!" << std::endl;
}
TipJocSportiv getTipJocSportiv() const {
return tipJocSportiv;
}
void setTipJocSportiv(TipJocSportiv tipJocSportiv) {
JocSportiv::tipJocSportiv = tipJocSportiv;
}
friend std::ostream &operator<<(std::ostream &os, const JocSportiv &sportiv) {
os << static_cast<const Joc &>(sportiv) << " tipJocSportiv: ";
if (sportiv.tipJocSportiv == TEREN)os << "TEREN";
if (sportiv.tipJocSportiv == MASA)os << "MASA";
os << std::endl;
return os;
}
};
class Volei : public JocSportiv {
protected:
enum TipVolei {
CLASIC,
PLAJA
};
private:
TipVolei tipVolei;
public:
explicit Volei(TipVolei tipVolei = CLASIC) : JocSportiv(TEREN) {
std::cout << "Obiect Volei construit!" << std::endl;
}
~Volei() {
std::cout << "Obiect Volei distrus!" << std::endl;
}
friend std::ostream &operator<<(std::ostream &os, const Volei &volei) {
os << static_cast<const JocSportiv &>(volei) << " tipVolei: ";
if (volei.tipVolei == CLASIC)os << "CLASIC";
if (volei.tipVolei == PLAJA)os << "PLAJA";
os << std::endl;
return os;
}
};
int main() {
Volei volei;
std::cout << volei << std::endl;
std::cout << (JocSportiv) volei << std::endl;
std::cout << (Joc) volei << std::endl;
std::cout << std::endl << "Punct b" << std::endl;
}
class Roata {
float raza;
public:
explicit Roata(float raza = 0) : raza(raza) {
std::cout << "Obiect Roata creat" << std::endl;
}
virtual ~Roata() {
std::cout << "Obiect Roata ditrus" << std::endl;
}
float getRaza() const {
return raza;
}
void setRaza(float raza) {
Roata::raza = raza;
}
friend std::ostream &operator<<(std::ostream &os, const Roata &roata) {
os << "raza: " << roata.raza << std::endl;
return os;
}
};
class Automobil {
Roata roti[4];
std::string firma;
public:
explicit Automobil(float razaRoti = 0, std::string firma = "") : firma(std::move(
firma)) {
std::cout << "Obiect Automobil creat" << std::endl;
for (Roata &roata:roti) {
roata.setRaza(razaRoti);
}
}
~Automobil() {
std::cout << "Obiect Automobil distrus" << std::endl;
}
const Roata *getRoti() const {
return roti;
}
void setRazaRoti(float razaRoti) {
for (Roata &roata:roti) {
roata.setRaza(razaRoti);
}
}
const std::string &getFirma() const {
return firma;
}
void setFirma(const std::string &firma) {
Automobil::firma = firma;
}
friend std::ostream &operator<<(std::ostream &os, const Automobil &automobil) {
os << "roti: " << std::endl;
for (const Roata &roata:automobil.roti) {
os << roata;
}
os << " firma: " << automobil.firma << std::endl;
return os;
}
};
class Autocamion : public Automobil {
float tonaj;
public:
explicit Autocamion(float tonaj = 0, float razaRoti = 0, std::string firma = "")
: tonaj(tonaj),
Automobil(razaRoti,
std::move(firma)) {
std::cout << "Obiect Autocamion creat" << std::endl;
}
~Autocamion() {
std::cout << "Obiect Autocamion distrus" << std::endl;
}
float getTonaj() const {
return tonaj;
}
void setTonaj(float tonaj) {
Autocamion::tonaj = tonaj;
}
friend std::ostream &operator<<(std::ostream &os, const Autocamion &autocamion) {
os << static_cast<const Automobil &>(autocamion) << " tonaj: " << autocamion.
tonaj;
return os;
}
};
int main() {
Autocamion autocamion(8.75, 0.4, "ISUZU");
std::cout << autocamion << std::endl;
}