• Ternary operator ( ?:) Operator Overloading Syntax ReturnType classname :: Operator Operator Symbol (argument list) \\ Function body } Keyword Operator to be overloaded Chapter Page - - . . Restrictions on Operator Overloading Following are some restrictions to be kept in mind while implementing operator overloading. . Precedence and Associativity of an operator cannot be changed. . No new operators can be created, only existing operators can be overloaded. . Cannot redefine the meaning of an operator’s procedure. You cannot change how integers are added.Only additional functions can be given to an operator . Overloaded operators cannot have default arguments. . When binary operators are overloaded, the left hand object must be an object of the relevant class #include<iostream> using namespace std; class complex { int real,img; public: void read() cout<<"\nEnter the REAL PART : "; cin>>real; cout<<"\nEnter the IMAGINARY PART : "; cin>>img; complex operator +(complex c2) complex c3; c3.real=real+c2.real; c3.img=img+c2.img; return c3; void display() { cout<<real<<"+"<<img<<"i"; } }; int main() complex c1,c2,c3; int choice, cont; cout<<"\n\nEnter the First Complex Number"; c1.read(); cout<<"\n\nEnter the Second Complex Number"; c2.read(); c3=c1+c2; // binary + overloaded cout<<"\n\nSUM = "; c3.display(); return ; Output Enter the First Complex Number Enter the REAL PART : Enter the IMAGINARY PART : Enter the Second Complex Number Enter the REAL PART : Enter the IMAGINARY PART : SUM = +12i Illustration . Binary operator overloading using ‘+’in Complex number addition Chapter Page - - #include<string.h> #include<iostream> using namespace std; class strings public: char s[ ]; void getstring(char str[]) strcpy(s,str); void operator+(strings); }; void strings::operator+(strings ob) strcat(s,ob.s); cout<<"\nConcatnated String is:"<<s; int main() strings ob1, ob2; char string1[ ], string2[ ]; cout<<"\nEnter First String:"; cin>>string1; ob1.getstring(string1); cout<<"\nEnter Second String:"; cin>>string2; ob2.getstring(string2); //Calling + operator to Join/Concatnate strings ob1+ob2; return ; Output Enter First String:COMPUTER Enter Second String:SCIENCE Concatnated String is:COMPUTERSCIENCE Illustration . Concatenation of string using operator overloading Chapter Page - -
📖 Samacheer Kalvi · 11th TN - English Medium · Computer Science · Page 259poem
• Ternary operator ( ?:)
Chapter 3: 4 · Computer Science
Related topics
Have a question about this topic?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →