Compare commits
2
Commits
058179b643
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5993dd1dc8 | ||
|
|
4e2e5f72a2 |
+44
-37
@@ -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;
|
||||
|
||||
+31
-28
@@ -1,28 +1,31 @@
|
||||
|
||||
|
||||
#ifndef Person_h
|
||||
#define Person_h
|
||||
|
||||
#include <string>
|
||||
|
||||
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
|
||||
|
||||
|
||||
#ifndef Person_h
|
||||
#define Person_h
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Person {
|
||||
public:
|
||||
virtual ~Person();
|
||||
|
||||
static int GetCount();
|
||||
|
||||
Person(std::string name, int age);
|
||||
|
||||
void setSalary(int sal);
|
||||
|
||||
static std::shared_ptr<Person> GetPerson(std::string name, int age);
|
||||
|
||||
private:
|
||||
Person(const Person &);
|
||||
|
||||
std::string name;
|
||||
int age;
|
||||
int salary;
|
||||
|
||||
static int Count;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+69
-75
@@ -1,75 +1,69 @@
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Person.h"
|
||||
|
||||
#include "PersonException.h"
|
||||
#include <iostream>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::string;
|
||||
using std::exception;
|
||||
|
||||
int main() {
|
||||
std::vector<Person *> people;
|
||||
|
||||
string namePrompt = "Enter name: ";
|
||||
string name;
|
||||
string agePrompt = "Enter age: ";
|
||||
int age;
|
||||
string salaryPrompt = "Enter salary: ";
|
||||
int salary;
|
||||
|
||||
cout << namePrompt;
|
||||
cin >> name;
|
||||
|
||||
while (name != "q") {
|
||||
try {
|
||||
cout << agePrompt;
|
||||
cin >> age;
|
||||
|
||||
if (!cin) {
|
||||
cin.clear();
|
||||
string dummy;
|
||||
cin >> dummy;
|
||||
throw PersonException("invalid age");
|
||||
}
|
||||
|
||||
Person *personPtr = new Person(name, age);
|
||||
|
||||
cout << salaryPrompt;
|
||||
cin >> salary;
|
||||
if (!cin) {
|
||||
cin.clear();
|
||||
string dummy;
|
||||
cin >> dummy;
|
||||
throw PersonException("invalid salary");
|
||||
}
|
||||
|
||||
personPtr->setSalary(salary);
|
||||
|
||||
people.push_back(personPtr);
|
||||
}
|
||||
|
||||
catch (exception &e) {
|
||||
cout << "PersonException thrown: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
cout << "There are " << Person::GetCount() << " Person(s), " << people.size() << " in vector\n";
|
||||
|
||||
cout << namePrompt;
|
||||
cin >> name;
|
||||
}
|
||||
|
||||
// delete all the person(s) in the vector
|
||||
for (std::vector<Person *>::iterator iter = people.begin(); iter != people.end(); ++iter) {
|
||||
delete *iter;
|
||||
}
|
||||
|
||||
cout << "There are " << Person::GetCount() << " Person(s) left dangling\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Person.h"
|
||||
|
||||
#include "PersonException.h"
|
||||
#include <iostream>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::string;
|
||||
using std::exception;
|
||||
|
||||
int main() {
|
||||
std::vector<std::shared_ptr<Person>> people;
|
||||
|
||||
string namePrompt = "Enter name: ";
|
||||
string name;
|
||||
string agePrompt = "Enter age: ";
|
||||
int age;
|
||||
string salaryPrompt = "Enter salary: ";
|
||||
int salary;
|
||||
|
||||
cout << namePrompt;
|
||||
cin >> name;
|
||||
|
||||
while (name != "q") {
|
||||
try {
|
||||
cout << agePrompt;
|
||||
cin >> age;
|
||||
|
||||
if (!cin) {
|
||||
cin.clear();
|
||||
string dummy;
|
||||
cin >> dummy;
|
||||
throw PersonException("invalid age");
|
||||
}
|
||||
|
||||
auto personPtr = Person::GetPerson(name, age);
|
||||
|
||||
cout << salaryPrompt;
|
||||
cin >> salary;
|
||||
if (!cin) {
|
||||
cin.clear();
|
||||
string dummy;
|
||||
cin >> dummy;
|
||||
throw PersonException("invalid salary");
|
||||
}
|
||||
|
||||
personPtr->setSalary(salary);
|
||||
|
||||
people.push_back(personPtr);
|
||||
}
|
||||
|
||||
catch (exception &e) {
|
||||
cout << "PersonException thrown: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
cout << "There are " << Person::GetCount() << " Person(s), " << people.size() << " in vector\n";
|
||||
|
||||
cout << namePrompt;
|
||||
cin >> name;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user