forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_member_functions.cpp
More file actions
84 lines (64 loc) · 1.95 KB
/
static_member_functions.cpp
File metadata and controls
84 lines (64 loc) · 1.95 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*******************************************************************************
*
* Program:
*
* Description: Demonstration of how to use cin to accept user input with C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=1xY0rF399j0
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
// A simple square shape type
class Square {
private:
int side_length;
// static member variables, also know as class variables, are
// variables of the class rather than specific to an individual
// object instance
static int total_squares;
// static member functions can be private, and only accessible within
// the class
static void incrementTotal()
{
// static member functions can alter static member variables
total_squares++;
}
public:
Square(int length)
{
side_length = length;
// we can call private static member functions in non-static member
// functions
incrementTotal();
}
int area()
{
return side_length * side_length;
}
// public static member functions are available outside the class definition,
// such as in the main function
static int getTotal()
{
return total_squares;
}
};
// initialize the private member variable
int Square::total_squares = 0;
int main()
{
Square square1(5);
cout << "square1 area: " << square1.area() << endl;
// We can access the getTotal() static member function with the syntax
// ClassName::FunctionName()
cout << "total squares: " << Square::getTotal() << endl;
Square square2(10);
cout << "square2 area: " << square2.area() << endl;
// We can also access the getTotal() static member function with any specific
// object instance, but using the ClassName::FunctionName() syntax is a
// best practice
cout << "total squares: " << square2.getTotal() << endl;
return 0;
}