answersLogoWhite

0


Best Answer

#include<iostream>

#include<string>

// a simple class

class my_class

{

private:

std::string m_caption;

public:

// default constructor

my_class (std::string caption): m_caption (caption) {}

// read-only accessor

const std::string& get_caption() const { return m_caption; }

};

// output stream insertion operator overload

std::ostream& operator<< (std::ostream& os, const my_class& obj)

{

os << obj.get_caption();

return os;

}

int main()

{

// call default constructor

my_class object1 ("This is the caption for object1");

// exercise output stream insertion operator overload

std::cout << object1 << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

The assignment operator. If you do not declare one in your class, the compiler generates one for you.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus program for overloading operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


Why do you use of operator overloading in c?

Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...


What is operator overloading?

I think you mean operation overlord??? It is the American, Canadian and British offensive on Europe in World War 2. They landed in Normandy on 6th June 1944 (Commonly called D-Day, Day of Days or Deliverance Day) and progressed throughout France liberating Paris on the 25th August. This allowed the allies a foothold in Europe.


What is the operator is used to allocate the memory in c plus plus?

calloc operator,malloc operator


How do you find largest of two object using operator overloading in c?

I will not use operator overloading in C# to do anything. Operator overloading may lead to one operator has more than 1 semantic meaning. For example, we know 1 + 2 yields 3, and "1" + 2 yields "12". I do not like this overloading of the operator + being used for addition in Number hierarchy, while as the concatenation in strings. That is, one operator (+) has 2 significant semantics.And the question "find largest of two object" is too vague - what do you mean "largest"? and object? We know apple and orange are 2 objects, but how do you compare them, and find the largest one?????? (size, price or what???)

Related questions

Is it possible to do operator overloading in c?

No. Operator and/or function overloading is only a C++ thing.


C coding for operator overloading?

C does not support operator overloading. If you mean C++ operator overloading, it depends on exactly what you wanted to do. If you wanted to '+' to strings, then you could write: string operator+(string a, string b) { // do something }


Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


What is an operator overloading in C?

one function but multiple behaviours depending on the parameters


Why should you have to use comparison operator overloading in pairs in c?

You cannot overload operators in C. This is a C++ thing only.


Why do you use of operator overloading in c?

Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...


What is the operator that cannot be overloaded in c plus plus and java?

conditional operator , size of operator , membership operator and scope resulation operator can not be overload in c++


What is operator overloading?

I think you mean operation overlord??? It is the American, Canadian and British offensive on Europe in World War 2. They landed in Normandy on 6th June 1944 (Commonly called D-Day, Day of Days or Deliverance Day) and progressed throughout France liberating Paris on the 25th August. This allowed the allies a foothold in Europe.


How do you find largest of two object using operator overloading in c?

I will not use operator overloading in C# to do anything. Operator overloading may lead to one operator has more than 1 semantic meaning. For example, we know 1 + 2 yields 3, and "1" + 2 yields "12". I do not like this overloading of the operator + being used for addition in Number hierarchy, while as the concatenation in strings. That is, one operator (+) has 2 significant semantics.And the question "find largest of two object" is too vague - what do you mean "largest"? and object? We know apple and orange are 2 objects, but how do you compare them, and find the largest one?????? (size, price or what???)


What is the operator is used to allocate the memory in c plus plus?

calloc operator,malloc operator


What is the operator of power in c plus plus?

There is no "power" operator in C or C++. You need to the use the math library function pow().


Example of binaray operator in c plus plus?

+ is an example, one of many, of a binary operator in C or C++ a = b + c; // for usage example