Differences

This shows you the differences between two versions of the page.

Link to this comparison view

poo-is-ab:laboratoare:08 [2025/09/23 20:06]
razvan.cristea0106
poo-is-ab:laboratoare:08 [2025/11/16 12:40] (current)
razvan.cristea0106
Line 41: Line 41:
 int adunare(const int& a, const int& b) int adunare(const int& a, const int& b)
 { {
- return a + b;+    ​return a + b;
 } }
  
 float adunare(const float& a, const float& b) float adunare(const float& a, const float& b)
 { {
- return a + b;+    ​return a + b;
 } }
  
 int main() int main()
 { {
- int sumaIntregi = adunare(2, 3); +    ​int sumaIntregi = adunare(2, 3); 
- float sumaFloaturi = adunare(3.5f,​ 8.25f);+    float sumaFloaturi = adunare(3.5f,​ 8.25f);
  
- std::cout << "Suma numerelor intregi este: " << sumaIntregi << '​\n';​ +    ​std::cout << "Suma numerelor intregi este: " << sumaIntregi << '​\n';​ 
- std::cout << "Suma numerelor reale este: " << sumaFloaturi << '​\n';​+    std::cout << "Suma numerelor reale este: " << sumaFloaturi << '​\n';​
  
- return 0;+    ​return 0;
 } }
 </​code>​ </​code>​
Line 71: Line 71:
 T adunare(const T& a, const T& b) T adunare(const T& a, const T& b)
 { {
- return a + b;+    ​return a + b;
 } }
 </​code>​ </​code>​
Line 85: Line 85:
 T adunare(const T& a, const T& b) T adunare(const T& a, const T& b)
 { {
- return a + b;+    ​return a + b;
 } }
  
 int main() int main()
 { {
- int sumaIntregi = adunare(2, 3); +    ​int sumaIntregi = adunare(2, 3); 
- /*int sumaIntregi = adunare<​int>​(2,​ 3); // corect si asa deoarece este pus explicit*/+    /*int sumaIntregi = adunare<​int>​(2,​ 3); // corect si asa deoarece este pus explicit*/
  
- /*float sumaFloaturi = adunare(3.5f,​ 8.25f); // valid*/ +    ​/*float sumaFloaturi = adunare(3.5f,​ 8.25f); // valid*/ 
- float sumaFloaturi = adunare<​float>​(3.5f,​ 8.25f); // valid+    float sumaFloaturi = adunare<​float>​(3.5f,​ 8.25f); // valid
  
- std::cout << "Suma numerelor intregi este: " << sumaIntregi << '​\n';​ +    ​std::cout << "Suma numerelor intregi este: " << sumaIntregi << '​\n';​ 
- std::cout << "Suma numerelor reale este: " << sumaFloaturi << '​\n';​+    std::cout << "Suma numerelor reale este: " << sumaFloaturi << '​\n';​
  
- return 0;+    ​return 0;
 } }
 </​code>​ </​code>​
Line 109: Line 109:
 T1 adunare(const T2& a, const T3& b) T1 adunare(const T2& a, const T3& b)
 { {
- return (T1)a + b;+    ​return (T1)a + b;
 } }
 </​code>​ </​code>​
Line 121: Line 121:
 T1 adunare(const T2& a, const T3& b) T1 adunare(const T2& a, const T3& b)
 { {
- return (T1)a + b;+    ​return (T1)a + b;
 } }
  
 int main() int main()
 { {
- double suma = adunare<​double>​(2,​ 7.5f); +    ​double suma = adunare<​double>​(2,​ 7.5f); 
- /*double suma = adunare<​double,​ int, float>​(2,​ 7.5f); // corect de asemenea*/​ +    /*double suma = adunare<​double,​ int, float>​(2,​ 7.5f); // corect de asemenea*/​ 
- /*double suma = adunare(2, 7.5f); // incorect deoarece compilatorul nu stie ce tip de return sa utilizeze*/+    /*double suma = adunare(2, 7.5f); // incorect deoarece compilatorul nu stie ce tip de return sa utilizeze*/
  
- std::cout << "Suma este: " << suma << '​\n';​+    ​std::cout << "Suma este: " << suma << '​\n';​
  
- return 0;+    ​return 0;
 } }
 </​code>​ </​code>​
Line 152: Line 152:
 void interschimbare(T&​ x, T& y) void interschimbare(T&​ x, T& y)
 { {
- T aux = x; +    ​T aux = x; 
- x = y; +    x = y; 
- y = aux;+    y = aux;
 } }
  
