From 5993dd1dc8dd53142b1e75ecd4810f3e175377b4 Mon Sep 17 00:00:00 2001 From: portersky Date: Thu, 8 Oct 2026 10:16:23 +0200 Subject: [PATCH] feat(cmat): Completed Ex61 Part 2 --- cmat/Ex61/Person.cpp | 81 ++++++++++++++++++++++++-------------------- cmat/Ex61/Person.h | 59 +++++++++++++++++--------------- cmat/Ex61/main.cpp | 2 +- 3 files changed, 76 insertions(+), 66 deletions(-) diff --git a/cmat/Ex61/Person.cpp b/cmat/Ex61/Person.cpp index 5b6d36c..2e4f2a0 100644 --- a/cmat/Ex61/Person.cpp +++ b/cmat/Ex61/Person.cpp @@ -1,37 +1,44 @@ -#include "Person.h" -#include "PersonException.h" -#include "YoungPerson.h" -#include - -using std::cout; - -Person::Person(std::string name, int age) : name(name), age(age) { - if (age < 0) { - throw PersonException("negative age"); - } - - cout << "Person ctor\n"; - Count++; -} - -Person::Person(const Person &) { - cout << "Copy ctor\n"; - Count++; -} - -Person::~Person() { - cout << "Dtor\n"; - Count--; -} - -void Person::setSalary(int sal) { - if (sal < 0) { - throw PersonException("negative salary"); - } - - salary = sal; -} - -int Person::GetCount() { return Count; } - -int Person::Count = 0; +#include "Person.h" +#include "PersonException.h" +#include "YoungPerson.h" +#include +#include +#include + +using std::cout; + +std::shared_ptr Person::GetPerson(std::string name, int age) { + if (age < 18) return std::make_shared(name, age); + return std::make_shared(name, age); +} + +Person::Person(std::string name, int age) : name(name), age(age) { + if (age < 0) { + throw PersonException("negative age"); + } + + cout << "Person ctor\n"; + Count++; +} + +Person::Person(const Person &) { + cout << "Copy ctor\n"; + Count++; +} + +Person::~Person() { + cout << "Dtor\n"; + Count--; +} + +void Person::setSalary(int sal) { + if (sal < 0) { + throw PersonException("negative salary"); + } + + salary = sal; +} + +int Person::GetCount() { return Count; } + +int Person::Count = 0; diff --git a/cmat/Ex61/Person.h b/cmat/Ex61/Person.h index e043203..d9c6d36 100644 --- a/cmat/Ex61/Person.h +++ b/cmat/Ex61/Person.h @@ -1,28 +1,31 @@ - - -#ifndef Person_h -#define Person_h - -#include - -class Person { - public: - virtual ~Person(); - - static int GetCount(); - - Person(std::string name, int age); - - void setSalary(int sal); - - private: - Person(const Person &); - - std::string name; - int age; - int salary; - - static int Count; -}; - -#endif \ No newline at end of file + + +#ifndef Person_h +#define Person_h + +#include +#include + +class Person { + public: + virtual ~Person(); + + static int GetCount(); + + Person(std::string name, int age); + + void setSalary(int sal); + + static std::shared_ptr GetPerson(std::string name, int age); + + private: + Person(const Person &); + + std::string name; + int age; + int salary; + + static int Count; +}; + +#endif diff --git a/cmat/Ex61/main.cpp b/cmat/Ex61/main.cpp index d9de8d4..21d747a 100644 --- a/cmat/Ex61/main.cpp +++ b/cmat/Ex61/main.cpp @@ -39,7 +39,7 @@ int main() { throw PersonException("invalid age"); } - auto personPtr = std::make_shared(name, age); + auto personPtr = Person::GetPerson(name, age); cout << salaryPrompt; cin >> salary;