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
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -520,9 +520,9 @@ the other version of this is:
520
520
int a = 6, b = 3;
521
521
522
522
int c = add(a, b);
523
-
intc = subtract(a, b);
524
-
intc = multiply(a, b);
525
-
intc = divide(a, b);
523
+
intd = subtract(a, b);
524
+
inte = multiply(a, b);
525
+
intf = divide(a, b);
526
526
```
527
527
Which do you like better? First version is more intuitive as for me. The same logic stands behind `operator`s in c++. One of the mottos in c++ is "Create your classes to function as harmoniously as the built-in types". So let's create harmonious
528
528
class complex with support for basic arithmethic.
@@ -577,7 +577,7 @@ int main()
577
577
}
578
578
```
579
579
Oh, that's good, but what about output, how could we output it?
580
-
It turns out that output is an operator too. Remember `std::cout << something` ? That's an right shift operator, but it is used to denote the output, and `std::cout` is just the global instance of class `std::ostream`. So we could just provide this operator for our own class:
580
+
It turns out that output is an operator too. Remember `std::cout << something` ? That's a left shift operator, but it is used to denote the output, and `std::cout` is just the global instance of class `std::ostream`. So we could just provide this operator for our own class:
581
581
```c++
582
582
#include <iostream>
583
583
@@ -609,7 +609,7 @@ int main()
609
609
std::cout << a / b;
610
610
}
611
611
```
612
-
There are also left shift operator denoting input, recall `std::cin >> something;`. About different kinds of operators, you can read [here](http://en.cppreference.com/w/cpp/language/operators).
612
+
There are also right shift operator denoting input, recall `std::cin >> something;`. About different kinds of operators, you can read [here](http://en.cppreference.com/w/cpp/language/operators).
613
613
614
614
### 1.6 Templates
615
615
Let's try to write `add` function from previous item.
Cool, but this won't work with `int`, and many other types! Templates is a mechanism that allows generalizing of the function, that works with every type that supports `operator+`, here is the notation:
622
+
Cool, but this won't work with `int`, and many other types! Templates is a mechanism that allows generalizing of a function, that works with every type that supports `operator+`, here is the notation:
623
623
```c++
624
624
template <typename T> // T is the name of a type
625
625
T add(const T& fst, const T& snd)
@@ -665,7 +665,7 @@ Also the new feature is coming to c++20, named [concepts](https://cppdepend.com/
665
665
666
666
## 2.0 General C++ Syntax
667
667
### 2.1 Namespaces
668
-
In big projects, there are thousands of varibles, and each neede it's own name. Let's imagine that there are library with linked list structure, and we need to somehow represent single node of a list:
668
+
In big projects, there are thousands of variables, and each needs it's own name. Let's imagine that there are library with linked list structure, and we need to somehow represent single node of a list:
669
669
```c++
670
670
// file named "LinkedList.h"
671
671
template <typename T> // use of templates
@@ -676,7 +676,7 @@ struct Node { // pretty and short name
676
676
T data;
677
677
};
678
678
```
679
-
And there library with binary search tree structure:
679
+
And there are library with binary search tree structure:
680
680
```c++
681
681
// file named "BST.h"
682
682
template <typename T>
@@ -697,7 +697,7 @@ int main()
697
697
Node<int> * a = new Node<int> {nullptr, nullptr, 3}; // oops
698
698
};
699
699
```
700
-
How do compiler know what Node we are about to use? `namespace` comes to rescue:
700
+
How do compiler know what Node we are about to use? `namespace` comes to the rescue:
701
701
```c++
702
702
// "LinkedList.h"
703
703
namespacelst {
@@ -728,12 +728,12 @@ int main()
728
728
bst::Node<int> * a = new bst::Node<int> {nullptr, nullptr, 3}; // it is bst node
729
729
};
730
730
```
731
-
So now we could specialize, and don't be afraied of name collisions, just use simple, pretty names.
732
-
By the way remember `std::cout`? It is object of `namsepaces std`, it is recommended to use full namespace name almost everywhere. But if it is your small(very small) project you could try:
731
+
So now you could specialize, and don't be afraid of name collisions, just use simple, pretty names.
732
+
By the way remember `std::cout`? It is object of `namsepaces std`, it is recommended to use full namespace names almost everywhere, because you are sure what structure you are using. But if it is your small(very small) project you could try:
0 commit comments