-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstructors2.cpp
65 lines (47 loc) · 1.55 KB
/
constructors2.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;
class User{
public:
string FirstName;
string LastName;
int Age;
string Email;
//constrcutor rules:
//constructors need to have the same name as the class
//they dont have a return type
//constructors need to be public
//default constrcutor doesnt have any parametrs
//if you dont create a default constrcutor yourself, C++ creates one for you
//default constrcutors are invoked when you create an object of the class
//when you create a constrcutor, you lose the defaul one provided by C==
//User(){} empty default constrcutor looks like this
//default constrcutor with some set values
User(){
FirstName = "n";
LastName = "ln";
Age = 0;
Email = "none";
}
//parameterized constrcutor
User(string fname, string lname, int age){
FirstName = "fname";
LastName = "lname";
Age = age;
Email = fname + lname + "@mail.com"; //generates email based on paramters
}
};
//non member global function
void GetUserinfo(User u){
cout << "FirstName: " << u.FirstName << endl;
cout << "LastName: " << u.LastName << endl;
cout << "Age: " << u.Age << endl;
cout << "Email: " << u.Email << endl;
}
int main(){
//User user1;
//without a constrcutir you would need to manually assign values to each property
//user1.Age = 10;
User user1("Perly", "D", 20);
//you can choose which constrcutor you want to incoke, but only one cane be invoked
GetUserinfo(user1);
}