Okay what i want to do is: Code the implementation of a Person class (2 person object are equal if they have the same name) (b) Code the implementation of a Worker class (c) The program must read in the name, age and workplace (comma delimited) from the console in to a Worker object and store in a vector. What i have done so far is: Code: #include <iostream> #include <string> using namespace std; class Person { public: string n; public: Person(string n) { this->n = n; } bool operator==(const Person& obj) const; Person(); }; bool Person::operator ==(const Person& obj) const { if(obj.n != this->n) { return false; } else { return true; } } class Worker { public: void setInfo(string, int, string); void getInfo(string&, int&, string&) const; Worker(string, int, string); Worker(); private: string name; int age; string workPlace; }; I am completely lost on part b and c i dont know what to do. ANy help gr app. Thanks
I have managed to figure out what i needed i would like to know how would i read a files values into my program that are comma deliminated. Like: "David",23,"XPlace" And does it matter if there are spaces when you reading in the information ANy help greatly appreciated,
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
ifstream in; in.read(buf,Num); Where buffer is where the content is read and num is the no of bytes read. So if I am getting you correctly this should read it from the file.
sorry but i dont understand im new, but how will that read infomration comma sperated it does not work for me
Why it shouldn't. Here is the sample code Code: #include <iostream.h> #include <fstream.h> #define N 100 int main() { char buf[N]; ifstream in; in.open("a.txt"); in.getline(buf,N); cout<<buf << endl; return 0; }
no i get that its just i want to use the data to store in an object. like : "darrel","xWork",34 Then store darrel xWork and 34 in the object