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
+48-59Lines changed: 48 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -716,8 +716,8 @@ int main() {
716
716
You can also similiarly overload the input stream operator (`>>`), and can read more about the various operators [here](http://en.cppreference.com/w/cpp/language/operators).
717
717
718
718
### 1.6 Templates
719
-
Templates are a very powerful abstraction allowing you to generate compile-time methods for any number of types by providing only
720
-
one implementation.
719
+
Templates are a very powerful abstraction allowing you to generate compile-time methods/classes/etc. for any number of types while
720
+
writing only one implementation.
721
721
722
722
Say you have a method that adds two floating point number together, and another to add two integers together:
723
723
@@ -731,9 +731,9 @@ int Add(const int a, const int b) {
731
731
}
732
732
```
733
733
734
-
That's great, but since both floating point numbers and integers implement the `+` operator and it has the same syntactic meaning,
735
-
you can use a template to instead write one generic implementation of a method that can operate on doubles, ints, floats, and (in this
736
-
case) any other type that implements the `+` operator.
734
+
That's great, but since both floating point numbers and integers implement the `+` operator you can use a template to instead
735
+
write one generic implementation of a method that can operate on doubles, ints, floats, and (in this case) any other type that
736
+
implements the `+` operator.
737
737
738
738
A simple templatized version of `Add` would look something like this:
739
739
@@ -755,6 +755,9 @@ int main() {
755
755
}
756
756
```
757
757
758
+
In this simple example the compiler would generate four different methods, one for each type. Templating allows you to write more
759
+
concise and modular code at the expense of generating a larger executable (code bloat).
760
+
758
761
Templates are especially useful to create class templates. Class templates must be completely defined in a single header file.
759
762
760
763
```c++
@@ -782,85 +785,71 @@ Read more about templates [here](https://www.geeksforgeeks.org/templates-cpp/) a
782
785
783
786
## 2.0 General C++ Syntax
784
787
### 2.1 Namespaces
785
-
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:
786
-
```c++
787
-
// file named "LinkedList.h"
788
-
template <typename T> // use of templates
789
-
structNode { // pretty and short name
790
-
Node* next;
791
-
Node* prev;
788
+
In a large production project you may have thousands of symbols for various types, variables, methods, and so on. To avoid symbol names conflicting
789
+
with one another you can use namespaces to logically separate symbol names in to broad categories. Namespaces are an inherent feature of C++; when you
790
+
create a class and refer to a method as `ClassName::Method()` you are essentially using a namespace feature intrinsic to classes.
792
791
793
-
T data;
794
-
};
795
-
```
796
-
And there are library with binary search tree structure:
797
-
```c++
798
-
// file named "BST.h"
799
-
template <typename T>
800
-
struct Node {
801
-
Node* left;
802
-
Node * right;
792
+
For a brief namespace example, suppose that you have two data structures, both of which implement a `Node` class. In the following code, namespaces
793
+
are used to allow the compiler (and the programmer) to distinguish between the two types.
803
794
804
-
T data;
805
-
};
806
-
```
807
-
We want to create our project that uses both of this libraries:
808
795
```c++
809
-
#include"LinkedList.h"
810
-
#include"BST.h"
796
+
// File: list.h
797
+
798
+
namespacelist {
811
799
812
-
intmain()
813
-
{
814
-
Node<int> * a = new Node<int> {nullptr, nullptr, 3}; // oops
815
-
};
816
-
```
817
-
How do compiler know what Node we are about to use? `namespace` comes to the rescue:
818
-
```c++
819
-
// "LinkedList.h"
820
-
namespacelst {
821
800
template <typenameT>
822
801
struct Node {
823
-
// ...
824
-
};
802
+
Node * next;
803
+
Node * prev;
804
+
T data;
825
805
};
806
+
807
+
}; // namespace
826
808
```
827
-
and
809
+
828
810
```c++
829
-
// "BST.h"
811
+
// File: bst.h
812
+
830
813
namespace bst {
814
+
831
815
template <typename T>
832
816
struct Node {
833
-
// ...
834
-
};
817
+
Node * left;
818
+
Node * right;
819
+
T data;
835
820
};
821
+
822
+
}; // namespace
836
823
```
837
-
and usage is:
824
+
838
825
```c++
839
-
#include"LinkedList.h"
840
-
#include"BST.h"
826
+
// File: main.cpp
827
+
#include"list.h"
828
+
#include"bst.h"
841
829
842
-
intmain()
843
-
{
844
-
lst::Node<int> * a = new lst::Node<int> {nullptr, nullptr, 3}; // it is linked list node
845
-
bst::Node<int> * a = new bst::Node<int> {nullptr, nullptr, 3}; // it is bst node
830
+
intmain() {
831
+
list::Node<int> a;
832
+
bst::Node<int> b;
846
833
};
847
834
```
848
-
So now you could specialize, and don't be afraid of name collisions, just use simple, pretty names.
849
-
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:
835
+
836
+
The standard C++ library uses the namespace `std`, e.g. `std::cout`, `std::string`, `std::endl`, etc. While you can use a `using namespace foo;` directive to address
837
+
symbols directly in the `foo` namespace without prefixing the `foo::` qualifier, this is generally considered bad practice as it pollutes the global namespace and
838
+
sort of undermines the point of using namespaces in the first place.
cout << "Hello, World" << endl; // <--- BAD: pollutes the global namespace
845
+
```
854
846
855
-
intmain()
856
-
{
857
-
cout << "Hello, namespace!"
847
+
```c++
848
+
#include<iostream>
858
849
859
-
return 0;
860
-
}
850
+
std::cout << "Hello, World" << std::endl; // <--- GOOD: It's clear that you're using symbols from the standard namespace
861
851
```
862
852
863
-
864
853
### 2.2 References and Pointers
865
854
are used to store the address of the varibale/object in memory. So having the pointer or reference, you could do the same operations as that of object being pointed to.
0 commit comments