c++ overload operator
C++
#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;
}// 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>
#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;
}#include <iostream>
using namespace std;
class Test
{
private:
int count;
public:
Test(): count(5){}
void operator ++()
{
count = count+1;
}
void Display() { cout<<"Count: "<<count; }
};
int main()
{
Test t;
// this calls "function void operator ++()" function
++t;
t.Display();
return 0;
}
Also in C++:
- Title
- hashset in c++
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- disjoint set code in c++
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- random number in c++
- Category
- C++
- Title
- cannot jump from switch statement to this case label c++
- Category
- C++
- Title
- c++ vector pop_back
- Category
- C++
- Title
- c++ declare variable
- Category
- C++
- Title
- creare array con c++
- Category
- C++
- Title
- Find the minimum difference between pairs in a simple path of tree C++
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- run cmd command c++
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- c++ delet from memory
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- extends c++
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- how to convert a string to a double c++
- Category
- C++
- Title
- error: invalid use of template-name without an argument list
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- char vector to string c++
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- how to sort a vector in reverse c++
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- templates of templates c++
- Category
- C++
- Title
- how to import getline in c++
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- how to end a c++ program early
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- nth_element c++
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- find_if c++ example
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- double to string c++
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- what does the modularity mean in c++
- Category
- C++
- Title
- Find a element in a map C++
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- c++ set console title
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- c++ enum rand
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- variable sized arrays hackerrank solution in c++
- Category
- C++
- Title
- class is replace by structure
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- newline in c++
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- c++ compare char array
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- c++ print one line to console instead of multiple
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- kruskal c++
- Category
- C++
- Title
- solve linear equations geeksforgeeks
- Category
- C++
- Title
- font awesome bootstrap cdn
- Category
- C++
- Title
- c++ raw string
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- range of int
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- min in vector c++
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- char to string c++
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- how to run a c++ program in the background
- Category
- C++
- Title
- char **
- Category
- C++