how to overload << and >> operator in c++

Answer Posted / manav sharma

class chocoBox {
private:
int pieCount;
float boxPrice;

public:
// By giving default arguments, const acts like
// 0, 1 and 2 argument contructor
myClass(int pCount = 10, float bPrice = 20.0)
: pieCount(pCount), boxPrice(bPrice)
{ }

int getPieCount() { return pieCount; }
float getBoxPrice() { return boxPrice; }
void setPieCount(int pc) { pieCount = pc; }
void setBoxPrice(float bp) {boxPrice = bp; }
};

/* Lets overload the operator << of ostream class. We will
return a reference of ostream class for cascading calls
e.g. cout<<obj1<<obj2
*/
ostream& operator<< (ostream &stream, const myClass &obj)
{
stream<<obj.GetPieCount<<" "<<obj.GetBoxPrice<<endl;
return (stream);
}

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is stl part of c++ standard?

726


Describe the My Computer and My Documents folders; identify the elements that are present in every Window.

1948


If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula: The population growth rate is given by the formula: B – D Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must have at least the following functions: 1. growthRate: This function takes its parameters the birth and death rates, and it returns the population growth rate. 2. estimatedPopulation: This function takes its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years Your program should not accept a negative birth rate, negative death rate, or a population less than 2. please answer my question ....

1865


totoo po ba ang manga aliens!

2423


What are the different types of stl containers?

746






what is template and type convertion

2094


What are the symptoms of stl?

700


How does an stl file work?

748


how to use C++?

2157


write a program to convert a decimal number in to its equivalent binary number?

2165


When did c++ add stl?

836


What are the components of stl?

718


Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.

2202


What is a list in c++ stl?

784


What is stl in c++ with example?

732