C++ Program For Copy Constructor
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Program for copy constructor
#include<iostream>
#include<string.h>
#define clrscr() system("cls")
using namespace std;
class Student
{
private:
int rno;
char name[50];
public:
Student(int rno,char name[])
{
this->rno=rno;
strcpy(this->name,name);
}
Student()
{}
Student(Student &S)
{
this->rno=S.rno;
strcpy(this->name,S.name);
}
void display()
{
cout<<"Roll No: "<<rno;
cout<<"\nName: "<<name;
}
};
int main()
{
clrscr();
Student S1(101,"vishal");
Student S2=S1; // Copy Constructor
S2.display();
}
Did you like this article? If Yes, please give DataFlair 5 Stars on Google

