tampurus

7 Constructor

Nov 24th, 2021 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class student{
  4.     int x;
  5.     public:
  6.     student(){ // default
  7.         x = 67;
  8.     }
  9.     student(int a){ // parameterized
  10.         x = a;
  11.     }
  12.     student(student & a){ // copy
  13.         x = a.x;
  14.     }
  15.    
  16.     void display(){
  17.         cout<<x<<endl;
  18.     }
  19. };
  20. int main()
  21. {
  22.     student s1;     // defalut
  23.     student s2(40); // parameterized
  24.     student s3(s2); // copy
  25.     s1.display();
  26.     s2.display();
  27.     s3.display();
  28.     return 0;
  29. }
  30. /*
  31. Output
  32. 67
  33. 40
  34. 40
  35. */
Advertisement
Add Comment
Please, Sign In to add comment