Jump to content

operator

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
What is and how works operator in c++ ?


operator <<

operator >>



#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Since operators can be overloaded, can you at least provide the context in which they are used? They are most likely the extraction and insertion operators
operator>> - C++ Reference

#3
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Or bitshift operators, either left or right

#4
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

John said:

Since operators can be overloaded, can you at least provide the context in which they are used? They are most likely the extraction and insertion operators
operator>> - C++ Reference

My question is when they appear in a class. Example:

class Example
{
    const Example& operator << (int variable);
    const Example& operator >> (int variable);
}


#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
In your code they are overloaded so they do whatever code you tell them to do.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

A class can overload insertion & extraction operators in order to get/output and set its private data. For example, you have a following class


class Employee

{

  private:

      string m_sEmployeeName; //string to keep employee name

      int      m_iEmployeeID; //int to keep employee id

      double m_dEmployeeSalary; //a double to keep employee salary

 }


Suppose that you want to show an object to Employee Object data to console. Normally, we can use 'cout<<variable' to output to console: but in case of user-defined data types like class, we need to overload extraction operator for that class.

How to Overload '<<' For a class

Suppose for Employee class that we have defined above, we want to overload << operation. For this we define a friend function for this class


class Employee

{

  private:

      string m_sEmployeeName; //string to keep employee name

      int      m_iEmployeeID; //int to keep employee id

      double m_dEmployeeSalary; //a double to keep employee salary

  public:

       Employee(string n, int i, double s): m_sEmployeeName(n), m_iEmployeeID(i), m_dEmployeeSalary(s)

{

}

      //a friend function that overloads << operation for our class

       friend ostream& operator<< (ostream &out, Employee &employee);

};


ostream& operator<< (ostream &out, Employee &employee)

{

	    // Since operator<< is a friend of the Employee class, we can access

	    // Employee's members directly.


            out<<"Employee Name "<<employee.m_sEmployeeName<<endl;

            out<<"Employee ID "<<employee.m_iEmployeeID<<endl;

            out<<"Employee Salary "<<employee.m_dEmployeeSalary<<endl;


	    return out;

}


When overload '<<' is called

Since we have overload << for our class employee. we can use it to extract the values from an object of class employee



//in main function


int main()

{

   Employee employee("Munir", 1045, 6000.00);

   cout<<employee;[B][COLOR="SeaGreen"] //This is the place when overload function for << for Employee class is called[/COLOR][/B]

}



The following would be the output

Employee Name Munir

Employee ID 1045

Employee Salary 6000



I hope this helps!

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users