Created
April 19, 2017 16:43
-
-
Save tornikegomareli/4e21e000a29bc2ac18b6cead250ab253 to your computer and use it in GitHub Desktop.
Operator overloading in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| class Person | |
| { | |
| private: | |
| string Name; | |
| int Age; | |
| double Weight; | |
| double Height; | |
| public: | |
| Person() | |
| { | |
| cout << " Default Constructor was Called " << endl; | |
| } | |
| Person(string Name) | |
| { | |
| cout << " Overloaded Constructor was Called " << endl; | |
| this->Name = Name; | |
| this->Age = 0; | |
| this->Weight = 0; | |
| this->Height = 0; | |
| } | |
| Person operator+(const Person & other) | |
| { | |
| cout << " Operator + is Overloaded " << endl; | |
| Person test2; | |
| test2.Name = this->Name + other.Name; | |
| test2.Age = this->Age + other.Age; | |
| test2.Height = this->Height + other.Height; | |
| test2.Weight = this->Weight + other.Weight; | |
| return test2; | |
| } | |
| void Initialize(int Age, double Weight, double Height,string Name) | |
| { | |
| cout << " Initialiaze function was called " << endl; | |
| this->Name = Name; | |
| this->Age = Age; | |
| this->Weight = Weight; | |
| this->Height = Height; | |
| } | |
| void PrintPersonStats() | |
| { | |
| cout << " PersonStats Function is called " << endl; | |
| cout << " Name : " << Name << endl; | |
| cout << " Age : " << Age << endl; | |
| cout << " Weight : " << Weight << endl; | |
| cout << " Height : " << Height << endl; | |
| } | |
| }; | |
| int main() | |
| { | |
| Person Giorgi; | |
| Person Daviti; | |
| Person Tornike; | |
| Giorgi.Initialize(26, 76.5, 1.79, "Giorgi"); | |
| Daviti.Initialize(25, 137.5, 1.84, "Daviti"); | |
| Tornike.Initialize(22, 13.2, 44.4, "Tornike"); | |
| Person Test("Test"); | |
| Test = Giorgi + Daviti + Tornike; // test2 | |
| Test.PrintPersonStats(); | |
| cin.get(); | |
| cin.get(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment