feat(cmat): Completed Ex61 Part 2
This commit is contained in:
@@ -1,10 +1,17 @@
|
|||||||
#include "Person.h"
|
#include "Person.h"
|
||||||
#include "PersonException.h"
|
#include "PersonException.h"
|
||||||
#include "YoungPerson.h"
|
#include "YoungPerson.h"
|
||||||
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
using std::cout;
|
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) {
|
Person::Person(std::string name, int age) : name(name), age(age) {
|
||||||
if (age < 0) {
|
if (age < 0) {
|
||||||
throw PersonException("negative age");
|
throw PersonException("negative age");
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#ifndef Person_h
|
#ifndef Person_h
|
||||||
#define Person_h
|
#define Person_h
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Person {
|
class Person {
|
||||||
@@ -15,6 +16,8 @@ class Person {
|
|||||||
|
|
||||||
void setSalary(int sal);
|
void setSalary(int sal);
|
||||||
|
|
||||||
|
static std::shared_ptr<Person> GetPerson(std::string name, int age);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Person(const Person &);
|
Person(const Person &);
|
||||||
|
|
||||||
|
|||||||
+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