Line 160: Line 160:
 void interschimbare(T* x, T* y) void interschimbare(T* x, T* y)
 { {
- if (x == nullptr || y == nullptr) +    ​if (x == nullptr || y == nullptr) 
-+    
- return; +        return; 
- }+    }
  
- T aux = *x; +    ​T aux = *x; 
- *x = *y; +    *x = *y; 
- *y = aux;+    *y = aux;
 } }
  
 int main() int main()
 { {
- int a = 22; +    ​int a = 22; 
- int b = 6;+    int b = 6;
  
- interschimbare(a,​ b);+    ​interschimbare(a,​ b);
  
- std::cout << "a = " << a << '​\n';​ +    ​std::cout << "a = " << a << '​\n';​ 
- std::cout << "b = " << b << '​\n';​+    std::cout << "b = " << b << '​\n';​
  
- interschimbare(&​a,​ &b);+    ​interschimbare(&​a,​ &b);
  
- std::cout << "\na = " << a << '​\n';​ +    ​std::cout << "\na = " << a << '​\n';​ 
- std::cout << "b = " << b << '​\n';​+    std::cout << "b = " << b << '​\n';​
  
- return 0;+    ​return 0;
 } }
 </​code>​ </​code>​
Line 221: Line 221:
 T adunare(const T& x, const T& y) T adunare(const T& x, const T& y)
 { {
- return x + y;+    ​return x + y;
 } }
 </​code>​ </​code>​
Line 234: Line 234:
 int main() int main()
 { {
- int a = 22; +    ​int a = 22; 
- int b = 6;+    int b = 6;
  
- std::cout << adunare(a, b) << '​\n';​+    ​std::cout << adunare(a, b) << '​\n';​
  
- return 0;+    ​return 0;
 } }
 </​code>​ </​code>​
Line 261: Line 261:
 T adunare(const T& x, const T& y) T adunare(const T& x, const T& y)
 { {
- return x + y;+    ​return x + y;
 } }
  
 void testare() void testare()
 { {
- int s1 = adunare(2, 3); +    ​int s1 = adunare(2, 3); 
- float s2 = adunare(2.3f,​ 3.0f); +    float s2 = adunare(2.3f,​ 3.0f); 
- double s3 = adunare(-1.4,​ 8.24); +    double s3 = adunare(-1.4,​ 8.24); 
- unsigned int s4 = adunare(2u, 3u);+    unsigned int s4 = adunare(2u, 3u);
 } }
 </​code>​ </​code>​
Line 280: Line 280:
 int main() int main()
 { {
- std::cout << "Suma numerelor este: " << adunare(22, 8) << '​\n';​ // valid +    ​std::cout << "Suma numerelor este: " << adunare(22, 8) << '​\n';​ // valid 
- std::cout << "Suma numerelor este: " << adunare(2.2f,​ 4.5f) << '​\n';​ // valid +    std::cout << "Suma numerelor este: " << adunare(2.2f,​ 4.5f) << '​\n';​ // valid 
- std::cout << "Suma numerelor este: " << adunare(10.0,​ 7.5) << '​\n';​ // valid +    std::cout << "Suma numerelor este: " << adunare(10.0,​ 7.5) << '​\n';​ // valid 
- std::cout << "Suma numerelor este: " << adunare(4u, 6u) << '​\n';​ // valid +    std::cout << "Suma numerelor este: " << adunare(4u, 6u) << '​\n';​ // valid 
- /​*std::​cout << "Suma numerelor este: " << adunare(4l, 6l) << '​\n';​ // invalid*/+    /*std::cout << "Suma numerelor este: " << adunare(4l, 6l) << '​\n';​ // invalid*/
  
- return 0;+    ​return 0;
 } }
 </​code>​ </​code>​
Line 308: Line 308:
 class Student class Student
 { {
- char* nume; +    ​char* nume; 
- T medieAnuala;​+    T medieAnuala;​
  
 public: public:
  
- Student(const char* nume, const T& medieAnuala);​ +    ​Student(const char* nume, const T& medieAnuala);​ 
- Student(const Student&​ student); +    Student(const Student&​ student); 
- Student&​ operator=(const Student&​ student); +    Student&​ operator=(const Student&​ student); 
- ~Student();​+    ~Student();
  
- char* getNume() const; +    ​char* getNume() const; 
- T getMedieAnuala() const;+    T getMedieAnuala() const;
  
- void setNume(const char* nume); +    ​void setNume(const char* nume); 
- void setMedieAnuala(const T& medieAnuala);​+    void setMedieAnuala(const T& medieAnuala);​
  
- template <​typename T> +    ​template <​typename T> 
- friend std::​ostream&​ operator<<​(std::​ostream&​ out, const Student<​T>&​ student);+    friend std::​ostream&​ operator<<​(std::​ostream&​ out, const Student<​T>&​ student);
 }; };
 </​code>​ </​code>​
