SabirSazzad

Deep Copy

Feb 23rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class Student{
  4. private:
  5.     int *marks;
  6.     int numOfCourse;
  7. public:
  8.     Student():Student(1){}
  9.     Student(int numOfCourse){
  10.         this->numOfCourse = numOfCourse;
  11.         this->marks = new int[this->numOfCourse];
  12.  
  13.         this->takeMarks();
  14.     }
  15.     Student operator=(const Student& s)
  16.     {
  17.         Student tempStudent;
  18.         tempStudent.marks = s.marks;
  19.         tempStudent.numOfCourse = s.numOfCourse;
  20.  
  21.         return tempStudent;
  22.     }
  23.     void takeMarks(){
  24.         cout<<"Enter "<<this->numOfCourse << "  marks for this student\n";
  25.         for(int i=0; i<this->numOfCourse; i++)
  26.             cin>>this->marks[i];
  27.     }
  28.  
  29.     void showMarks(){
  30.      for(int i=0; i<this->numOfCourse; i++)
  31.         cout<<this->marks[i]<<"  ";
  32.      cout<<endl;
  33.     }
  34.     ~Student(){
  35.       delete [] this->marks;
  36.     }
  37. };
  38.  
  39.  
  40. int main(){
  41. Student *s1 = new Student;
  42. Student s2;
  43. s2 = *s1;
  44. delete s1;
  45.  
  46. s2.showMarks();
  47.  
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment