-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.cpp
More file actions
37 lines (30 loc) · 874 Bytes
/
example2.cpp
File metadata and controls
37 lines (30 loc) · 874 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
37
// what are constructors and how to declare them
#include <iostream>
#include <string>
using namespace std;
class BuckysClass
{
public:
BuckysClass(string z) // default constructor always similar to class name
{
setName(z);
}
void setName(string x)
{
name = x;
}
string getName()
{
return name;
}
private:
string name;
};
int main()
{
BuckysClass obj1("Jason"); // constructor OBJECT(string z), reason why we aren't calling setName is because BuckysClass already setsName (extended version of example1.cpp)
cout << obj1.getName(); // call getName to return name
BuckysClass obj2("Hong"); // constructor OBJECT(string z)
cout << obj2.getName(); // call getName to return name
return 0;
}