Line 337: Line 337:
 Student<​T>::​Student(const char* nume, const T& medieAnuala) Student<​T>::​Student(const char* nume, const T& medieAnuala)
 { {
- if (nume != nullptr) +    ​if (nume != nullptr) 
-+    
- this->​nume = new char[strlen(nume) + 1]; +        this->​nume = new char[strlen(nume) + 1]; 
- strcpy(this->​nume,​ nume); +        strcpy(this->​nume,​ nume); 
-+    
- else +    else 
-+    
- this->​nume = nullptr; +        this->​nume = nullptr; 
-+    
-  + 
- this->​medieAnuala = medieAnuala;​+    this->​medieAnuala = medieAnuala;​
 } }
  
Line 353: Line 353:
 Student<​T>::​Student(const Student<​T>&​ student) Student<​T>::​Student(const Student<​T>&​ student)
 { {
- if (student.nume != nullptr) +    ​if (student.nume != nullptr) 
-+    
- nume = new char[strlen(student.nume) + 1]; +        nume = new char[strlen(student.nume) + 1]; 
- strcpy(nume,​ student.nume);​ +        strcpy(nume,​ student.nume);​ 
-+    
- else +    else 
-+    
- nume = nullptr; +        nume = nullptr; 
- }+    }
  
- medieAnuala = student.medieAnuala;​+    ​medieAnuala = student.medieAnuala;​
 } }
 </​code>​ </​code>​
Line 373: Line 373:
 Student<​T>&​ Student<​T>::​operator=(const Student<​T>&​ student) Student<​T>&​ Student<​T>::​operator=(const Student<​T>&​ student)
 { {
- if (this == &​student) +    ​if (this == &​student) 
-+    
- return *this; +        return *this; 
- }+    }
  
- if (nume != nullptr) +    ​if (nume != nullptr) 
-+    
- delete[] nume; +        delete[] nume; 
- }+    }
  
- if (student.nume != nullptr) +    ​if (student.nume != nullptr) 
-+    
- nume = new char[strlen(student.nume) + 1]; +        nume = new char[strlen(student.nume) + 1]; 
- strcpy(nume,​ student.nume);​ +        strcpy(nume,​ student.nume);​ 
-+    
- else +    else 
-+    
- nume = nullptr; +        nume = nullptr; 
- }+    }
  
- medieAnuala = student.medieAnuala;​+    ​medieAnuala = student.medieAnuala;​
  
- return *this;+    ​return *this;
 } }
 </​code>​ </​code>​
Line 405: Line 405:
 Student<​T>::​~Student() Student<​T>::​~Student()
 { {
- if (nume != nullptr) +    ​if (nume != nullptr) 
-+    
- delete[] nume; +        delete[] nume; 
- }+    }
 } }
 </​code>​ </​code>​
Line 418: Line 418:
 char* Student<​T>::​getNume() const char* Student<​T>::​getNume() const
 { {
- return nume;+    ​return nume;
 } }
  
Line 424: Line 424:
 T Student<​T>::​getMedieAnuala() const T Student<​T>::​getMedieAnuala() const
 { {
- return medieAnuala;​+    ​return medieAnuala;​
 } }
  
Line 430: Line 430:
 void Student<​T>::​setNume(const char* nume) void Student<​T>::​setNume(const char* nume)
 { {
- if (nume == nullptr) +    ​if (nume == nullptr) 
-+    
- return; +        return; 
- }+    }
  
- if (this->​nume != nullptr) +    ​if (this->​nume != nullptr) 
-+    
- delete[] this->​nume;​ +        delete[] this->​nume;​ 
- }+    }
  
- this->​nume = new char[strlen(nume) + 1]; +    ​this->​nume = new char[strlen(nume) + 1]; 
- strcpy(this->​nume,​ nume);+    strcpy(this->​nume,​ nume);
 } }
  
Line 447: Line 447:
 void Student<​T>::​setMedieAnuala(const T& medieAnuala) void Student<​T>::​setMedieAnuala(const T& medieAnuala)
 { {
- if (medieAnuala <= 0) +    ​if (medieAnuala <= 0) 
-+    
- return; +        return; 
- }+    }
  
