answersLogoWhite

0


Best Answer

class foo

{

private:

int m_data;

public:

foo (int data=0): m_data (data) {}

foo (const foo& source): m_data (source.m_data) {}

foo (foo& source): m_data (std::move (source.m_data)) {}

// operator overloads: assign

foo& operator= (const int source) { m_data = source; return *this; }

foo& operator= (const foo& source) { m_data = source.m_data; return *this; }

foo& operator= (foo& source) { m_data = std::move (source.m_data); return this; }

// compound operator overloads: increment and assign

foo& operator+= (const foo& rhs) { m_data += rhs.m_data; return *this; }

foo& operator+= (const int rhs) { m_data += rhs; return *this; }

};

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus program using operator overloading?
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.


What are the disadvantage of operator overloading?

The only disadvantage of operator overloading is when it is used non-intuitively. All operators must behave with predictable results, thus it makes no sense to implement the plus (+) operator so that it behaves like a subtract (-) operator, or a multiply (*) operator, or indeed anything other than the intuitive sum of two objects.


Why is it necessary to overload an operator?

The assignment is done explicit without internal operation. Subject to the programming language, explicit assignment operators are needed wherever implicit ones are insufficient. Implicit assignment is typically implemented as a flat copy, while explicit overloading of the assignment operator allows for any other suitable behavior. Consider this example in pseudocode similar to C++: class Demo { int* valuepointer; ... }; Demo a, b; ... b = a; Assigning a to b using implicit assignment means that a.valuepointer and b.valuepointer share the same value. Both a and b can change the pointed-to value, and the either will "see" the change. This is the required behavior in some cases, but often, you'd want to explicitly assign a to b such that each has its own pointer, accessing different copies of the value. This behavior would require an explicit assignment operator (or copy constructor).


Write a c plus plus program to use plus operator to display the sum of 2 numbers and concatenation of two strings by using operator overloading?

There is no need to overload the plus operator to achieve this. It is already overloaded with this functionality. #include<iostream> #include<string> int main() { int x=40; int y=2; int z=x+y; std::cout<<x<<" + "<<y<<" = "<<z<<std::endl; std::string s1="Hello "; std::string s2="world!"; std::string s3=s1+s2; std::cout<<"""<<s1.c_str()<<"" + ""<<s2.c_str()<<"" = ""<<s3.c_str()<<"""<<std::endl; return(0); }


Program for new and delete operator using c plus plus?

#include<iostream> struct object { int m_data; }; void main() { object obj=new object; obj.m_data = 42; delete( obj ); return( 0 ); }

Related questions

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.


Definition of operator overloading inc plus plus?

to define an additional task to an operator ,we must specify what it means in relation to the class to which the operator is applied.this is done with the help of a special function called operator function ,which describes the task.


What are the disadvantage of operator overloading?

The only disadvantage of operator overloading is when it is used non-intuitively. All operators must behave with predictable results, thus it makes no sense to implement the plus (+) operator so that it behaves like a subtract (-) operator, or a multiply (*) operator, or indeed anything other than the intuitive sum of two objects.


Why is it necessary to overload an operator?

The assignment is done explicit without internal operation. Subject to the programming language, explicit assignment operators are needed wherever implicit ones are insufficient. Implicit assignment is typically implemented as a flat copy, while explicit overloading of the assignment operator allows for any other suitable behavior. Consider this example in pseudocode similar to C++: class Demo { int* valuepointer; ... }; Demo a, b; ... b = a; Assigning a to b using implicit assignment means that a.valuepointer and b.valuepointer share the same value. Both a and b can change the pointed-to value, and the either will "see" the change. This is the required behavior in some cases, but often, you'd want to explicitly assign a to b such that each has its own pointer, accessing different copies of the value. This behavior would require an explicit assignment operator (or copy constructor).


Write a c plus plus program to use plus operator to display the sum of 2 numbers and concatenation of two strings by using operator overloading?

There is no need to overload the plus operator to achieve this. It is already overloaded with this functionality. #include<iostream> #include<string> int main() { int x=40; int y=2; int z=x+y; std::cout<<x<<" + "<<y<<" = "<<z<<std::endl; std::string s1="Hello "; std::string s2="world!"; std::string s3=s1+s2; std::cout<<"""<<s1.c_str()<<"" + ""<<s2.c_str()<<"" = ""<<s3.c_str()<<"""<<std::endl; return(0); }


What is the c plus plus program to calculate the area of a circle using function overloading?

Function overloading is used when you want to re-use the same function name with different argument types or a different number of arguments. Calculating the area of a circle isn't the sort of function that requires overloading since the only argument you need is the radius. double area_of_circle (const double radius) { const double pi=4*atan(1); return pi*radius*radius; }


Program for new and delete operator using c plus plus?

#include<iostream> struct object { int m_data; }; void main() { object obj=new object; obj.m_data = 42; delete( obj ); return( 0 ); }


How a program in c that will add 2 numbers without using any plus operator?

int main() { int a=10,b=20; while(a--) b++; printf("%d",b); } or: a -= -b;


Major c plus plus feature that were removed from java?

Several features that they either made unsafe programs or hard-to-read programs were removed. Some of the features removed include operator overloading; pointers; implicit conversions between numeric and boolean; multiple inheritance.Several features that they either made unsafe programs or hard-to-read programs were removed. Some of the features removed include operator overloading; pointers; implicit conversions between numeric and boolean; multiple inheritance.Several features that they either made unsafe programs or hard-to-read programs were removed. Some of the features removed include operator overloading; pointers; implicit conversions between numeric and boolean; multiple inheritance.Several features that they either made unsafe programs or hard-to-read programs were removed. Some of the features removed include operator overloading; pointers; implicit conversions between numeric and boolean; multiple inheritance.


Write c plus plus program for new and delete operator using class?

#include<iostream> class foo{ int m_data; }; int main() { foo* p=new foo; delete( foo), foo=NULL; return(0); }


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

calloc operator,malloc operator


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++