-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.cpp
More file actions
36 lines (29 loc) · 781 Bytes
/
constructor.cpp
File metadata and controls
36 lines (29 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
using namespace std;
class Student{
public:
string name = "ALI";
// Non-parametrize constructor
Student() {
cout << "Custom Constructor" << endl;
}
// Parametrize constructor (User-Defined Constructor)
Student(string name){
this->name = name; // ' this ' is a pointer ot the current object
}
// Copy constructor
Student(Student &obj){
this->name = obj.name;
}
void getinfo() {
cout << "Name: " << name << endl;
}
};
int main() {
Student s1("ALI"); // Call the parametrize constructor
s1.getinfo();
Student s2(s1); // Call the copy constructor
s2.getinfo();
return 0;
}