- this->​medieAnuala = medieAnuala;​+    ​this->​medieAnuala = medieAnuala;​
 } }
 </​code>​ </​code>​
Line 462: Line 462:
 std::​ostream&​ operator<<​(std::​ostream&​ out, const Student<​T>&​ student) std::​ostream&​ operator<<​(std::​ostream&​ out, const Student<​T>&​ student)
 { {
- out << "​Numele studentului este: ";+    ​out << "​Numele studentului este: ";
  
- if (student.nume == nullptr) +    ​if (student.nume == nullptr) 
-+    
- out << "​N/​A\n";​ +        out << "​N/​A\n";​ 
-+    
- else +    else 
-+    
- out << student.nume << '​\n';​ +        out << student.nume << '​\n';​ 
- }+    }
  
- out << "Media anuala a studentului este: " << student.medieAnuala << '​\n';​+    ​out << "Media anuala a studentului este: " << student.medieAnuala << '​\n';​
  
- return out;+    ​return out;
 } }
 </​code>​ </​code>​
Line 484: Line 484:
 void testTemplate() void testTemplate()
 { {
- Student<​int>​ s1("​Ion",​ 10); +    ​Student<​int>​ s1("​Ion",​ 10); 
- Student<​int>​ s2("​George",​ 9); +    Student<​int>​ s2("​George",​ 9); 
- Student<​int>​ s3 = s2;+    Student<​int>​ s3 = s2;
  
- s3 = s1;+    ​s3 = s1;
  
- s3.setNume("​Maria"​);​ +    ​s3.setNume("​Maria"​);​ 
- s3.setMedieAnuala(8);​+    s3.setMedieAnuala(8);​
  
- std::cout << s3 << '​\n';​ +    ​std::cout << s3 << '​\n';​ 
- std::cout << s3.getNume() << "​\n\n";​ +    std::cout << s3.getNume() << "​\n\n";​ 
- std::cout << s3.getMedieAnuala() << "​\n\n";​+    std::cout << s3.getMedieAnuala() << "​\n\n";​
  
- Student<​double>​ s4("​Ion",​ 10); +    ​Student<​double>​ s4("​Ion",​ 10); 
- Student<​double>​ s5("​George",​ 9); +    Student<​double>​ s5("​George",​ 9); 
- Student<​double>​ s6 = s5;+    Student<​double>​ s6 = s5;
  
- s5 = s4;+    ​s5 = s4;
  
- s5.setNume("​Maria"​);​ +    ​s5.setNume("​Maria"​);​ 
- s5.setMedieAnuala(9.9);​+    s5.setMedieAnuala(9.9);​
  
- std::cout << s4 << '​\n';​ +    ​std::cout << s4 << '​\n';​ 
- std::cout << s4.getNume() << "​\n\n";​ +    std::cout << s4.getNume() << "​\n\n";​ 
- std::cout << s4.getMedieAnuala() << "​\n\n";​+    std::cout << s4.getMedieAnuala() << "​\n\n";​
 } }
 </​code>​ </​code>​
Line 521: Line 521:
 int main() int main()
 { {
- Student<​int>​ s1("​Ion",​ 10); +    ​Student<​int>​ s1("​Ion",​ 10); 
- Student<​int>​ s2("​George",​ 9); +    Student<​int>​ s2("​George",​ 9); 
- Student<​int>​ s3 = s2;+    Student<​int>​ s3 = s2;
  
- s3 = s1;+    ​s3 = s1;
  
- s3.setNume("​Maria"​);​ +    ​s3.setNume("​Maria"​);​ 
- s3.setMedieAnuala(8);​+    s3.setMedieAnuala(8);​
  
- std::cout << s3 << '​\n';​+    ​std::cout << s3 << '​\n';​
  
- Student<​double>​ s4("​Ion",​ 10); +    ​Student<​double>​ s4("​Ion",​ 10); 
- Student<​double>​ s5("​George",​ 9); +    Student<​double>​ s5("​George",​ 9); 
- Student<​double>​ s6 = s5;+    Student<​double>​ s6 = s5;
  
- s5 = s4;+    ​s5 = s4;
  
- s5.setNume("​Maria"​);​ +    ​s5.setNume("​Maria"​);​ 
- s5.setMedieAnuala(9.9);​+    s5.setMedieAnuala(9.9);​
  
- std::cout << s4 << '​\n';​+    ​std::cout << s4 << '​\n';​
  
- return 0;+    ​return 0;
 } }
 </​code>​ </​code>​
poo-is-ab/laboratoare/08.txt · Last modified: 2025/11/16 12:40 by razvan.cristea0106
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0