Hello,

I unfortunately failed my advanced c++ class last semester so I need to retake it. Our first assignment is to "Define a base class person that will contain universal information, including name, address, birth date, gender
and identification (student, worker etc). Derive from this class the following classes:
Student
Worker
Student_worker

Write a program that asks user to input information (student, worker etc) and creates a list of persons.
Give user choice to view list by gender and by identification( list of people who are students, list of people who are workers etc)."

I'm stuck on how I can create a list to display gender or identification. I'm utterly stuck right now.. I was thinking of using a vector container but I don't know how I would go about doing that. Any help would be appreciated. In the meantime I'll get back to trying to figure this out

Code is as follows:

#include <iostream>
#include <string>

using namespace std;

class person {
protected:
	string name;
	string address;
	string birth_date;
	string gender;
	string identification;
public:
	person()//class constructor for person
	{
		cout << "Please enter the following information:" << endl;
		cout << "Name: ";
		cin >> name;
		cout << "Address: ";
		cin >> address;
		cout << "Birth date: ";
		cin >> birth_date;
		cout << "Gender: ";
		cin >> gender;
		cout << "Identification: ";
		cin >> identification; 
		cout << endl;
	}
};

class student:public person {
protected:
	string school;
public:
	student()//class constructor for student
	{
		cout << "Please enter the name of the school you attend: ";
		cin >> school;
	}
};

class worker:public person {
protected:
	string job;
public:
	worker()//class constructor for worker
	{
		cout << "Please enter the title of your job: ";
		cin >> job;
	}
};

class student_worker:public student, public worker  {
};

int main()
{
	int x;
	cout << "Please enter the information for a student: " << endl;
	student s1;
	cout << "Please enter the information for a worker: " << endl;
	worker w1;
	cout << "Please enter the information for a student worker: " << endl;
	student_worker sw1;

}

The code runs as is, but it isn't the entire assignment (obviously). Thanks for any future help.

Regards,
Don Panda

Recommended Answers

All 4 Replies

std::vector is not a difficult class to use

class Person
{
   // blabla
};

vector<Person> theList;

// add a person to the list
Person p;
// fill out the structure is not shown here

theList.push_back(p); // add to the list

// after that you can use the vector just like any other array

Alright, I have that part figured out. I've been tinkering with how I can display the list by gender or identification. I can display all the information via the vector, but how can I control what gets output? For example gender or identification. Would I use an iterator for this or am I thinking too complicated? Or is there a way to access parts of the vector directly?

You have a vector full of person objects, therefore you iterate over the objects and for each entry you query the object to see if it meets your criteria for printing it.

for( std::vector<person>::iterator it = people.begin();
it != people.end(); ++it )
{
  if( (*it).get_gender() == "Male" )
  {
      //print
  }
}

Obviously you'll need to have public accessor methods for your protected attributes in person.
i.e.

std::string get_gender(){ return gender; }

Yes even using iterator is not complex , just remove that mind block , type the code , compile and run , it will be as easy as cakewalk.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.