Jump to content
  • Advertisement
Sign in to follow this  

Can someone check this

This topic is 4413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just tried overloading << and >>. Can someone look into this and suggest any changes or tell me if I am doing thing right. Is this right we do do it??
#include<iostream>
using namespace std;

class Employee
{
        private:
             char name[40];
             int age;
             long salary;
             char gender;

        public:
             Employee();   
             ~Employee();  

             void RetreiveData();
             void DisplayData();

             istream& operator >> (istream&);
             ostream& operator << (ostream&);
};

Employee::Employee()   
{

}
 
Employee::~Employee() 
{

}
 
void Employee::RetreiveData()
{
       *this >> cin;
}

void Employee::DisplayData()
{
       *this << cout;
}

istream& Employee::operator >> (istream& input)
{
        cout << "\nEnter Name : ";
        input.ignore(100000,'\n');
        input.getline(name,40,'\n');

        cout << "Enter Age : ";
        input >> age;

        cout << "Enter Salary : ";
        input >> salary;

        cout << "Enter Gender : ";
        input >> gender;
     
        return (input);
}

ostream& Employee::operator << (ostream& output)
{
       output << "\nName : " << name << endl;
       output << "Age : " << age << endl;
       output << "Salary : " << salary << endl;
       output << "Gender : " << gender << endl;

       return (output);
}

int main()
{
    int n;
    cout << "Enter number of Employees : ";
    cin >> n;

    
    Employee* obj = new Employee[n];
   
    

    for (int i=0; i<n; i++)
    {
          cout << "\nData for Employee " << i+1 << " : " << endl;
          obj.RetreiveData();
    }

    cout << "\n\nYou Entered the following data : " << endl;
   
    for (i=0; i<n; i++)
    {
          cout << "\nEmployee " << i+1 << " : " << endl;
          obj.DisplayData();
    }

    delete []obj;   
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Your << and >> operators are member functions - they take a istream/ostream as their 2nd parameter - but they should take an istream/ostream as their 1st param.

You need to make your << and >> operators non-member functions with the following signatures:

istream& operator >> (istream&,Employee&);
ostream& operator << (ostream&,const Employee&);

Share this post


Link to post
Share on other sites
No, because you want to have your object as the argument. Ie, you don't do myclass.operator<<(cout), you do cout.operator<<(myclass). Since you can't modify std::ostream, you're only possibility is to define an operator<< outside your class - and its first parameter will be an ostream object.

Regards,

Share this post


Link to post
Share on other sites

#include<iostream>
using namespace std;

class Employee
{
private:
friend ostream &operator<<(ostream &o,const Employee &e);
friend istream &operator>>(istream &i,Employee &e);

char name[40];
int age;
long salary;
char gender;

public:
Employee(){ }
~Employee(){ }
};


ostream &operator<<(ostream &o,const Employee &e)
{
o << e.name << endl << e.age << endl; // and so on
return o;
}

istream &operator>>(istream &i,Employee &e)
{
i >> e.name >> e.age; // and so on
return i;
}

int main(int,const char **av)
{
Employee Emp[10];

cout << "Enter details:" << endl;
for(int i=0;i<10;++i) cin >> Emp;

cout << "You entered:" << endl;
for(int i=0;i<10;++i) cout << Emp << endl;

return 0;
}






I guess this is more the sort of thing you want. HTH.

Share this post


Link to post
Share on other sites
<< and >> can be made into member functions, but it won't work in this case.

If << is a member function, then the expression a << b is the same as
a.operator<<(b);, so the stream input/output operators would have to be members of the stream class.

Your options are to make operators << and >> into friend functions or to provide functions on the employee class to that give access to the state of the object, and then write << and >> in terms of them.

Share this post


Link to post
Share on other sites
Quote:
Original post by TEUTON
I just tried overloading << and >>. Can someone look into this and suggest any changes or tell me if I am doing thing right. Is this right we do do it??

*** Source Snippet Removed ***


0) Don't declare default constructors or the destructor if you're not going to do anything in the implementation. Unless you (a) will write some *other* constructor AND (b) it is valid to default-construct without having any information - then you may write the default constructor.
1) Use vectors and strings.
2) cout and cin have to go on the left hand side of an operator<< or operator>> usage.
3) When you implement it for your own class, (a) as mentioned, it can't be a member and (b) don't output prompt messages in the input routine!
4) Don't wrap the operator overloads in member functions. If anything, you want the other way around (implement the operator overload using member functions).
5) When you "serialize" a string, you need to know how long it is. For my example, I will be assuming the "name" is one word.

The whole idea of "reading in an object from a stream" is *not* appropriate for an *interactive* environment. You normally want to read from a file instead. For my example, I will prompt for the values individually, and then call an appropriate constructor using those values. I will also write the operator>>, but not use it.

Re 1) and 2): The arrows do point towards the thing that receives the data, as a mnemonic, but you still want the stream object on the left-hand side.

For writing the operator overloads, I will take the approach of "implement in terms of a member function". You can also do it (and many will recommend it, though I'm on the fence myself) doing it with a friend function.


#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
using namespace std;

class Employee {
string name;
int age;
long salary;
char gender;

public:
Employee(string name, int age, long salary, char gender) :
name(name), age(age), salary(salary), gender(gender) {}

void writeTo(ostream& os);
void readFrom(istream& is);
};

void Employee::writeTo(ostream& os) {
// Reformat as desired.
os &lt;&lt; name &lt;&lt; " " &lt;&lt; age &lt;&lt; " " &lt;&lt; salary &lt;&lt; " " &lt;&lt; gender;
}

void Employee::readFrom(istream& is) {
is &gt;&gt; name &gt;&gt; age &gt;&gt; salary &gt;&gt; gender;
}

istream& operator&gt;&gt; (istream& is, Employee& e) {
e.readFrom(is);
return is;
}

ostream& operator&lt;&lt; (ostream& os, const Employee& e) {
e.writeTo(os);
return os;
}

template &lt;typename T&gt;
T prompt(const char* label) {
T result;
bool read;
while (read) {
cout &lt;&lt; "\nEnter " &lt;&lt; label &lt;&lt; " : " &lt;&lt; flush;
if (cin &gt;&gt; result) { read = true; } // still do cleanup
cin.reset();
cin.ignore(100000,'\n');
}
return result;
}

Employee promptForEmployee() {
// We assign to temporaries only to ensure that
// the prompts are performed in this order
string name = prompt&lt;string&gt;("Name");
int age = prompt&lt;int&gt;("Age");
long salary = prompt&lt;long&gt;("Salary");
char gender = prompt&lt;char&gt;("Gender");
return Employee(name, age, salary, gender);
}

int main() {
vector&lt;Employee&gt; employees;
int n;
cout &lt;&lt; "Enter number of Employees : ";
cin &gt;&gt; n;

for (int i=0; i&lt;n; i++) {
cout &lt;&lt; "\nData for Employee " &lt;&lt; i+1 &lt;&lt; " : " &lt;&lt; endl;
employees.push_back(promptForEmployee);
}

cout &lt;&lt; "\n\nYou Entered the following data : " &lt;&lt; endl;

for (i=0; i&lt;n; i++) {
cout &lt;&lt; "\nEmployee " &lt;&lt; i+1 &lt;&lt; " : " &lt;&lt; endl;
cout &lt;&lt; employees &lt;&lt; endl;
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!