feat(cmat): Completed Ex61 Part 2

This commit is contained in:
2026-10-08 10:16:23 +02:00
parent 4e2e5f72a2
commit 5993dd1dc8
3 changed files with 76 additions and 66 deletions
+44 -37
View File
@@ -1,37 +1,44 @@
#include "Person.h"
#include "PersonException.h"
#include "YoungPerson.h"
#include <iostream>
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 <cstddef>
#include <iostream>
#include <memory>
using std::cout;
std::shared_ptr<Person> Person::GetPerson(std::string name, int age) {
if (age < 18) return std::make_shared<YoungPerson>(name, age);
return std::make_shared<Person>(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;