You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: C++ Syntax.md
+37-22Lines changed: 37 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,11 +48,14 @@
48
48
49
49
## 1.0 C++ Classes
50
50
### 1.1 Class Syntax
51
-
#### 1.1.1 Class Declaration (`.h` file)
51
+
#### 1.1.1 Class Declaration (`polygon.h` file)
52
52
Here's a simple class representing a polygon, a shape with any number of sides.
53
53
54
-
The class *declaration* typically goes in the `.h` file. The *declaration* gives the class name, any classes it may extend, declares the members and methods, and declares which members/methods are public, private, or protected.
54
+
The class *declaration* typically goes in the header file, which has the extension `.h` (or, less commonly, `.hpp` to distinguish from C headers). The *declaration* gives the class name, any classes it may extend, declares the members and methods, and declares which members/methods are public, private, or protected. You can think of the declaration as sort of saying: "there will be a thing and here's how it will look like". The declaration is used to inform the compiler about the future essence and use of a particular symbol.
55
+
55
56
```c++
57
+
// File: polygon.h
58
+
56
59
#include<string>
57
60
58
61
classPolygon {
@@ -80,14 +83,19 @@ public:
80
83
}; // <--- Don't forget the semicolon!
81
84
```
82
85
83
-
#### 1.1.2 Class Definition (`.cpp` file)
86
+
#### 1.1.2 Class Definition (`polygon.cpp` file)
87
+
The class *definition* typically goes in the `.cpp` file. The *definition* extends the declaration by providing an actual implementation of whatever it is that you're building. Continuing the example from the declaration, the definition can be thought of as saying: "Right, that thing I told you briefly about earlier? Here's how it actually functions". The definition thus provides the compileable implementation.
88
+
84
89
```c++
85
-
#include <string> // explicit is better then implicit
90
+
// File: polygon.cpp
86
91
87
-
#include "Polygon.h" // <--- Obtains the class declaration
92
+
#include <string> // <--- Required for std::string
93
+
94
+
#include "polygon.h" // <--- Obtains the class declaration
88
95
89
96
// Constructor
90
97
// You must scope the method definitions with the class name (Polygon::)
98
+
// Also, see the section on the 'explicit' keyword for a warning about constructors with exactly one argument
91
99
Polygon::Polygon(const int num_sides, const std::string & name) {
92
100
this->num_sides = num_sides; // 'this' is a pointer to the instance of the class. Members are accessed via the -> operator
93
101
this->name = name; // In this case you need to use 'this->...' to avoid shadowing the member variable since the argument shares the same name
#### 1.1.3 Class Utilization (Another `.cpp` file)
125
+
Regarding the use of `this->` in a class definition, there are places where it's strictly necessary for readability, e.g. when your method parameter shares the exact same name as a member variable, you use `this->` to avoid what's called shadowing. However, some prefer to always use `this->` explicitly regardless of whether it's necessary.
126
+
127
+
#### 1.1.3 Class Utilization (Some other `.cpp` file)
118
128
```c++
119
129
#include<string>
120
130
#include<iostream>
121
131
122
132
#include"Polygon.h"// <--- Obtains the class declaration
123
133
124
-
intmain(int argc, char *argv[]) {
134
+
intmain(int argc, char *argv[]) {
125
135
// Create a polygon with 4 sides and the name "Rectangle"
126
136
Polygon polygon = Polygon(4, "Rectangle");
127
137
128
138
// Check number of sides -- Prints "Rectangle has 4 sides"
// Change number of sides to 3 and name to "Triangle"
141
+
// Change number of sides to 3 and rename to "Triangle"
132
142
polygon.SetNumSides(3);
133
143
polygon.SetName("Triangle");
134
144
}
@@ -152,22 +162,26 @@ public:
152
162
};
153
163
```
154
164
155
-
Another important consideration: If you have getters and setters for all of your members, you may want to reconsider the design of your class. It is more often than not that having getters and setters for every member is indicative of poor planning of the class design and interface. Getters are very common, but setters should be used more carefully. Should you have set the variable in the constructor? Is it set somewhere else in another method, perhaps even indirectly?
165
+
This is often used for very basic getters and setters, and also for basic constructors. In contrast, you'll nearly always find more complex methods defined in the `.cpp` file. One exception to this is with class templates, in which the entire templated class declaration and definition must reside in the header file.
166
+
167
+
Another important consideration: If you have getters and setters for all of your members, you may want to reconsider the design of your class. Sometimes having getters and setters for every member is indicative of poor planning of the class design and interface. In particular, setters should be used more thoughtfully. Could a variable be set once in the constructor and left constant thereafter? Does it need to be modified at all? Is it set somewhere else in another method, perhaps even indirectly?
156
168
157
169
### 1.2 Inheritance
158
170
A class can extend another class, meaning that the new class inherits all of the data from the other class, and can also override its methods, add new members, etc. Inheritance is the key feature required for polymorphism.
159
-
P. S. it is very important for a beginner not to overuse this feature(because human's brain tends to create hierarchies,
160
-
even where it is not needed). There are some good alternatives like [composition](https://en.wikipedia.org/wiki/Composition_over_inheritance) and [aggregation](https://stackoverflow.com/a/269535)
161
171
162
-
**Example:** the class `Rectangle` can inherit the class `Polygon`. You would then say that `Rectangle` extends `Polygon`, or that class `Rectangle` is a sub-class of `Polygon`. In plain English, this means that a `Rectangle` is a more specialized version of a `Polygon`.
172
+
It is important to note that this feature is often overused by beginners and sometimes unnecessary hierarchies are created, adding to the overally complexity. There are some good alternatives such as [composition](https://en.wikipedia.org/wiki/Composition_over_inheritance) and [aggregation](https://stackoverflow.com/a/269535), although, of course, sometimes inheritance is exactly what is needed.
173
+
174
+
**Example:** the class `Rectangle` can inherit from the class `Polygon`. You would then say that a `Rectangle` extends from a `Polygon`, or that class `Rectangle` is a sub-class of `Polygon`. In plain English, this means that a `Rectangle` is a more specialized version of a `Polygon`. Thus, all rectangles are polygons, but not all polygons are rectangles.
0 commit comments