Answers for "operator overloading cpp"

C++
2

c++ overloaded == operator

#include <iostream>
#include <string>
 
class Car
{
private:
    std::string m_make;
    std::string m_model;
 
public:
    Car(const std::string& make, const std::string& model)
        : m_make{ make }, m_model{ model }
    {
    }
 
    friend bool operator== (const Car &c1, const Car &c2);
    friend bool operator!= (const Car &c1, const Car &c2);
};
 
bool operator== (const Car &c1, const Car &c2)
{
    return (c1.m_make== c2.m_make &&
            c1.m_model== c2.m_model);
}
 
bool operator!= (const Car &c1, const Car &c2)
{
    return !(c1== c2);
}
 
int main()
{
    Car corolla{ "Toyota", "Corolla" };
    Car camry{ "Toyota", "Camry" };
 
    if (corolla == camry)
        std::cout << "a Corolla and Camry are the same.n";
 
    if (corolla != camry)
        std::cout << "a Corolla and Camry are not the same.n";
 
    return 0;
}
Posted by: Guest on June-17-2020
0

overload the >> operator in c++

istream &operator>>( istream  &input, Class_Name &c )
Posted by: Guest on December-01-2020
1

c++ overload < operator

struct Foo
{
    double val;
    friend bool operator<(const Foo& l, const Foo& r)
    {
      	//Custom comparison for l < r goes here
        return l.val < r.val; 
    }
};
Posted by: Guest on March-16-2021
0

overload >> operator c++ in class

istream& operator>>(istream& is, node*& head)
{
    // Function call to overload the ">>"
    // operator
    takeInput(head);
}
Posted by: Guest on September-21-2021
1

c++ over load operator

// This will substract one vector (math vector) from another
// Good example of how to use operator overloading

vec2 operator - (vec2 const &other) {
    return vec2(x - other.x, y - other.y);
}
Posted by: Guest on February-12-2021

Code answers related to "operator overloading cpp"

Browse Popular Code Answers by Language