C++ Program For Copy Constructor

Free C++ course with real-time projects 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();
      
}

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *