Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to use operator overloading in C++

1 Answer

0 votes
#include <iostream>

class Example {
public: 
    int a;
    int b;

    Example() {} 
  
    Example(int a, int b) { 
        this->a = a;
        this->b = b;
    }
  
    Example operator+(Example obj) {
        Example result; 
        result.a = this->a + obj.a;
        result.b = this->b + obj.b;
        
        return result;
    }
};
  
int main() {
    Example obj1(7, 9);
    Example obj2(2, 4);
    Example sum;
  
    sum = obj1 + obj2;
  
    std::cout << sum.a << " " << sum.b;
}




/*
run:

9 13

*/

 



answered Dec 2, 2022 by avibootz

Related questions

1 answer 208 views
1 answer 232 views
1 answer 205 views
2 answers 254 views
1 answer 237 views
237 views asked Sep 6, 2015 by avibootz
1 answer 214 views
...