This shows you the differences between two versions of the page.
|
poo-is-ab:laboratoare:07 [2025/09/23 20:04] razvan.cristea0106 |
poo-is-ab:laboratoare:07 [2025/11/08 17:18] (current) razvan.cristea0106 [Moștenirea multiplă] |
||
|---|---|---|---|
| Line 36: | Line 36: | ||
| class ProdusComercial | class ProdusComercial | ||
| { | { | ||
| - | float pret; | + | float pret; |
| public: | public: | ||
| - | ProdusComercial(const float& pret = 0.0f); | + | ProdusComercial(const float& pret = 0.0f); |
| - | ProdusComercial& operator=(const ProdusComercial& produsComercial); | + | ProdusComercial& operator=(const ProdusComercial& produsComercial); |
| - | friend std::ostream& operator<<(std::ostream& out, const ProdusComercial& produsComercial); | + | friend std::ostream& operator<<(std::ostream& out, const ProdusComercial& produsComercial); |
| }; | }; | ||
| </code> | </code> | ||
| Line 51: | Line 51: | ||
| <code cpp> | <code cpp> | ||
| #pragma once | #pragma once | ||
| - | #include <string> | + | #include <cstring> |
| #include <iostream> | #include <iostream> | ||
| class PiesaElectronica | class PiesaElectronica | ||
| { | { | ||
| - | char* brand; | + | char* brand; |
| public: | public: | ||
| - | PiesaElectronica(); | + | PiesaElectronica(); |
| - | PiesaElectronica(const char* brand); | + | PiesaElectronica(const char* brand); |
| - | PiesaElectronica(const PiesaElectronica& piesaElectronica); | + | PiesaElectronica(const PiesaElectronica& piesaElectronica); |
| - | PiesaElectronica& operator=(const PiesaElectronica& piesaElectronica); | + | PiesaElectronica& operator=(const PiesaElectronica& piesaElectronica); |
| - | ~PiesaElectronica(); | + | ~PiesaElectronica(); |
| - | friend std::ostream& operator<<(std::ostream& out, const PiesaElectronica& piesaElectronica); | + | friend std::ostream& operator<<(std::ostream& out, const PiesaElectronica& piesaElectronica); |
| }; | }; | ||
| </code> | </code> | ||
| Line 81: | Line 81: | ||
| class CameraWeb : public ProdusComercial, public PiesaElectronica // CameraWeb mosteneste atat clasa ProdusComercial cat si clasa PiesaElectronica | class CameraWeb : public ProdusComercial, public PiesaElectronica // CameraWeb mosteneste atat clasa ProdusComercial cat si clasa PiesaElectronica | ||
| { | { | ||
| - | int rezolutie; | + | int rezolutie; |
| - | char* culoare; | + | char* culoare; |
| public: | public: | ||
| - | CameraWeb(); | + | CameraWeb(); |
| - | CameraWeb(const float& pret, const char* brand, const int& rezolutie, const char* culoare); | + | CameraWeb(const float& pret, const char* brand, const int& rezolutie, const char* culoare); |
| - | CameraWeb(const CameraWeb& cameraWeb); | + | CameraWeb(const CameraWeb& cameraWeb); |
| - | CameraWeb& operator=(const CameraWeb& cameraWeb); | + | CameraWeb& operator=(const CameraWeb& cameraWeb); |
| - | ~CameraWeb(); | + | ~CameraWeb(); |
| - | friend std::ostream& operator<<(std::ostream& out, const CameraWeb& cameraWeb); | + | friend std::ostream& operator<<(std::ostream& out, const CameraWeb& cameraWeb); |
| }; | }; | ||
| </code> | </code> | ||
| Line 105: | Line 105: | ||
| CameraWeb::CameraWeb() : ProdusComercial(), PiesaElectronica() | CameraWeb::CameraWeb() : ProdusComercial(), PiesaElectronica() | ||
| { | { | ||
| - | rezolutie = 0; | + | rezolutie = 0; |
| - | culoare = nullptr; | + | culoare = nullptr; |
| } | } | ||
| </code> | </code> | ||
| Line 119: | Line 119: | ||
| CameraWeb::CameraWeb(const float& pret, const char* brand, const int& rezolutie, const char* culoare) : ProdusComercial(pret), PiesaElectronica(brand) | CameraWeb::CameraWeb(const float& pret, const char* brand, const int& rezolutie, const char* culoare) : ProdusComercial(pret), PiesaElectronica(brand) | ||
| { | { | ||
| - | if (rezolutie <= 0) | + | if (rezolutie <= 0) |
| - | { | + | { |
| - | this->rezolutie = 0; | + | this->rezolutie = 0; |
| - | } | + | } |
| - | else | + | else |
| - | { | + | { |
| - | this->rezolutie = rezolutie; | + | this->rezolutie = rezolutie; |
| - | } | + | } |
| - | if (culoare != nullptr) | + | if (culoare != nullptr) |
| - | { | + | { |
| - | this->culoare = new char[strlen(culoare) + 1]; | + | this->culoare = new char[strlen(culoare) + 1]; |
| - | strcpy(this->culoare, culoare); | + | strcpy(this->culoare, culoare); |
| - | } | + | } |
| - | else | + | else |
| - | { | + | { |
| - | this->culoare = nullptr; | + | this->culoare = nullptr; |
| - | } | + | } |
| } | } | ||
| </code> | </code> | ||
| Line 145: | Line 145: | ||
| CameraWeb::CameraWeb(const CameraWeb& cameraWeb) : ProdusComercial(cameraWeb), PiesaElectronica(cameraWeb) | CameraWeb::CameraWeb(const CameraWeb& cameraWeb) : ProdusComercial(cameraWeb), PiesaElectronica(cameraWeb) | ||
| { | { | ||
| - | if (cameraWeb.rezolutie <= 0) | + | if (cameraWeb.rezolutie <= 0) |
| - | { | + | { |
| - | rezolutie = 0; | + | rezolutie = 0; |
| - | } | + | } |
| - | else | + | else |
| - | { | + | { |
| - | rezolutie = cameraWeb.rezolutie; | + | rezolutie = cameraWeb.rezolutie; |
| - | } | + | } |
| - | if (cameraWeb.culoare != nullptr) | + | if (cameraWeb.culoare != nullptr) |
| - | { | + | { |
| - | culoare = new char[strlen(cameraWeb.culoare) + 1]; | + | culoare = new char[strlen(cameraWeb.culoare) + 1]; |
| - | strcpy(culoare, cameraWeb.culoare); | + | strcpy(culoare, cameraWeb.culoare); |
| - | } | + | } |
| - | else | + | else |
| - | { | + | { |
| - | culoare = nullptr; | + | culoare = nullptr; |
| - | } | + | } |
| } | } | ||
| </code> | </code> | ||
| Line 175: | Line 175: | ||
| CameraWeb& CameraWeb::operator=(const CameraWeb& cameraWeb) | CameraWeb& CameraWeb::operator=(const CameraWeb& cameraWeb) | ||
| { | { | ||
| - | if (this == &cameraWeb) | + | if (this == &cameraWeb) |
| - | { | + | { |
| - | return *this; | + | return *this; |
| - | } | + | } |
| - | ProdusComercial::operator=(cameraWeb); | + | ProdusComercial::operator=(cameraWeb); |
| - | PiesaElectronica::operator=(cameraWeb); | + | PiesaElectronica::operator=(cameraWeb); |
| - | if (culoare != nullptr) | + | if (culoare != nullptr) |
| - | { | + | { |
| - | delete[] culoare; | + | delete[] culoare; |
| - | } | + | } |
| - | if (cameraWeb.rezolutie <= 0) | + | if (cameraWeb.rezolutie <= 0) |
| - | { | + | { |
| - | rezolutie = 0; | + | rezolutie = 0; |
| - | } | + | } |
| - | else | + | else |
| - | { | + | { |
| - | rezolutie = cameraWeb.rezolutie; | + | rezolutie = cameraWeb.rezolutie; |
| - | } | + | } |
| - | if (cameraWeb.culoare != nullptr) | + | if (cameraWeb.culoare != nullptr) |
| - | { | + | { |
| - | culoare = new char[strlen(cameraWeb.culoare) + 1]; | + | culoare = new char[strlen(cameraWeb.culoare) + 1]; |
| - | strcpy(culoare, cameraWeb.culoare); | + | strcpy(culoare, cameraWeb.culoare); |
| - | } | + | } |
| - | else | + | else |
| - | { | + | { |
| - | culoare = nullptr; | + | culoare = nullptr; |
| - | } | + | } |
| - | return *this; | + | return *this; |
| } | } | ||
| </code> | </code> | ||
| Line 216: | Line 216: | ||
| CameraWeb::~CameraWeb() | CameraWeb::~CameraWeb() | ||
| { | { | ||
| - | if (culoare != nullptr) | + | if (culoare != nullptr) |
| - | { | + | { |
| - | delete[] culoare; | + | delete[] culoare; |
| - | } | + | } |
| } | } | ||
| </code> | </code> | ||
| Line 232: | Line 232: | ||
| std::ostream& operator<<(std::ostream& out, const CameraWeb& cameraWeb) | std::ostream& operator<<(std::ostream& out, const CameraWeb& cameraWeb) | ||
| { | { | ||
| - | operator<<(out, (ProdusComercial&)cameraWeb); | + | operator<<(out, (ProdusComercial&)cameraWeb); |
| - | operator<<(out, (PiesaElectronica&)cameraWeb); | + | operator<<(out, (PiesaElectronica&)cameraWeb); |
| - | out << "Rezolutia camerei web este: " << cameraWeb.rezolutie << " pixeli\n"; | + | out << "Rezolutia camerei web este: " << cameraWeb.rezolutie << " pixeli\n"; |
| - | out << "Culoarea camerei web este: "; | + | out << "Culoarea camerei web este: "; |
| - | if (cameraWeb.culoare != nullptr) | + | if (cameraWeb.culoare != nullptr) |
| - | { | + | { |
| - | out << cameraWeb.culoare << '\n'; | + | out << cameraWeb.culoare << '\n'; |
| - | } | + | } |
| - | else | + | else |
| - | { | + | { |
| - | out << "N/A\n"; | + | out << "N/A\n"; |
| - | } | + | } |
| - | return out; | + | return out; |
| } | } | ||
| </code> | </code> | ||
| Line 270: | Line 270: | ||
| class Laptop | class Laptop | ||
| { | { | ||
| - | double pret; | + | double pret; |
| - | CameraWeb cameraWeb; // relatia de has-a | + | CameraWeb cameraWeb; // relatia de has-a |
| public: | public: | ||
| - | Laptop(); | + | Laptop(); |
| - | Laptop(const double& pret, const CameraWeb& cameraWeb); | + | Laptop(const double& pret, const CameraWeb& cameraWeb); |
| - | friend std::ostream& operator<<(std::ostream& out, const Laptop& laptop); | + | friend std::ostream& operator<<(std::ostream& out, const Laptop& laptop); |
| }; | }; | ||
| </code> | </code> | ||
| - | Iar implementările metodelor și a operatorului de afișare pentru clasa **Laptop** le putem observa în codul de mai jos. | + | Iar implementările metodelor și a operatorului de afișare pentru clasa **Laptop** le putem observa în blocul de cod de mai jos. |
| <code cpp> | <code cpp> | ||
| Laptop::Laptop() | Laptop::Laptop() | ||
| { | { | ||
| - | pret = 0.0; | + | pret = 0.0; |
| - | cameraWeb = CameraWeb(); | + | cameraWeb = CameraWeb(); // se foloseste operatorul egal din clasa CameraWeb |
| } | } | ||
| Laptop::Laptop(const double& pret, const CameraWeb& cameraWeb) | Laptop::Laptop(const double& pret, const CameraWeb& cameraWeb) | ||
| { | { | ||
| - | if (pret <= 0.0) | + | if (pret <= 0.0) |
| - | { | + | { |
| - | this->pret = 0.0; | + | this->pret = 0.0; |
| - | } | + | } |
| - | else | + | else |
| - | { | + | { |
| - | this->pret = pret; | + | this->pret = pret; |
| - | } | + | } |
| - | this->cameraWeb = cameraWeb; | + | this->cameraWeb = cameraWeb; // se foloseste operatorul egal din clasa CameraWeb |
| } | } | ||
| std::ostream& operator<<(std::ostream& out, const Laptop& laptop) | std::ostream& operator<<(std::ostream& out, const Laptop& laptop) | ||
| { | { | ||
| - | out << "Pretul laptopului este: " << laptop.pret << " ron\n"; | + | out << "Pretul laptopului este: " << laptop.pret << " ron\n"; |
| - | out << "\nDetalii despre camera web a laptopului\n\n"; | + | out << "\nDetalii despre camera web a laptopului\n\n"; |
| - | out << laptop.cameraWeb; | + | out << laptop.cameraWeb; |
| - | return out; | + | return out; |
| } | } | ||
| </code> | </code> | ||
| Line 326: | Line 326: | ||
| #pragma once | #pragma once | ||
| - | namespace MathHelper | + | namespace MathHelper |
| { | { | ||
| int rest(const int& a, const int& b); | int rest(const int& a, const int& b); | ||
| Line 343: | Line 343: | ||
| int MathHelper::rest(const int& a, const int& b) | int MathHelper::rest(const int& a, const int& b) | ||
| { | { | ||
| - | return (b == 0) ? -1 : a % b; | + | return (b == 0) ? -1 : a % b; |
| } | } | ||
| int MathHelper::adunare(const int& a, const int& b) | int MathHelper::adunare(const int& a, const int& b) | ||
| { | { | ||
| - | return a + b; | + | return a + b; |
| } | } | ||
| int MathHelper::diferenta(const int& a, const int& b) | int MathHelper::diferenta(const int& a, const int& b) | ||
| { | { | ||
| - | return a - b; | + | return a - b; |
| } | } | ||
| int MathHelper::inmultire(const int& a, const int& b) | int MathHelper::inmultire(const int& a, const int& b) | ||
| { | { | ||
| - | return a * b; | + | return a * b; |
| } | } | ||
| int MathHelper::impartire(const int& a, const int& b) | int MathHelper::impartire(const int& a, const int& b) | ||
| { | { | ||
| - | return (b == 0) ? 0 : a / b; | + | return (b == 0) ? 0 : a / b; |
| } | } | ||
| </code> | </code> | ||
| Line 374: | Line 374: | ||
| int main() | int main() | ||
| { | { | ||
| - | int x = 10; | + | int x = 10; |
| - | int y = 5; | + | int y = 5; |
| - | std::cout << "Suma numerelor este: " << MathHelper::adunare(x, y) << '\n'; | + | std::cout << "Suma numerelor este: " << MathHelper::adunare(x, y) << '\n'; |
| - | std::cout << "Diferenta numerelor este: " << MathHelper::diferenta(x, y) << '\n'; | + | std::cout << "Diferenta numerelor este: " << MathHelper::diferenta(x, y) << '\n'; |
| - | std::cout << "Produsul numerelor este: " << MathHelper::inmultire(x, y) << '\n'; | + | std::cout << "Produsul numerelor este: " << MathHelper::inmultire(x, y) << '\n'; |
| - | std::cout << "Rezultatul impartirii numerelor este: " << MathHelper::impartire(x, y) << '\n'; | + | std::cout << "Rezultatul impartirii numerelor este: " << MathHelper::impartire(x, y) << '\n'; |
| - | std::cout << "Restul impartirii numerelor este: " << MathHelper::rest(x, y) << '\n'; | + | std::cout << "Restul impartirii numerelor este: " << MathHelper::rest(x, y) << '\n'; |
| - | return 0; | + | return 0; |
| } | } | ||
| </code> | </code> | ||