Skip to content

Instantly share code, notes, and snippets.

@rajan9519
Created April 17, 2020 10:02
Show Gist options
  • Select an option

  • Save rajan9519/10d9cd65c93f000d026ccd4de81944c6 to your computer and use it in GitHub Desktop.

Select an option

Save rajan9519/10d9cd65c93f000d026ccd4de81944c6 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
class student
{
private:
string name;
int rollNumeber;
public:
student(); // custom default Constructor
student(const student & obj); // custom copy constructor
student & operator=(const student & obj); // custom copy assignment operator
};
student::student()
{
name = "No Name";
rollNumeber = 0;
}
student::student(const student & obj)
{
cout<<"custom copy constructor is called\n";
name = obj.name;
rollNumeber = obj.rollNumeber;
}
student & student::operator=(const student & obj)
{
cout<<"custom assignment operator is called\n";
name = obj.name;
rollNumeber = obj.rollNumeber;
return *this;
}
void test1(student s)
{
// testing function
}
student test2(student s)
{
return s;
}
int main()
{
student s1,s2;
student s3 = s2; // copy constructor is called
test1(s3); // copy constructor is called
s3 = test2(s3); // copy constructor is called and then assignment operator for "="
s2 = s3; // assignment operator is called
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment