Last active
October 2, 2019 12:15
-
-
Save Krythz43/ad0984a4f9be7f496e034c6c1182cb21 to your computer and use it in GitHub Desktop.
Testing gists with Constructors for C++14 structures
This file contains 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 <bits/stdc++.h> | |
using namespace std; | |
struct Complex { | |
int real, imag; | |
Complex(int r = 0, int i =0) {real = r; imag = i;} | |
Complex operator + (Complex const &obj) { | |
// Note that the operator on left is passed as a refrence and can be modified directly by modifying *this. | |
Complex res; | |
res.real = real + obj.real; | |
res.imag = imag + obj.imag; | |
return res; | |
} | |
void print() { cout << real << " + i" << imag << endl; } | |
}; | |
int main() | |
{ | |
Complex c1(10, 5), c2(2, 4); | |
Complex c3 = c1 + c2; // An example call to "operator+" | |
c3.print(); | |
c1.print(); | |
c2.print(); | |
} |
This file contains 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 <bits/stdc++.h> | |
using namespace std; | |
struct st{ | |
int mn; | |
int x; | |
string name; | |
st(){ | |
mn=50; | |
x=80; | |
name="Krithick"; | |
}; | |
}; | |
int main(){ | |
st s1; | |
cout<<s1.mn<<endl; | |
cout<<s1.x<<endl; | |
cout<<s1.name<<endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment