feat(cmat): Completed Ex61 Part 2
This commit is contained in:
+44
-37
@@ -1,37 +1,44 @@
|
|||||||
#include "Person.h"
|
#include "Person.h"
|
||||||
#include "PersonException.h"
|
#include "PersonException.h"
|
||||||
#include "YoungPerson.h"
|
#include "YoungPerson.h"
|
||||||
#include <iostream>
|
#include <cstddef>
|
||||||
|
#include <iostream>
|
||||||
using std::cout;
|
#include <memory>
|
||||||
|
|
||||||
Person::Person(std::string name, int age) : name(name), age(age) {
|
using std::cout;
|
||||||
if (age < 0) {
|
|
||||||
throw PersonException("negative age");
|
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);
|
||||||
cout << "Person ctor\n";
|
}
|
||||||
Count++;
|
|
||||||
}
|
Person::Person(std::string name, int age) : name(name), age(age) {
|
||||||
|
if (age < 0) {
|
||||||
Person::Person(const Person &) {
|
throw PersonException("negative age");
|
||||||
cout << "Copy ctor\n";
|
}
|
||||||
Count++;
|
|
||||||
}
|
cout << "Person ctor\n";
|
||||||
|
Count++;
|
||||||
Person::~Person() {
|
}
|
||||||
cout << "Dtor\n";
|
|
||||||
Count--;
|
Person::Person(const Person &) {
|
||||||
}
|
cout << "Copy ctor\n";
|
||||||
|
Count++;
|
||||||
void Person::setSalary(int sal) {
|
}
|
||||||
if (sal < 0) {
|
|
||||||
throw PersonException("negative salary");
|
Person::~Person() {
|
||||||
}
|
cout << "Dtor\n";
|
||||||
|
Count--;
|
||||||
salary = sal;
|
}
|
||||||
}
|
|
||||||
|
void Person::setSalary(int sal) {
|
||||||
int Person::GetCount() { return Count; }
|
if (sal < 0) {
|
||||||
|
throw PersonException("negative salary");
|
||||||
int Person::Count = 0;
|
}
|
||||||
|
|
||||||
|
salary = sal;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Person::GetCount() { return Count; }
|
||||||
|
|
||||||
|
int Person::Count = 0;
|
||||||
|
|||||||
+31
-28
@@ -1,28 +1,31 @@
|
|||||||
|
|
||||||
|
|
||||||
#ifndef Person_h
|
#ifndef Person_h
|
||||||
#define Person_h
|
#define Person_h
|
||||||
|
|
||||||
#include <string>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
class Person {
|
|
||||||
public:
|
class Person {
|
||||||
virtual ~Person();
|
public:
|
||||||
|
virtual ~Person();
|
||||||
static int GetCount();
|
|
||||||
|
static int GetCount();
|
||||||
Person(std::string name, int age);
|
|
||||||
|
Person(std::string name, int age);
|
||||||
void setSalary(int sal);
|
|
||||||
|
void setSalary(int sal);
|
||||||
private:
|
|
||||||
Person(const Person &);
|
static std::shared_ptr<Person> GetPerson(std::string name, int age);
|
||||||
|
|
||||||
std::string name;
|
private:
|
||||||
int age;
|
Person(const Person &);
|
||||||
int salary;
|
|
||||||
|
std::string name;
|
||||||
static int Count;
|
int age;
|
||||||
};
|
int salary;
|
||||||
|
|
||||||
#endif
|
static int Count;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
+1
-1
@@ -39,7 +39,7 @@ int main() {
|
|||||||
throw PersonException("invalid age");
|
throw PersonException("invalid age");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto personPtr = std::make_shared<Person>(name, age);
|
auto personPtr = Person::GetPerson(name, age);
|
||||||
|
|
||||||
cout << salaryPrompt;
|
cout << salaryPrompt;
|
||||||
cin >> salary;
|
cin >> salary;
|
||||||
|
|||||||
Reference in New Issue
Block a user