-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.constructor.cpp
58 lines (50 loc) · 1.69 KB
/
15.constructor.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
#include <iostream>
// constructor is basically a special type of method which runs every time we instantiate an object
// class Player
// {
// public:
// float x, y;
// void Init()
// {
// x = 0.0f;
// y = 0.0f;
// }
// void Print()
// {
// std::cout << x << "," << y << std::endl;
// }
// };
// int main()
// {
// Player p;
// p.Init(); // this will make them to output 0 instead of random variable
// p.Print(); // without init it will output a random values because when we instantiated it and allocated memory fot it
// // we didnt initialize that memory we got whatever was left over in that memory space
// // so we need initiallization so we create init method but it cause extra code so we use constructers
// std::cin.get();
// }
// using constructors
class Player
{
public:
int x, y;
// Player() = delete; // this will delete default constructor
Player() // it doesnt have return and its name must match the class name, we can optionally give it parameters
{
x = 0;
y = 0;
} // if we dont write a constructer we will still have it but its default constructer but it doesnt do anything
// i can write as many contructor as i want
void Print()
{
std::cout << x << "," << y << std::endl;
}
};
int main()
{
Player p; // if contructor have entity we can instatiate class like this: Player p(0, 0)
// contructors wont run if we dont instantiate methods so if we use static methods they wont run
// if we make the contructor private we cant instantiate class liket this: Player p but we can use its static methods
p.Print();
std::cin.get();
}