Well i got most of everything working with a little work. Like this:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Person
{
protected: string o_name;
public:
Person(string name)
{
this->o_name = name;
}
bool operator==(const Person& obj) const;
bool operator!=(const Person& obj) const;
};
bool Person::operator ==(const Person& obj) const
{
return (obj.o_name == this->o_name);
}
bool Person::operator !=(const Person &obj) const
{
return (obj.o_name != this->o_name);
}
class Worker: public Person
{
friend ostream& operator<<(ostream&, const Worker &);
friend istream& operator>>(istream&, Worker &);
private:
string workPlace;
int age;
public:
Worker(string name, string w_wP, int w_a): Person(name)
{
workPlace = w_wP;
age = w_a;
}
string getName() const;
string getWorkPlace() const;
int getAge() const;
void print() const;
};
string Worker::getName() const
{
return o_name;
}
string Worker::getWorkPlace() const
{
return workPlace;
}
int Worker::getAge() const
{
return age;
}
void Worker::print() const
{
cout << "Name = " << o_name
<< "; WorkPlace = " << workPlace
<< "; Age = " << age;
}
ostream& operator<<(ostream& osObject, const Worker& worker)
{
osObject <<"Name = " << worker.o_name << "; Work Place = " << worker.workPlace << "; Age = " << worker.age;
return osObject;
}
istream& operator>>(istream& isObject, Worker& worker)
{
isObject >> worker.o_name >> worker.workPlace >> worker.age;
return isObject;
}
int main()
{
string text = "", text1 = "";
int count = 0;
ifstream in;
ofstream out;
in.open("one.txt");
out.open("two.txt");
string w_name, w_name2;
string w_workPlace, w_workPlace2;
int w_age, w_age2;
char ch;
cout << "Enter name, work place and age, comma seperated : ";
getline(cin, w_name, ',');
getline(cin, w_workPlace, ',');
cin >> w_age;
cin.get(ch);
cout << "Enter name, work place and age, comma seperated : ";
getline(cin, w_name2, ',');
getline(cin, w_workPlace2, ',');
cin >> w_age2;
cin.get(ch);
Worker w1(w_name, w_workPlace, w_age);
Worker w2(w_name2, w_workPlace2, w_age2);
if(w1 == w2)
{
cout << "They have the same name" << endl;
}
else if(w1 != w2)
{
cout << "They do not have the same name" << endl;
}
vector<Worker> w;
w.push_back(w1);
w.push_back(w2);
cout << "Size of vector : " << w.size() << endl;
w1.print();
cout << endl;
cout << w1 << endl;
cout << "First element : " << w.front() << endl;
cout << "Last element : " << w.back() << endl;
in.close();
out.close();
return 0;
}
Does anyone know how i can enter stuff from a file (problem being, the stuff in the folder will be comma deliminated) coz i have to store it in a worker object then store in a vector once i have read it in. ANy help gret appre