operator << operator >>
5 replies to this topic
#1
Posted 28 October 2010 - 11:10 AM
What is and how works operator in c++ ?
|
|
|
#2
Posted 28 October 2010 - 11:38 AM
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
operator>> - C++ Reference
#3
Posted 28 October 2010 - 11:48 AM
Or bitshift operators, either left or right
#4
Posted 28 October 2010 - 05:04 PM
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
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
Posted 29 October 2010 - 06:57 AM
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
Posted 29 October 2010 - 12:46 PM
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
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
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
The following would be the output
I hope this helps!
Munir
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


Sign In
Create Account


Back to top









