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

What do stl stand for?

647


How connect plc and pc through software

1927


Define stl.

784


Why should a c++ programmer be interested in stl?

647


How do you convert stl to steps?

639






What does stl mean in slang?

656


What are the different types of stl containers?

652


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

2086


Is stl part of c++ standard?

647


How is stl different from c++ standard library?

752


Do you like to Submit Questions in Bulk under Same Category?? Then use our Bulk ListerDo you like to Submit Questions in Bulk under Same Category?? Then use our Bulk Lister

1668


To modify an, existing worksheet. What steps are involved for: 1. Inserting and deleting rows and columns. 2. Printing cell formulas 3Jld displayed values 3. Using the page setup command

1787


how to use C++?

2072


How does an stl file work?

657


What is a standard template library (stl)? What are the various types of stl containers?

676