Skip to content

anonymous /Diamond Problem

#include <iostream>
using namespace std;
class person
{
public:
void set_name(string n)
{
name=n;
}
string get_name() const
{
return name;
}
void set_age(int a)
{
age=a;
}
int get_age() const
{
return age;
}
void set_gender(char g)
{
gender=g;
}
char get_gender() const
{
return gender;
}
void set_address(string add)
{
address=add;
}
string get_address() const
{
return address;
}
void set_identification(string i)
{
identification=i;
}
string get_identification() const
{
return identification;
}
person()
{
name="";
age=0;
gender=' ';
address="";
identification="";
}
virtual void input()
{
cout<<"Enter name: ";
cin>>name;
set_name(name);
cout<<"Enter age: ";
cin>>age;
set_age(age);
cout<<"Enter gender (m or f): ";
cin>>gender;
set_gender(gender);
cout<<"Enter address: ";
cin>>address;
set_address(address);
}
protected:
string identification;
string name;
int age;
char gender;
string address;
};
class student:virtual public person
{
public:
void set_school(string s)
{
school=s;
}
string get_school() const
{
return school;
}
student():person()
{
school="";
identification="s";
}
virtual void input()
{
person::input();
cout<<"Enter school: ";
cin>>school;
set_school(school);
}
protected:
string school;
};
class worker:virtual public person
{
public:
void set_company(string c)
{
company=c;
}
string get_company() const
{
return company;
}
worker():person()
{
company="";
identification="w";
}
virtual void input()
{
person::input();
cout<<"Enter company: ";
cin>>company;
set_company(company);
}
protected:
string company;
};
class student_worker: virtual public student, virtual public worker
{
public:
student_worker():student(),worker()
{
identification="sw";
}
virtual void input()
{
student::input();
worker::input();
}
};
int main()
{
person p[2];
string id;
student s;
worker w;
student_worker sw;
for(int i=0; i<2; i++)
{
cout<<"Enter student 's', worker 'w', or student worker 'sw': ";
cin>>id;
if (id == "s")
{
s.input();
p[i] = s;
}
if (id == "w")
{
w.input();
p[i] = w;
}
if (id == "sw")
{
sw.input();
p[i] = sw;
}
}
string choice;
cout<<"Sort by gender (g) or identification (id)?: ";
cin>>choice;
if(choice=="g")
{
cout<<"-Males-"<<endl;
for(int i=0; i<2; i++)
{
if(p[i].get_gender()=='m')
{
cout<<p[i].get_name()<<endl;
}
}
cout<<"-Females-"<<endl;
for(int i=0; i<2; i++)
{
if(p[i].get_gender()=='f')
{
cout<<p[i].get_name()<<endl;
}
}
}
if(choice=="id")
{
cout<<"-Students-"<<endl;
for(int i=0; i<2; i++)
{
if(p[i].get_identification()=="s")
{
cout<<p[i].get_name()<<endl;
}
}
cout<<"-Workers-"<<endl;
for(int i=0; i<2; i++)
{
if(p[i].get_identification()=="w")
{
cout<<p[i].get_name()<<endl;
}
}
cout<<"-Student Workers-"<<endl;
for(int i=0; i<2; i++)
{
if(p[i].get_identification()=="sw")
{
cout<<p[i].get_name()<<endl;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.