Skip to content

Instantly share code, notes, and snippets.

@dvliman
Last active August 29, 2015 14:09
Show Gist options
  • Save dvliman/b721c6ec023a09e1ec09 to your computer and use it in GitHub Desktop.
Save dvliman/b721c6ec023a09e1ec09 to your computer and use it in GitHub Desktop.
new
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Person {
protected:
string name;
string address;
string birthdate;
string gender;
string identification;
public:
virtual ~Person() {};
Person(string n, string a, string b, string g, string i) {
name = n;
address = a;
birthdate = b;
gender = g;
identification = i;
};
void output() {
cout << "name: " << name << " ";
cout << ", address: " << address << " ";
cout << ", birthdate: " << birthdate << " ";
cout << ", gender: " << gender << " ";
cout << ", identification: " << identification << " ";
};
string getGender() {
return gender;
};
string getType() {
return identification; // not sure what by type is, assume this class
};
};
// comparators
bool byGender(Person* left, Person* right) { return left->getGender() < right->getGender(); };
bool byType(Person* left, Person* right) { return left->getType() < right->getType(); };
class Student: public Person {
protected:
string school;
public:
Student(string n, string a, string b, string g, string i, string s): Person(n, a, b, g, i) {
school = s;
};
void output() {
Person::output();
cout << ", school: " << school << " " << endl; // endl clear stdout buffer
};
};
class Worker: public Person {
protected:
string company;
public:
Worker(string n, string a, string b, string g, string i, string c): Person(n, a, b, g, i) {
company = c;
};
void output() {
Person::output();
cout << ", company: " << company << " " << endl;
};
};
class StudentWorker: public Student, public Worker {
public:
StudentWorker(string n, string a, string b, string g, string i, string s, string c):
Student(n, a, b, g, i, s), Worker(n, a, b, g, i, s) {}; // inherited from both student and worker
// namespace with either Student:: or Worker:: to remove disambiguity
void output() {
cout << "name: " << Student::name << " ";
cout << ", address: " << Student::address << " ";
cout << ", birthdate: " << Student::birthdate << " ";
cout << ", gender: " << Student::gender << " ";
cout << ", identification: " << Student::identification << " ";
cout << ", school: " << Student::school << " ";
cout << ", company: " << Worker::company << " " << endl;
};
};
int main() {
vector <Person*> people;
while(true) {
cout << "enter 1 for student, 2 for worker, 3 for both (StudentWorker), or 4 to print out";
int choice;
cin >> choice;
cout << choice;
if (choice == 1) {
string name, address, birthdate, gender, identification, school;
cout << "enter name: "; cin >> name;
cout << "enter address: "; cin >> address;
cout << "enter birthdate (mm/dd/yyyy): "; cin >> birthdate;
cout << "enter gender: "; cin >> gender;
cout << "enter identification: "; cin >> identification;
cout << "enter school: "; cin >> school;
people.push_back(new Student(name, address, birthdate, gender, identification, school));
} else if (choice == 2) {
string name, address, birthdate, gender, identification, company;
cout << "enter name: "; cin >> name;
cout << "enter address: "; cin >> address;
cout << "enter birthdate (mm/dd/yyyy): "; cin >> birthdate;
cout << "enter gender: "; cin >> gender;
cout << "enter identification: "; cin >> identification;
cout << "enter company: "; cin >> company;
people.push_back(new Worker(name, address, birthdate, gender, identification, company));
} else if (choice == 3) {
string name, address, birthdate, gender, identification, school, company;
cout << "enter name: "; cin >> name;
cout << "enter address: "; cin >> address;
cout << "enter birthdate (mm/dd/yyyy): "; cin >> birthdate;
cout << "enter gender: "; cin >> gender;
cout << "enter identification: "; cin >> identification;
cout << "enter school: "; cin >> school;
cout << "enter company: "; cin >> company;
StudentWorker* student_worker = new StudentWorker(name, address, birthdate, gender, identification, school, company);
Person* person = static_cast<Person*>(static_cast<Student*>(student_worker)); // polymorphism shit
people.push_back(person);
} else {
break; // we are done accepting user input if choice 4 or anything else
}
}
cout << "you have entered " << people.size() << " records, printing...";
// loop through the vector using iterator
for(std::vector<Person*>::size_type i = 0; i != people.size(); i++) {
people[i]->output();
}
// sort by gender
sort(people.begin(), people.end(), byGender);
for(std::vector<Person*>::size_type i = 0; i != people.size(); i++) {
people[i]->output();
}
// sort by type
sort(people.begin(), people.end(), byType);
for(std::vector<Person*>::size_type i = 0; i != people.size(); i++) {
people[i]->output();
}
return 0; // success exit code
};
// Student Name: Raymond The
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Person
{
protected:
string Name;
string Address;
string Birth_Date;
string Gender;
string ID;
public:
Person(string, string, string, string, string);
Person()
{
Name = "";
Address = "";
Birth_Date = "";
Gender = "";
ID = "";
}
void input();
void output();
};
Person::Person(string n, string a, string bd, string g, string id)
{
Name = n;
Address = a;
Birth_Date = bd;
Gender = g;
ID = id;
}
void Person::input()
{
cout << "Enter your Name, Address, Birth Date (mm/dd/yy), Gender, and ID (student 'S', worker 'W', or both 'B')." << endl;
cin >> Name >> Address >> Birth_Date >> Gender >> ID;
}
void Person::output()
{
cout << Name << " " << Address << " " << Birth_Date << " " << Gender << " " << ID << endl;
}
class Student : public Person
{
protected:
string School;
public:
Student();
Student(string, string, string, string, string, string);
void Student::input();
void Student::output();
};
void Student::input()
{
cout << "Enter your Name, Address, Birth Date (mm/dd/yy), Gender, ID (student 'S', worker 'W', or both 'B'), and School." << endl;
cin >> Name >> Address >> Birth_Date >> Gender >> ID >> School;
}
void Student::output()
{
cout << Name << " " << Address << " " << Birth_Date << " " << Gender << " " << ID << " " << School << endl;
}
Student::Student(string n, string a, string bd, string g, string id, string s)
{
Name = n;
Address = a;
Birth_Date = bd;
Gender = g;
ID = id;
School = s;
}
class Worker : public Person
{
protected:
string Company;
public:
Worker();
Worker(string, string, string, string, string, string);
void Worker::input();
void Worker::output();
};
void Worker::input()
{
Person::input();
cout << "Enter your Name, Address, Birth Date (mm/dd/yy), Gender, ID (student 'S', worker 'W', or both 'B'), and Company." << endl;
cin >> Name >> Address >> Birth_Date >> Gender >> ID >> Company;
}
void Worker::output()
{
cout << Name << " " << Address << " " << Birth_Date << " " << Gender << " " << ID << " " << Company << endl;
}
Worker::Worker(string n, string a, string bd, string g, string id, string c)
{
Name = n;
Address = a;
Birth_Date = bd;
Gender = g;
ID = id;
Company = c;
}
/*
class Student_Worker : public Student, public Worker
{
public:
Student_Worker();
Student_Worker(string, string, string, string, string, string, string);
void input()
{
Student::input();
cout << "Enter your Name, Address, Birth Date (mm/dd/yy), Gender, ID (student 'S', worker 'W', or both 'B'), School, and Company." << endl;
cin >> Student::Name >> Student::Address >> Student::Birth_Date >> Student::Gender >> Student::ID >> Student::School >> Worker::Company;
}
void output()
{
Student::output();
cout << Student::Name << " " << Student::Address << " " << Student::Birth_Date << " " << Student::Gender << " " << Student::ID << " " << Student::School << " " << Worker::Company << endl;
}
};
Student_Worker::Student_Worker(string n, string a, string bd, string g, string id, string s, string c)
{
Student::Name = n;
Student::Address = a;
Student::Birth_Date = bd;
Student::Gender = g;
Student::ID = id;
Student::School = s;
Worker::Company = c;
}
*/
void main()
{
int x = 0;
string c = "Y";
vector<void*> p;
while (c != "N")
{
string name, address, birth_date, gender, id, school, company;
int choice;
cout << "Enter ID (student '1', worker '2', or both '3'): ";
cin >> choice;
switch (choice) {
case 1: {
cout << "Enter your Name, Address, Birth Date (mm/dd/yy), Gender, and School." << endl;
cin >> name >> address >> birth_date >> gender >> school;
p.push_back(Student(name, address, birth_date, gender, "S", school));
break;
}
case 2: {
cout << "Enter your Name, Address, Birth Date (mm/dd/yy), Gender, and Company." << endl;
cin >> name >> address >> birth_date >> gender >> company;
p.push_back(Worker(name, address, birth_date, gender, "W", company));
break;
}
}
cout << "Enter another person? (Y/N) : ";
cin >> c;
x++;
}
for (int i = 0; i < p.size(); i++) {
p[i].output();
}
}
@dvliman
Copy link
Author

dvliman commented Nov 12, 2014

~/Downloads g++ Project2.cpp -o out
~/Downloads ./out
enter 1 for student, 2 for worker, 3 for both (StudentWorker), or 4 to print out1
1enter name: david
enter address: liman
enter birthdate (mm/dd/yyyy): 12
enter gender: a
enter identification: b
enter school: c
enter 1 for student, 2 for worker, 3 for both (StudentWorker), or 4 to print out2
2enter name: liman
enter address: test
enter birthdate (mm/dd/yyyy): fda
enter gender: b
enter identification: a
enter company: d
enter 1 for student, 2 for worker, 3 for both (StudentWorker), or 4 to print out4
4you have entered 2 records, printing...
name: david , address: liman , birthdate: 12 , gender: a , identification: b
name: liman , address: test , birthdate: fda , gender: b , identification: a

name: david , address: liman , birthdate: 12 , gender: a , identification: b
name: liman , address: test , birthdate: fda , gender: b , identification: a

name: liman , address: test , birthdate: fda , gender: b , identification: a
name: david , address: liman , birthdate: 12 , gender: a , identification: b %

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment