c++ operator overloading
C++
// money.h -- define the prototype
class Money
{
public:
Money & operator += (const Money &rhs);
}
// money.cpp -- define the implementation
Money& Money :: operator += (const Money &rhs)
{
// Yadda Yadda
return *this;
}#include <iostream>
using namespace std;
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Main function for the program
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}#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;
}
Also in C++:
- Title
- hello world c++
- Category
- C++
- Title
- check if intent has extras
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- deque c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- multiple words C++ in same
- Category
- C++
- Title
- how to avoid tle in c++
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- recursive in c++
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- stringstream in c++
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- range based for loop c++ with reference
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- matrix class in c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- c++ replace substrings
- Category
- C++
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- how to print 5 precision float in c++
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- ceil in c++
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- array sort c++
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- uepic games github
- Category
- C++
- Title
- how to convert a string to a double c++
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- c++ vector
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- free or delete in c++
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- c++ delet from memory
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- primeros numeros primos
- Category
- C++
- Title
- find height of a tree
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- All palindromic substrings
- Category
- C++
- Title
- find all occurrences of a substring in a string c++
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- c++ map insert
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- double to float c++
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- intersection between vector c++
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- char to string c++
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- fail() in c++
- Category
- C++
- Title
- widechartomultibyte
- Category
- C++