-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.cpp
More file actions
59 lines (44 loc) · 1.58 KB
/
constructor.cpp
File metadata and controls
59 lines (44 loc) · 1.58 KB
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
#include <iostream>
class Student{
public:
std::string name;
int age;
double gpa;
Student(std::string name)//if param names are different from default variable names above you do not need this->
{
this->name = name;
}
Student(std::string name, int age)//if param names are different from default variable names above you do not need this->
{
this->name = name;
this->age = age;
}
Student(std::string name, int age, double gpa)//if param names are different from default variable names above you do not need this->
{
this->name = name;
this->age = age;
this->gpa = gpa;
}
};
int main()
{
//constructor = special method that is automatically called when an object is instantiated
// useful for assigning values to attributes as arguments
//
// Overloaded constructors = multiple constuctors w/ same name but different
// parameters. allows for varying arguments when
// instantiating an object
Student student1("Kyle");
Student student2("Jake", 20);
Student student3("Seb", 21, 3.0);
std::cout << student1.name << '\n';
//std::cout << student1.age << '\n';
//std::cout << student1.gpa << '\n';
std::cout << student2.name << '\n';
std::cout << student2.age << '\n';
//std::cout << student2.gpa << '\n';
std::cout << student3.name << '\n';
std::cout << student3.age << '\n';
std::cout << student3.gpa << '\n';
return 0;
}