From ebdde372443c0fe51b2513c9bfcfad8378ae52cd Mon Sep 17 00:00:00 2001 From: Riku-Pekka Silvola Date: Wed, 25 Feb 2015 18:10:57 +0100 Subject: fixed some typos --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 5f80f26f..67fa054c 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -492,7 +492,7 @@ bool doSomethingWithAFile(const char* filename) { FILE* fh = fopen(filename, "r"); // Open the file in read mode if (fh == nullptr) // The returned pointer is null on failure. - reuturn false; // Report that failure to the caller. + return false; // Report that failure to the caller. // Assume each function returns false if it failed if (!doSomethingWithTheFile(fh)) { @@ -513,7 +513,7 @@ bool doSomethingWithAFile(const char* filename) { FILE* fh = fopen(filename, "r"); if (fh == nullptr) - reuturn false; + return false; if (!doSomethingWithTheFile(fh)) goto failure; -- cgit v1.2.3 From c88c28058b34c84de853694eb71d402fee937168 Mon Sep 17 00:00:00 2001 From: Arnie97 Date: Fri, 13 Mar 2015 23:40:45 +0800 Subject: Closes #1000 --- c++.html.markdown | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 67fa054c..1978d183 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -30,10 +30,10 @@ one of the most widely-used programming languages. // C++ is _almost_ a superset of C and shares its basic syntax for // variable declarations, primitive types, and functions. -// However, C++ varies in some of the following ways: -// A main() function in C++ should return an int, -// though void main() is accepted by most compilers (gcc, clang, etc.) +// Just like in C, your program's entry point is a function called +// main with an integer return type, +// though void main() is also accepted by most compilers (gcc, clang, etc.) // This value serves as the program's exit status. // See http://en.wikipedia.org/wiki/Exit_status for more information. int main(int argc, char** argv) @@ -51,6 +51,8 @@ int main(int argc, char** argv) return 0; } +// However, C++ varies in some of the following ways: + // In C++, character literals are one byte. sizeof('c') == 1 -- cgit v1.2.3 From 299f3de0eacfe4e3d1303135716ad14a09bb0118 Mon Sep 17 00:00:00 2001 From: Arnie97 Date: Sat, 11 Apr 2015 12:52:23 +0800 Subject: [c++/en] Fix spelling --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 1978d183..1a84efa4 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -555,7 +555,7 @@ void doSomethingWithAFile(const char* filename) // Compare this to the use of C++'s file stream class (fstream) // fstream uses its destructor to close the file. // Recall from above that destructors are automatically called -// whenver an object falls out of scope. +// whenever an object falls out of scope. void doSomethingWithAFile(const std::string& filename) { // ifstream is short for input file stream -- cgit v1.2.3 From 7d5368eda1e2402b6b2bed85586fde0c6af87816 Mon Sep 17 00:00:00 2001 From: MoreMoschops Date: Sun, 26 Apr 2015 15:33:29 +0100 Subject: Neither gcc nor clang accept void main. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Neither gcc nor clang accept void main. Remove this bad information. Tested as follows: $ cat 045.cpp void main() { } $ g++ 045.cpp 045.cpp:1:11: error: ‘::main’ must return ‘int’ void main() ^ $ clang++ 045.cpp 045.cpp:1:1: error: 'main' must return 'int' void main() ^~~~ int 1 error generated. $ g++ --version g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2 $ clang++ --version Ubuntu clang version 3.4.2-3ubuntu2~xedgers (tags/RELEASE_34/dot2-final) (based on LLVM 3.4.2) --- c++.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 1a84efa4..ae93ceba 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -32,8 +32,7 @@ one of the most widely-used programming languages. // variable declarations, primitive types, and functions. // Just like in C, your program's entry point is a function called -// main with an integer return type, -// though void main() is also accepted by most compilers (gcc, clang, etc.) +// main with an integer return type. // This value serves as the program's exit status. // See http://en.wikipedia.org/wiki/Exit_status for more information. int main(int argc, char** argv) -- cgit v1.2.3 From c21cf5a1e230dadf81bc4e31e2d2f9133551b365 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 4 May 2015 02:40:04 -0600 Subject: Templates and such --- c++.html.markdown | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index ae93ceba..10c39c9c 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -432,6 +432,81 @@ int main () { return 0; } +///////////////////// +// Templates +///////////////////// + +// Templates in C++ are mostly used for generic programming, though they are +// much more powerful than generics constructs in other languages. It also +// supports explicit and partial specialization, functional-style type classes, +// and also it's Turing-complete. + +// We start with the kind of generic programming you might be familiar with. To +// define a class or function that takes a type parameter: +template +class Box { + // In this class, T can be used as any other type. + void insert(const T&) { ... } +}; + +// During compilation, the compiler actually generates copies of each template +// with parameters substituted, and so the full definition of the class must be +// present at each invocation. This is why you will see template classes defined +// entirely in header files. + +// To instantiate a template class on the stack: +Box intBox; + +// and you can use it as you would expect: +intBox.insert(123); + +// You can, of course, nest templates: +Box > boxOfBox; +boxOfBox.insert(intBox); + +// Up until C++11, you muse place a space between the two '>'s, otherwise '>>' +// will be parsed as the right shift operator. + +// You will sometimes see +// template +// instead. The 'class' keyword and 'typename' keyword are _mostly_ +// interchangeable in this case. For full explanation, see +// http://en.wikipedia.org/wiki/Typename +// (yes, that keyword has its own Wikipedia page). + +// Similarly, a template function: +template +void barkThreeTimes(const T& input) +{ + input.bark(); + input.bark(); + input.bark(); +} + +// Notice that nothing is specified about the type parameters here. The compiler +// will generate and then type-check every invocation of the template, so the +// above function works with any type 'T' that has a const 'bark' method! + +Dog fluffy; +fluffy.setName("Fluffy") +barkThreeTimes(fluffy); Prints "Fluffy barks" three times. + +// Template parameters don't have to be classes, though this is used very rarely: +template +void printMessage() { + cout << "Learn C++ in " << Y << " minutes!" << endl; +} + +// And you can explicitly specialize templates for more efficient code: +template<> +void printMessage<10>() { + cout << "Learn C++ faster in only 10 minutes!" << endl; +} + +printMessage<20>(); // Prints "Learn C++ in 20 minutes!" +printMessage<10>(); // Prints "Learn C++ faster in only 10 minutes!" + + ///////////////////// // Exception Handling ///////////////////// @@ -585,6 +660,33 @@ void doSomethingWithAFile(const std::string& filename) // vector (i.e. self-resizing array), hash maps, and so on // all automatically destroy their contents when they fall out of scope. // - Mutexes using lock_guard and unique_lock + + +///////////////////// +// Fun stuff +///////////////////// + +// Aspects of C++ that may be surprising to newcomers (and even some veterans): + +// You can override private methods! +class Foo { + virtual void bar(); +}; +class FooSub : public Foo { + virtual void bar(); // overrides Foo::bar! +}; + +// 0, false, NULL are all the same thing! +bool* pt = new bool; +*pt = 0; // Sets the value points by 'pt' to false. +pt = 0; // Sets 'pt' to the null pointer. Yes both lines compile without warning. + +// '=' != '=' +Foo f1 = f2; // Calls Foo::Foo(const Foo&) or some variant copy constructor. + +Foo f1; +f1 = f2; // Calls Foo::operator=(Foo&) or variant. + ``` Futher Reading: -- cgit v1.2.3 From 05c2617aaf6d040b1e4372d8f83df11288ff2279 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 4 May 2015 02:45:31 -0600 Subject: Minor fix --- c++.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 10c39c9c..e50c33fa 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -489,15 +489,16 @@ void barkThreeTimes(const T& input) Dog fluffy; fluffy.setName("Fluffy") -barkThreeTimes(fluffy); Prints "Fluffy barks" three times. +barkThreeTimes(fluffy); // Prints "Fluffy barks" three times. -// Template parameters don't have to be classes, though this is used very rarely: +// Template parameters don't have to be classes: template void printMessage() { cout << "Learn C++ in " << Y << " minutes!" << endl; } -// And you can explicitly specialize templates for more efficient code: +// And you can explicitly specialize templates for more efficient code (most +// real-world uses of specialization are not as trivial as this): template<> void printMessage<10>() { cout << "Learn C++ faster in only 10 minutes!" << endl; -- cgit v1.2.3 From 6c635d1c3e5bb88940fb0afb01218d2e7d3f11c0 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Tue, 5 May 2015 00:45:14 -0600 Subject: Response to comments --- c++.html.markdown | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index e50c33fa..517ce367 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -497,8 +497,10 @@ void printMessage() { cout << "Learn C++ in " << Y << " minutes!" << endl; } -// And you can explicitly specialize templates for more efficient code (most -// real-world uses of specialization are not as trivial as this): +// And you can explicitly specialize templates for more efficient code. Of +// course, most real-world uses of specialization are not as trivial as this. +// Note that you still need to declare the function (or class) as a template +// even if you explicitly specified all parameters. template<> void printMessage<10>() { cout << "Learn C++ faster in only 10 minutes!" << endl; @@ -667,7 +669,9 @@ void doSomethingWithAFile(const std::string& filename) // Fun stuff ///////////////////// -// Aspects of C++ that may be surprising to newcomers (and even some veterans): +// Aspects of C++ that may be surprising to newcomers (and even some veterans). +// This section is, unfortunately, wildly incomplete; C++ is one of the easiest +// languages with which to shoot yourself in the foot. // You can override private methods! class Foo { @@ -677,16 +681,35 @@ class FooSub : public Foo { virtual void bar(); // overrides Foo::bar! }; -// 0, false, NULL are all the same thing! + +// 0 == false == NULL (most of the time)! bool* pt = new bool; *pt = 0; // Sets the value points by 'pt' to false. -pt = 0; // Sets 'pt' to the null pointer. Yes both lines compile without warning. +pt = 0; // Sets 'pt' to the null pointer. Both lines compile without warnings. + +// nullptr is supposed to fix some of that issue: +int* pt2 = new int; +*pt2 = nullptr; // Doesn't compile +pt2 = nullptr; // Sets pt2 to null. + +// But somehow 'bool' type is an exception. +*pt = nullptr; // This still compiles, even though '*pt' is a bool! + + +// '=' != '=' != '='! +// Calls Foo::Foo(const Foo&) or some variant copy constructor. +Foo f2; +Foo f1 = f2; -// '=' != '=' -Foo f1 = f2; // Calls Foo::Foo(const Foo&) or some variant copy constructor. +// Calls Foo::Foo(const Foo&) or variant, but only copies the 'Foo' part of +// 'fooSub'. Any extra members of 'fooSub' are discarded. This lovely behavior +// is called "object slicing." +FooSub fooSub; +Foo f1 = fooSub; +// Calls Foo::operator=(Foo&) or variant. Foo f1; -f1 = f2; // Calls Foo::operator=(Foo&) or variant. +f1 = f2; ``` Futher Reading: -- cgit v1.2.3 From 25bd06d77a70964acfbdbf8a7c7a50eb312eae9f Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Wed, 6 May 2015 15:25:28 -0600 Subject: comment changes --- c++.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 517ce367..66d4aeb1 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -692,7 +692,7 @@ int* pt2 = new int; *pt2 = nullptr; // Doesn't compile pt2 = nullptr; // Sets pt2 to null. -// But somehow 'bool' type is an exception. +// But somehow 'bool' type is an exception (this is to make `if (ptr)` compile). *pt = nullptr; // This still compiles, even though '*pt' is a bool! @@ -702,8 +702,8 @@ Foo f2; Foo f1 = f2; // Calls Foo::Foo(const Foo&) or variant, but only copies the 'Foo' part of -// 'fooSub'. Any extra members of 'fooSub' are discarded. This lovely behavior -// is called "object slicing." +// 'fooSub'. Any extra members of 'fooSub' are discarded. This sometimes +// horrifying behavior is called "object slicing." FooSub fooSub; Foo f1 = fooSub; -- cgit v1.2.3 From bad283ee14dadd02f9b21d5fbcff226bfb5c954f Mon Sep 17 00:00:00 2001 From: Hans Ole Hatzel Date: Wed, 20 May 2015 19:38:11 +0200 Subject: [c++/en] Fixed typos. --- c++.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 66d4aeb1..9f8f5f32 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -425,7 +425,7 @@ int main () { Point up (0,1); Point right (1,0); // This calls the Point + operator - // Point up calls the + (function) with right as its paramater + // Point up calls the + (function) with right as its parameter Point result = up + right; // Prints "Result is upright (1,1)" cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; @@ -464,7 +464,7 @@ intBox.insert(123); Box > boxOfBox; boxOfBox.insert(intBox); -// Up until C++11, you muse place a space between the two '>'s, otherwise '>>' +// Up until C++11, you must place a space between the two '>'s, otherwise '>>' // will be parsed as the right shift operator. // You will sometimes see @@ -712,7 +712,7 @@ Foo f1; f1 = f2; ``` -Futher Reading: +Further Reading: An up-to-date language reference can be found at -- cgit v1.2.3 From 462ac892179d64437b1124263402378a6054e50b Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 31 May 2015 21:38:03 -0400 Subject: Remove return type from Dog class constructor and destructor, change nonexistant printDog function to print. --- c++.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 9f8f5f32..2c6d51a9 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -300,7 +300,7 @@ public: }; // A semicolon must follow the class definition. // Class member functions are usually implemented in .cpp files. -void Dog::Dog() +Dog::Dog() { std::cout << "A dog has been constructed\n"; } @@ -323,7 +323,7 @@ void Dog::print() const std::cout << "Dog is " << name << " and weighs " << weight << "kg\n"; } -void Dog::~Dog() +Dog::~Dog() { cout << "Goodbye " << name << "\n"; } @@ -332,7 +332,7 @@ int main() { Dog myDog; // prints "A dog has been constructed" myDog.setName("Barkley"); myDog.setWeight(10); - myDog.printDog(); // prints "Dog is Barkley and weighs 10 kg" + myDog.print(); // prints "Dog is Barkley and weighs 10 kg" return 0; } // prints "Goodbye Barkley" -- cgit v1.2.3 From 3db1042157204ad05484d6b42140261f849040cc Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 31 May 2015 21:42:03 -0400 Subject: Add missing semicolons. --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 2c6d51a9..5cd491b9 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -288,7 +288,7 @@ public: // Functions can also be defined inside the class body. // Functions defined as such are automatically inlined. - void bark() const { std::cout << name << " barks!\n" } + void bark() const { std::cout << name << " barks!\n"; } // Along with constructors, C++ provides destructors. // These are called when an object is deleted or falls out of scope. @@ -341,7 +341,7 @@ int main() { // This class inherits everything public and protected from the Dog class class OwnedDog : public Dog { - void setOwner(const std::string& dogsOwner) + void setOwner(const std::string& dogsOwner); // Override the behavior of the print function for all OwnedDogs. See // http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping -- cgit v1.2.3 From cea52ca43490b74316781c23779654fd46aaeab4 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 1 Jun 2015 21:57:18 -0400 Subject: Exceptions do not take a string argument in their constructor. --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 5cd491b9..b214de7a 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -523,7 +523,7 @@ printMessage<10>(); // Prints "Learn C++ faster in only 10 minutes!" // _catch_ handlers. try { // Do not allocate exceptions on the heap using _new_. - throw std::exception("A problem occurred"); + throw std::exception(); } // Catch exceptions by const reference if they are objects catch (const std::exception& ex) @@ -614,7 +614,7 @@ void doSomethingWithAFile(const char* filename) { FILE* fh = fopen(filename, "r"); // Open the file in read mode if (fh == nullptr) - throw std::exception("Could not open the file."); + throw std::exception(); try { doSomethingWithTheFile(fh); -- cgit v1.2.3 From 47d3cea47e8c5203efa857070a00dcfbff67b019 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 1 Jun 2015 22:00:52 -0400 Subject: Template example class should have public method so it can get called externally. --- c++.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index b214de7a..d049408a 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -445,6 +445,7 @@ int main () { // define a class or function that takes a type parameter: template class Box { +public: // In this class, T can be used as any other type. void insert(const T&) { ... } }; -- cgit v1.2.3 From 894792e1e17173823a5d50de24439427c69d63f4 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 2 Jun 2015 16:30:35 -0400 Subject: Using std::runtime_error instead of std::exception. --- c++.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index d049408a..6f4d2959 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -519,12 +519,13 @@ printMessage<10>(); // Prints "Learn C++ faster in only 10 minutes!" // (see http://en.cppreference.com/w/cpp/error/exception) // but any type can be thrown an as exception #include +#include // All exceptions thrown inside the _try_ block can be caught by subsequent // _catch_ handlers. try { // Do not allocate exceptions on the heap using _new_. - throw std::exception(); + throw std::runtime_error("A problem occurred"); } // Catch exceptions by const reference if they are objects catch (const std::exception& ex) -- cgit v1.2.3 From 06889be239622266d9c36c750f7ee755ccdae05d Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 2 Jun 2015 19:14:52 -0400 Subject: Updated other exception to also be runtime_error type instead. --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 6f4d2959..ff2a98fd 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -616,7 +616,7 @@ void doSomethingWithAFile(const char* filename) { FILE* fh = fopen(filename, "r"); // Open the file in read mode if (fh == nullptr) - throw std::exception(); + throw std::runtime_error("Could not open the file."); try { doSomethingWithTheFile(fh); -- cgit v1.2.3 From 97b97408eab97fbe322df4266cda9ab2ed21fceb Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Fri, 28 Aug 2015 11:48:38 -0600 Subject: Fix C++ namespace explanation --- c++.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index ff2a98fd..883d3482 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -158,11 +158,12 @@ void foo() int main() { - // Assume everything is from the namespace "Second" - // unless otherwise specified. + // Includes all symbols from `namesapce Second` into the current scope. Note + // that simply `foo()` no longer works, since it is now ambiguous whether + // we're calling the `foo` in `namespace Second` or the top level. using namespace Second; - foo(); // prints "This is Second::foo" + Second::foo(); // prints "This is Second::foo" First::Nested::foo(); // prints "This is First::Nested::foo" ::foo(); // prints "This is global foo" } -- cgit v1.2.3 From 1d1def16a5d7925bb8f7fba7dc49182e33359e85 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Sun, 30 Aug 2015 14:20:18 -0600 Subject: A little more about C++ references --- c++.html.markdown | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index ff2a98fd..efce0053 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -4,6 +4,7 @@ filename: learncpp.cpp contributors: - ["Steven Basart", "http://github.com/xksteven"] - ["Matt Kline", "https://github.com/mrkline"] + - ["Geoff Liu", "http://geoffliu.me"] lang: en --- @@ -248,6 +249,56 @@ const string& barRef = bar; // Create a const reference to bar. // Like C, const values (and pointers and references) cannot be modified. barRef += ". Hi!"; // Error, const references cannot be modified. +// Sidetrack: Before we talk more about references, we must introduce a concept +// called a temporary object. Suppose we have the following code: +string tempObjectFun() { ... } +string retVal = tempObjectFun(); + +// What happens in the second line is actually: +// - a string object is returned from `tempObjectFun` +// - a new string is constructed with the returned object as arugment to the +// constructor +// - the returned object is destroyed +// The returned object is called a temporary object. Temporary objects are +// created whenever a function returns an object, and they are destroyed at the +// end of the evaluation of the enclosing expression. So in this code: +foo(bar(tempObjectFun())) + +// assuming `foo` and `bar` exist, the object returned from `tempObjectFun` is +// passed to `bar`, and it is destroyed before `foo` is called. + +// Now back to references. The exception to the "at the end of the enclosing +// expression" rule is if a temporary object is bound to a const reference, in +// which case its life gets extended to the current scope: + +void constReferenceTempObjectFun() { + // `constRef` gets the temporary object, and it is valid until the end of this + // function. + const string& constRef = tempObjectFun(); + ... +} + +// Another kind of reference introduced in C++11 is specifically for temporary +// objects. You cannot have a variable of its type, but it takes precedence in +// overload resolution: + +void someFun(string& s) { ... } // Regular reference +void someFun(string&& s) { ... } // Reference to temporary object + +string foo; +someFun(foo); // Calls the version with regular reference +someFun(tempObjectFun()); // Calls the version with temporary reference + +// For example, you will see these two versions of constructors for +// std::basic_string: +basic_string(const basic_string& other); +basic_string(basic_string&& other); + +// Idea being if we are constructing a new string from a temporary object (which +// is going to be destroyed soon anyway), we can have a more efficient +// constructor that "salvages" parts of that temporary string. You will see this +// concept referred to as the move semantic. + ////////////////////////////////////////// // Classes and object-oriented programming ////////////////////////////////////////// -- cgit v1.2.3 From a230d76307ecbc0f53c4b359cdb90628720f915e Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Sun, 30 Aug 2015 14:41:02 -0600 Subject: More about temporary objects --- c++.html.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index e45c73c3..74bd8913 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -765,6 +765,22 @@ Foo f1 = fooSub; Foo f1; f1 = f2; + +// How to truly clear a container: +class Foo { ... }; +vector v; +for (int i = 0; i < 10; ++i) + v.push_back(Foo()); + +// Following line sets size of v to 0, but destructors don't get called, +// and resources aren't released! +v.empty(); +v.push_back(Foo()); // New value is copied into the first Foo we inserted in the loop. + +// Truly destroys all values in v. See section about temporary object for +// explanation of why this works. +v.swap(vector()); + ``` Further Reading: -- cgit v1.2.3 From fc9ae44e4887500634bf3a87343d687b4d7d4e3c Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Sun, 30 Aug 2015 14:46:46 -0600 Subject: Now that we explained move semantics --- c++.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 74bd8913..fa80e6d5 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -751,7 +751,8 @@ pt2 = nullptr; // Sets pt2 to null. // '=' != '=' != '='! -// Calls Foo::Foo(const Foo&) or some variant copy constructor. +// Calls Foo::Foo(const Foo&) or some variant (see move semantics) copy +// constructor. Foo f2; Foo f1 = f2; -- cgit v1.2.3 From 85f6ba0b57b9d894c694df66449b1e1c555c625b Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Wed, 2 Sep 2015 00:46:30 -0600 Subject: A note about RVO --- c++.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index fa80e6d5..26dfe111 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -262,7 +262,10 @@ string retVal = tempObjectFun(); // - the returned object is destroyed // The returned object is called a temporary object. Temporary objects are // created whenever a function returns an object, and they are destroyed at the -// end of the evaluation of the enclosing expression. So in this code: +// end of the evaluation of the enclosing expression (Well, this is what the +// standard says, but compilers are allowed to change this behavior. Look up +// "return value optimization" if you're into this kind of details). So in this +// code: foo(bar(tempObjectFun())) // assuming `foo` and `bar` exist, the object returned from `tempObjectFun` is -- cgit v1.2.3 From 8eb410208a8d9b0a42f6c52411455ace04c78101 Mon Sep 17 00:00:00 2001 From: George Gognadze Date: Thu, 1 Oct 2015 18:55:28 +0400 Subject: Update c++.html.markdown o should be capitalized. this: overrides should be: Overrides --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 26dfe111..8a7f5a59 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -735,7 +735,7 @@ class Foo { virtual void bar(); }; class FooSub : public Foo { - virtual void bar(); // overrides Foo::bar! + virtual void bar(); // Overrides Foo::bar! }; -- cgit v1.2.3 From ae86e4ebabb0c78c1bd8052e6ab5916446ef39c2 Mon Sep 17 00:00:00 2001 From: Alva Connor Waters Date: Fri, 2 Oct 2015 15:19:38 +0000 Subject: Clarify character literals --- c++.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 8a7f5a59..1cf5508a 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -53,11 +53,11 @@ int main(int argc, char** argv) // However, C++ varies in some of the following ways: -// In C++, character literals are one byte. -sizeof('c') == 1 +// In C++, character literals are chars +sizeof('c') == sizeof(char) == 1 -// In C, character literals are the same size as ints. -sizeof('c') == sizeof(10) +// In C, character literals are ints +sizeof('c') == sizeof(int) // C++ has strict prototyping -- cgit v1.2.3 From 455afa3a7bf59fc272f3439825da55659765eec0 Mon Sep 17 00:00:00 2001 From: Alva Connor Waters Date: Fri, 2 Oct 2015 15:55:05 +0000 Subject: More explanation on virtual destructors --- c++.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 1cf5508a..b59635f5 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -349,7 +349,10 @@ public: // These are called when an object is deleted or falls out of scope. // This enables powerful paradigms such as RAII // (see below) - // Destructors must be virtual to allow classes to be derived from this one. + // Destructors should be virtual if a class is to be derived from; + // if they are not virtual, then any resources allocated using RAII in + // the derived class will not be released if it destroyed through a + // base-class reference or pointer. virtual ~Dog(); }; // A semicolon must follow the class definition. -- cgit v1.2.3 From 12286a4b78f82bde3907d4bf348e20c12dd6d46f Mon Sep 17 00:00:00 2001 From: Alva Connor Waters Date: Fri, 2 Oct 2015 16:00:13 +0000 Subject: Misc. typos and formatting --- c++.html.markdown | 72 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 33 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index b59635f5..e5eceac1 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -159,9 +159,9 @@ void foo() int main() { - // Includes all symbols from `namesapce Second` into the current scope. Note - // that simply `foo()` no longer works, since it is now ambiguous whether - // we're calling the `foo` in `namespace Second` or the top level. + // Includes all symbols from namespace Second into the current scope. Note + // that simply foo() no longer works, since it is now ambiguous whether + // we're calling the foo in namespace Second or the top level. using namespace Second; Second::foo(); // prints "This is Second::foo" @@ -256,7 +256,7 @@ string tempObjectFun() { ... } string retVal = tempObjectFun(); // What happens in the second line is actually: -// - a string object is returned from `tempObjectFun` +// - a string object is returned from tempObjectFun // - a new string is constructed with the returned object as arugment to the // constructor // - the returned object is destroyed @@ -268,15 +268,15 @@ string retVal = tempObjectFun(); // code: foo(bar(tempObjectFun())) -// assuming `foo` and `bar` exist, the object returned from `tempObjectFun` is -// passed to `bar`, and it is destroyed before `foo` is called. +// assuming foo and bar exist, the object returned from tempObjectFun is +// passed to bar, and it is destroyed before foo is called. // Now back to references. The exception to the "at the end of the enclosing // expression" rule is if a temporary object is bound to a const reference, in // which case its life gets extended to the current scope: void constReferenceTempObjectFun() { - // `constRef` gets the temporary object, and it is valid until the end of this + // constRef gets the temporary object, and it is valid until the end of this // function. const string& constRef = tempObjectFun(); ... @@ -301,7 +301,7 @@ basic_string(basic_string&& other); // Idea being if we are constructing a new string from a temporary object (which // is going to be destroyed soon anyway), we can have a more efficient // constructor that "salvages" parts of that temporary string. You will see this -// concept referred to as the move semantic. +// concept referred to as "move semantics". ////////////////////////////////////////// // Classes and object-oriented programming @@ -349,10 +349,10 @@ public: // These are called when an object is deleted or falls out of scope. // This enables powerful paradigms such as RAII // (see below) - // Destructors should be virtual if a class is to be derived from; - // if they are not virtual, then any resources allocated using RAII in - // the derived class will not be released if it destroyed through a - // base-class reference or pointer. + // The destructor should be virtual if a class is to be derived from; + // if it is not virtual, then the derived class' destructor will + // not be called if the object is destroyed through a base-class reference + // or pointer. virtual ~Dog(); }; // A semicolon must follow the class definition. @@ -495,9 +495,10 @@ int main () { ///////////////////// // Templates in C++ are mostly used for generic programming, though they are -// much more powerful than generics constructs in other languages. It also -// supports explicit and partial specialization, functional-style type classes, -// and also it's Turing-complete. +// much more powerful than generic constructs in other languages. They also +// support explicit and partial specialization and functional-style type +// classes; in fact, they are a Turing-complete functional language embedded +// in C++! // We start with the kind of generic programming you might be familiar with. To // define a class or function that takes a type parameter: @@ -509,7 +510,7 @@ public: }; // During compilation, the compiler actually generates copies of each template -// with parameters substituted, and so the full definition of the class must be +// with parameters substituted, so the full definition of the class must be // present at each invocation. This is why you will see template classes defined // entirely in header files. @@ -523,13 +524,13 @@ intBox.insert(123); Box > boxOfBox; boxOfBox.insert(intBox); -// Up until C++11, you must place a space between the two '>'s, otherwise '>>' -// will be parsed as the right shift operator. +// Until C++11, you had to place a space between the two '>'s, otherwise '>>' +// would be parsed as the right shift operator. // You will sometimes see // template -// instead. The 'class' keyword and 'typename' keyword are _mostly_ -// interchangeable in this case. For full explanation, see +// instead. The 'class' keyword and 'typename' keywords are _mostly_ +// interchangeable in this case. For the full explanation, see // http://en.wikipedia.org/wiki/Typename // (yes, that keyword has its own Wikipedia page). @@ -585,12 +586,15 @@ try { // Do not allocate exceptions on the heap using _new_. throw std::runtime_error("A problem occurred"); } + // Catch exceptions by const reference if they are objects catch (const std::exception& ex) { - std::cout << ex.what(); + std::cout << ex.what(); +} + // Catches any exception not caught by previous _catch_ blocks -} catch (...) +catch (...) { std::cout << "Unknown exception caught"; throw; // Re-throws the exception @@ -600,8 +604,8 @@ catch (const std::exception& ex) // RAII /////// -// RAII stands for Resource Allocation Is Initialization. -// It is often considered the most powerful paradigm in C++, +// RAII stands for "Resource Acquisition Is Initialization". +// It is often considered the most powerful paradigm in C++ // and is the simple concept that a constructor for an object // acquires that object's resources and the destructor releases them. @@ -622,9 +626,9 @@ void doSomethingWithAFile(const char* filename) // Unfortunately, things are quickly complicated by error handling. // Suppose fopen can fail, and that doSomethingWithTheFile and // doSomethingElseWithIt return error codes if they fail. -// (Exceptions are the preferred way of handling failure, -// but some programmers, especially those with a C background, -// disagree on the utility of exceptions). +// (Exceptions are the preferred way of handling failure, +// but some programmers, especially those with a C background, +// disagree on the utility of exceptions). // We now have to check each call for failure and close the file handle // if a problem occurred. bool doSomethingWithAFile(const char* filename) @@ -744,15 +748,17 @@ class FooSub : public Foo { // 0 == false == NULL (most of the time)! bool* pt = new bool; -*pt = 0; // Sets the value points by 'pt' to false. +*pt = 0; // Sets the value points by 'pt' to false. pt = 0; // Sets 'pt' to the null pointer. Both lines compile without warnings. // nullptr is supposed to fix some of that issue: int* pt2 = new int; -*pt2 = nullptr; // Doesn't compile +*pt2 = nullptr; // Doesn't compile pt2 = nullptr; // Sets pt2 to null. -// But somehow 'bool' type is an exception (this is to make `if (ptr)` compile). +// There is an exception made for bools. +// This is to allow you to test for null pointers with if(!ptr), +// but as a consequence you can assign nullptr to a bool directly! *pt = nullptr; // This still compiles, even though '*pt' is a bool! @@ -779,12 +785,12 @@ vector v; for (int i = 0; i < 10; ++i) v.push_back(Foo()); -// Following line sets size of v to 0, but destructors don't get called, +// Following line sets size of v to 0, but destructors don't get called // and resources aren't released! v.empty(); -v.push_back(Foo()); // New value is copied into the first Foo we inserted in the loop. +v.push_back(Foo()); // New value is copied into the first Foo we inserted -// Truly destroys all values in v. See section about temporary object for +// Truly destroys all values in v. See section about temporary objects for // explanation of why this works. v.swap(vector()); -- cgit v1.2.3 From c4b8281ceeed59ddfa003cc95e40944735d1c910 Mon Sep 17 00:00:00 2001 From: Alva Connor Waters Date: Fri, 2 Oct 2015 16:19:29 +0000 Subject: Add to contributors --- c++.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index e5eceac1..4acc1b9d 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -5,6 +5,7 @@ contributors: - ["Steven Basart", "http://github.com/xksteven"] - ["Matt Kline", "https://github.com/mrkline"] - ["Geoff Liu", "http://geoffliu.me"] + - ["Connor Waters", "http://github.com/connorwaters"] lang: en --- -- cgit v1.2.3 From 9bc553c46ce9b7154ec7c82451d71608f4beda82 Mon Sep 17 00:00:00 2001 From: Pushkar Sharma Date: Sun, 4 Oct 2015 10:12:55 +0530 Subject: Update c++.html.markdown Regarding issue #1216, Better explaining the Reference 'fooRef'. --- c++.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 4acc1b9d..bbd2f9a9 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -245,7 +245,13 @@ cout << fooRef; // Prints "I am foo. Hi!" // Doesn't reassign "fooRef". This is the same as "foo = bar", and // foo == "I am bar" // after this line. +cout << &fooRef << endl; //Prints address of fooRef fooRef = bar; +cout << &fooRef << endl; //Prints address of fooRef, AGAIN +cout << fooRef; // Prints "I am bar" + +//The address of fooRef remains the same, i.e. it is still referring to foo. + const string& barRef = bar; // Create a const reference to bar. // Like C, const values (and pointers and references) cannot be modified. -- cgit v1.2.3 From 87e8e77e5fd8d84a252dbb6d6697202118378774 Mon Sep 17 00:00:00 2001 From: Pushkar Sharma Date: Mon, 5 Oct 2015 00:13:54 +0530 Subject: Fixed a mistake from previous commit. Better explained reference address. --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index bbd2f9a9..bd86e9e5 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -245,9 +245,9 @@ cout << fooRef; // Prints "I am foo. Hi!" // Doesn't reassign "fooRef". This is the same as "foo = bar", and // foo == "I am bar" // after this line. -cout << &fooRef << endl; //Prints address of fooRef +cout << &fooRef << endl; //Prints address of foo fooRef = bar; -cout << &fooRef << endl; //Prints address of fooRef, AGAIN +cout << &fooRef << endl; //Still prints address of foo cout << fooRef; // Prints "I am bar" //The address of fooRef remains the same, i.e. it is still referring to foo. -- cgit v1.2.3 From 3b246fd869564b0a7f7c847f44aecac82d318c78 Mon Sep 17 00:00:00 2001 From: Pushkar Sharma Date: Mon, 5 Oct 2015 00:32:34 +0530 Subject: Grammar the address --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index bd86e9e5..8ee964ca 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -245,9 +245,9 @@ cout << fooRef; // Prints "I am foo. Hi!" // Doesn't reassign "fooRef". This is the same as "foo = bar", and // foo == "I am bar" // after this line. -cout << &fooRef << endl; //Prints address of foo +cout << &fooRef << endl; //Prints the address of foo fooRef = bar; -cout << &fooRef << endl; //Still prints address of foo +cout << &fooRef << endl; //Still prints the address of foo cout << fooRef; // Prints "I am bar" //The address of fooRef remains the same, i.e. it is still referring to foo. -- cgit v1.2.3 From 9d64b532f8ccdfd95c2417dcf65257385956353a Mon Sep 17 00:00:00 2001 From: George Gognadze Date: Wed, 7 Oct 2015 01:27:12 +0400 Subject: Update c++.html.markdown spelling error --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 8ee964ca..6f4d0562 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -264,7 +264,7 @@ string retVal = tempObjectFun(); // What happens in the second line is actually: // - a string object is returned from tempObjectFun -// - a new string is constructed with the returned object as arugment to the +// - a new string is constructed with the returned object as argument to the // constructor // - the returned object is destroyed // The returned object is called a temporary object. Temporary objects are -- cgit v1.2.3 From e32eb715ef41e411da0a91b40e6e35f150a9c2eb Mon Sep 17 00:00:00 2001 From: Cameron Wood Date: Sun, 11 Oct 2015 03:41:20 -0400 Subject: [C++/en] Small inheritance clarification Clarified that private members are inherited but are not directly accessible --- c++.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 6f4d0562..2bee51dc 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -404,6 +404,8 @@ int main() { // Inheritance: // This class inherits everything public and protected from the Dog class +// as well as private but may not directly access private members/methods +// without a public or protected method for doing so class OwnedDog : public Dog { void setOwner(const std::string& dogsOwner); -- cgit v1.2.3 From ca435fbb0dd09cdc9c70fe945a891ae3e6c19ab2 Mon Sep 17 00:00:00 2001 From: Cameron Wood Date: Sat, 17 Oct 2015 06:05:54 -0400 Subject: [C++/en] Added in Enum information Added in some information about enums in C++ including some different use cases --- c++.html.markdown | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 2bee51dc..d03092e5 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -310,6 +310,70 @@ basic_string(basic_string&& other); // constructor that "salvages" parts of that temporary string. You will see this // concept referred to as "move semantics". +///////////////////// +// Enums +///////////////////// + +// Enums are a way to assign a value to a constant most commonly used for +// easier visualization and reading of code +enum ECarTypes +{ + Sedan, + Hatchback, + SUV, + Wagon +}; + +ECarTypes GetPreferredCarType() +{ + return ECarTypes::Hatchback; +} + +// As of C++11 there is an easy way to assign a type to the enum which can be +// useful in serialization of data and converting enums back-and-forth between +// the desired type and their respective constants +enum ECarTypes : uint8_t +{ + Sedan, // 0 + Hatchback, // 1 + SUV = 254, // 254 + Hybrid // 255 +}; + +void WriteByteToFile(uint8_t InputValue) +{ + // Serialize the InputValue to a file +} + +void WritePreferredCarTypeToFile(ECarTypes InputCarType) +{ + // The enum is implicitly converted to a uint8_t due to its declared enum type + WriteByteToFile(InputCarType); +} + +// On the other hand you may not want enums to be accidentally cast to an integer +// type or to other enums so it is instead possible to create an enum class which +// won't be implicitly converted +enum class ECarTypes : uint8_t +{ + Sedan, // 0 + Hatchback, // 1 + SUV = 254, // 254 + Hybrid // 255 +}; + +void WriteByteToFile(uint8_t InputValue) +{ + // Serialize the InputValue to a file +} + +void WritePreferredCarTypeToFile(ECarTypes InputCarType) +{ + // Won't compile even though ECarTypes is a uint8_t due to the enum + // being declared as an "enum class"! + WriteByteToFile(InputCarType); +} + ////////////////////////////////////////// // Classes and object-oriented programming ////////////////////////////////////////// -- cgit v1.2.3 From c3e769e4ac50d4475a530969663e073f4ff002ca Mon Sep 17 00:00:00 2001 From: Brook Zhou Date: Sat, 31 Oct 2015 16:17:58 -0700 Subject: [cpp/en] comparator function for std containers --- c++.html.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index d03092e5..6b452b1b 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -801,6 +801,24 @@ void doSomethingWithAFile(const std::string& filename) // all automatically destroy their contents when they fall out of scope. // - Mutexes using lock_guard and unique_lock +// containers with object keys of non-primitive values (custom classes) require +// compare function in the object itself or as a function pointer. Primitives +// have default comparators, but you can override it. +class Foo { +public: + int j; + Foo(int a) : j(a) {} +}; +struct compareFunction { + bool operator()(const Foo& a, const Foo& b) const { + return a.j < b.j; + } +}; +//this isn't allowed (although it can vary depending on compiler) +//std::map fooMap; +std::map fooMap; +fooMap[Foo(1)] = 1; +fooMap.find(Foo(1)); //true ///////////////////// // Fun stuff -- cgit v1.2.3 From fd26c8ddfb6d4bfa969b323a2e98ce1b74bc8127 Mon Sep 17 00:00:00 2001 From: John Rocamora Date: Sat, 12 Dec 2015 17:51:23 -0500 Subject: Added missing semicolon --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 6b452b1b..f4aa2f5a 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -149,7 +149,7 @@ namespace First { namespace Second { void foo() { - printf("This is Second::foo\n") + printf("This is Second::foo\n"); } } -- cgit v1.2.3 From 19ead59c1fde3623bc29e1fe56f33f2587c97d3a Mon Sep 17 00:00:00 2001 From: bk2dcradle Date: Tue, 5 Jan 2016 13:36:23 +0530 Subject: Add my Changes --- c++.html.markdown | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index f4aa2f5a..31dbe064 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -820,6 +820,73 @@ std::map fooMap; fooMap[Foo(1)] = 1; fooMap.find(Foo(1)); //true +/////////////////////////////////////// +// Lambda Expressions (C++11 and above) +/////////////////////////////////////// + +// lambdas are a convenient way of defining an anonymous function +// object right at the location where it is invoked or passed as +// an argument to a function. + +// For example, consider sorting a vector of pairs using the second +// value of the pair + +vector > tester; +tester.push_back(make_pair(3, 6)); +tester.push_back(make_pair(1, 9)); +tester.push_back(make_pair(5, 0)); + +// Pass a lambda expression as third argument to the sort function +// sort is from the header + +sort(tester.begin(), tester.end(), [](const pair& lhs, const pair& rhs) { + return lhs.second < rhs.second; + }); + +// Notice the syntax of the lambda expression, +// [] in the lambda is used to "capture" variables. +// For Example: + +vector dog_ids; +// number_of_dogs = 3; +for(int i = 0; i < 3; i++){ + dog_ids.push_back(i); +} + +int weight[3] = {30, 50, 10}; + +// Say you want to sort dog_ids according to the dogs' weights +// So dog_ids should in the end become: [2, 0, 1] + +// Here's where lambda expressions come in handy + +sort(dog_ids.begin(), dog_ids.end(), [&weight](const int &lhs, const int &rhs) { + return weight[lhs] < weight[rhs]; + }); +// Note we captured "weight" by reference in the above example. + +// lambda are really useful for the case of structs +// You can use lambda expressions instead of overloading +// the "<" operator + +/////////////////////////////// +// Range For (C++11 and above) +/////////////////////////////// + +// You can use a range for loop to iterate over a container +int arr[] = {1, 10, 3}; + +for(int elem: arr){ + cout << elem << endl; +} + +// You can use "auto" and not worry about the type of the elements of the container +// For example: + +for(auto elem: arr) { + // Do something with each element of arr +} + ///////////////////// // Fun stuff ///////////////////// -- cgit v1.2.3 From e6866f5a26dab28d2d1b5628fbb18139c36a5139 Mon Sep 17 00:00:00 2001 From: bk2dcradle Date: Fri, 8 Jan 2016 01:21:38 +0530 Subject: More Fixes --- c++.html.markdown | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 31dbe064..44cad665 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -840,16 +840,22 @@ tester.push_back(make_pair(5, 0)); // sort is from the header sort(tester.begin(), tester.end(), [](const pair& lhs, const pair& rhs) { - return lhs.second < rhs.second; - }); + return lhs.second < rhs.second; + }); // Notice the syntax of the lambda expression, -// [] in the lambda is used to "capture" variables. -// For Example: +// [] in the lambda is used to "capture" variables +// The "Capture List" defines what from the outside of the lambda should be available inside the function body and how. +// It can be either: +// 1. a value : [x] +// 2. a reference : [&x] +// 3. any variable currently in scope by reference [&] +// 4. same as 3, but by value [=] +// Example: vector dog_ids; // number_of_dogs = 3; -for(int i = 0; i < 3; i++){ +for(int i = 0; i < 3; i++) { dog_ids.push_back(i); } @@ -861,13 +867,10 @@ int weight[3] = {30, 50, 10}; // Here's where lambda expressions come in handy sort(dog_ids.begin(), dog_ids.end(), [&weight](const int &lhs, const int &rhs) { - return weight[lhs] < weight[rhs]; - }); + return weight[lhs] < weight[rhs]; + }); // Note we captured "weight" by reference in the above example. - -// lambda are really useful for the case of structs -// You can use lambda expressions instead of overloading -// the "<" operator +// More on Lambdas in C++ : http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 /////////////////////////////// // Range For (C++11 and above) -- cgit v1.2.3 From 32f18cd992b5b6988a3b37eaa533f8215d83fe2e Mon Sep 17 00:00:00 2001 From: Jaskamal Kainth Date: Thu, 28 Jan 2016 10:38:51 +0530 Subject: Added Tuple --- c++.html.markdown | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 44cad665..b8ab656c 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -955,6 +955,54 @@ v.push_back(Foo()); // New value is copied into the first Foo we inserted // explanation of why this works. v.swap(vector()); + +/////////////////////////////////////// +// Tuples (C++11 and above) +/////////////////////////////////////// + +#include + +// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members , +// its elements are accessed by their order in the tuple. + +// We start with constructing a tuple. +// +// Packing values into tuple +auto first = make_tuple ( 10 , 'A' ) ; +const int maxN = 1e9; +int maxL = 15; +auto second = make_tuple ( maxN , maxL ) ; + +// printing elements of 'first' tuple +cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A + +// printing elements of 'second' tuple +cout << get<0>(second)<< " " << get<1>(second) << "\n"; // prints: 1000000000 15 + + +// Unpacking tuple into variables + +int first_int; +char first_char; +tie (first_int , first_char ) = first; +cout << first_int << " " << first_char << "\n"; // prints : 10 A + +// We can also create tuple like this. + +tuple third ( 11 ,'A' , 3.14141); +// tuple_size returns number of elements in a tuple (as a constexpr) + +cout << tuple_size< decltype(third)>::value << "\n"; // prints: 3 + +// tuple_cat concatenates the elements of all the tuples in the same order. + +auto concatenated_tuple = tuple_cat( first, second ,third); +// concatenated_tuple becomes = (10 , 'A' , 1e9 , 15 , 11 , 'A' , 3.14141 ) + +cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 +cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 +cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' + ``` Further Reading: -- cgit v1.2.3 From c805148618f5b2679d6581ff41885abc7140fd4d Mon Sep 17 00:00:00 2001 From: Jaskamal Kainth Date: Thu, 28 Jan 2016 15:26:35 +0530 Subject: [C++/en] Tuples in C++ --- c++.html.markdown | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index b8ab656c..594cf15f 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -966,25 +966,24 @@ v.swap(vector()); // its elements are accessed by their order in the tuple. // We start with constructing a tuple. -// // Packing values into tuple -auto first = make_tuple ( 10 , 'A' ) ; +auto first = make_tuple( 10 , 'A' ) ; const int maxN = 1e9; -int maxL = 15; -auto second = make_tuple ( maxN , maxL ) ; +const int maxL = 15; +auto second = make_tuple( maxN , maxL ) ; // printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A // printing elements of 'second' tuple -cout << get<0>(second)<< " " << get<1>(second) << "\n"; // prints: 1000000000 15 +cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15 // Unpacking tuple into variables int first_int; char first_char; -tie (first_int , first_char ) = first; +tie(first_int , first_char ) = first; cout << first_int << " " << first_char << "\n"; // prints : 10 A // We can also create tuple like this. @@ -992,7 +991,7 @@ cout << first_int << " " << first_char << "\n"; // prints : 10 A tuple third ( 11 ,'A' , 3.14141); // tuple_size returns number of elements in a tuple (as a constexpr) -cout << tuple_size< decltype(third)>::value << "\n"; // prints: 3 +cout << tuple_size < decltype(third) >::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. -- cgit v1.2.3 From 4a1a6857ce30f19f8c04dcca4571bb27f7dc36d0 Mon Sep 17 00:00:00 2001 From: Jaskamal Kainth Date: Thu, 28 Jan 2016 17:15:44 +0530 Subject: [C++/en] Tuple , updated --- c++.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 594cf15f..ea6ef034 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -967,10 +967,10 @@ v.swap(vector()); // We start with constructing a tuple. // Packing values into tuple -auto first = make_tuple( 10 , 'A' ) ; +auto first = make_tuple(10,'A') ; const int maxN = 1e9; const int maxL = 15; -auto second = make_tuple( maxN , maxL ) ; +auto second = make_tuple(maxN,maxL) ; // printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A @@ -983,12 +983,12 @@ cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 1 int first_int; char first_char; -tie(first_int , first_char ) = first; +tie(first_int,first_char) = first; cout << first_int << " " << first_char << "\n"; // prints : 10 A // We can also create tuple like this. -tuple third ( 11 ,'A' , 3.14141); +tuple third (11,'A',3.14141); // tuple_size returns number of elements in a tuple (as a constexpr) cout << tuple_size < decltype(third) >::value << "\n"; // prints: 3 @@ -996,7 +996,7 @@ cout << tuple_size < decltype(third) >::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. auto concatenated_tuple = tuple_cat( first, second ,third); -// concatenated_tuple becomes = (10 , 'A' , 1e9 , 15 , 11 , 'A' , 3.14141 ) +// concatenated_tuple becomes = (10,'A',1e9,15,11,'A',3.14141) cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 -- cgit v1.2.3 From fa2b171008061bc82cf9b35e0470eebeaecb4a26 Mon Sep 17 00:00:00 2001 From: Jaskamal Kainth Date: Thu, 28 Jan 2016 18:22:11 +0530 Subject: [C++/en] Tuple , Updated --- c++.html.markdown | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index ea6ef034..1065b9e8 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -967,10 +967,10 @@ v.swap(vector()); // We start with constructing a tuple. // Packing values into tuple -auto first = make_tuple(10,'A') ; +auto first = make_tuple(10,'A'); const int maxN = 1e9; const int maxL = 15; -auto second = make_tuple(maxN,maxL) ; +auto second = make_tuple(maxN,maxL); // printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A @@ -978,7 +978,6 @@ cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A // printing elements of 'second' tuple cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15 - // Unpacking tuple into variables int first_int; -- cgit v1.2.3 From e1016455d5e4472e7a533c8cdd6df8ae4f2e7854 Mon Sep 17 00:00:00 2001 From: ven Date: Thu, 28 Jan 2016 14:04:41 +0100 Subject: #2119 fixups --- c++.html.markdown | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 1065b9e8..82662b15 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -967,10 +967,10 @@ v.swap(vector()); // We start with constructing a tuple. // Packing values into tuple -auto first = make_tuple(10,'A'); +auto first = make_tuple(10, 'A'); const int maxN = 1e9; const int maxL = 15; -auto second = make_tuple(maxN,maxL); +auto second = make_tuple(maxN, maxL); // printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A @@ -982,20 +982,20 @@ cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 1 int first_int; char first_char; -tie(first_int,first_char) = first; +tie(first_int, first_char) = first; cout << first_int << " " << first_char << "\n"; // prints : 10 A // We can also create tuple like this. -tuple third (11,'A',3.14141); +tuple third(11, 'A', 3.14141); // tuple_size returns number of elements in a tuple (as a constexpr) -cout << tuple_size < decltype(third) >::value << "\n"; // prints: 3 +cout << tuple_size::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. -auto concatenated_tuple = tuple_cat( first, second ,third); -// concatenated_tuple becomes = (10,'A',1e9,15,11,'A',3.14141) +auto concatenated_tuple = tuple_cat(first, second, third); +// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 -- cgit v1.2.3 From 1bb2535efda67de687d4eb94a0f964f9c06dcd4a Mon Sep 17 00:00:00 2001 From: AndrejPetelin Date: Sun, 31 Jan 2016 16:08:28 +0100 Subject: Update c++.html.markdown v.empty() queries if v is empty while v.clear() actually clears it (or rather sets size to 0). --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 82662b15..a59b4db8 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -948,7 +948,7 @@ for (int i = 0; i < 10; ++i) // Following line sets size of v to 0, but destructors don't get called // and resources aren't released! -v.empty(); +v.clear(); v.push_back(Foo()); // New value is copied into the first Foo we inserted // Truly destroys all values in v. See section about temporary objects for -- cgit v1.2.3 From 1d562740f3d3b68fbb51a45f66ae6b60eee7b2de Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 15 Feb 2016 14:33:23 -0500 Subject: Remove a section from c++, fixes #2130 --- c++.html.markdown | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index a59b4db8..a02e7e5b 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -330,7 +330,7 @@ ECarTypes GetPreferredCarType() } // As of C++11 there is an easy way to assign a type to the enum which can be -// useful in serialization of data and converting enums back-and-forth between +// useful in serialization of data and converting enums back-and-forth between // the desired type and their respective constants enum ECarTypes : uint8_t { @@ -352,7 +352,7 @@ void WritePreferredCarTypeToFile(ECarTypes InputCarType) } // On the other hand you may not want enums to be accidentally cast to an integer -// type or to other enums so it is instead possible to create an enum class which +// type or to other enums so it is instead possible to create an enum class which // won't be implicitly converted enum class ECarTypes : uint8_t { @@ -468,7 +468,7 @@ int main() { // Inheritance: // This class inherits everything public and protected from the Dog class -// as well as private but may not directly access private members/methods +// as well as private but may not directly access private members/methods // without a public or protected method for doing so class OwnedDog : public Dog { @@ -825,10 +825,10 @@ fooMap.find(Foo(1)); //true /////////////////////////////////////// // lambdas are a convenient way of defining an anonymous function -// object right at the location where it is invoked or passed as +// object right at the location where it is invoked or passed as // an argument to a function. -// For example, consider sorting a vector of pairs using the second +// For example, consider sorting a vector of pairs using the second // value of the pair vector > tester; @@ -856,7 +856,7 @@ sort(tester.begin(), tester.end(), [](const pair& lhs, const pair dog_ids; // number_of_dogs = 3; for(int i = 0; i < 3; i++) { - dog_ids.push_back(i); + dog_ids.push_back(i); } int weight[3] = {30, 50, 10}; @@ -940,29 +940,13 @@ Foo f1; f1 = f2; -// How to truly clear a container: -class Foo { ... }; -vector v; -for (int i = 0; i < 10; ++i) - v.push_back(Foo()); - -// Following line sets size of v to 0, but destructors don't get called -// and resources aren't released! -v.clear(); -v.push_back(Foo()); // New value is copied into the first Foo we inserted - -// Truly destroys all values in v. See section about temporary objects for -// explanation of why this works. -v.swap(vector()); - - /////////////////////////////////////// // Tuples (C++11 and above) /////////////////////////////////////// #include -// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members , +// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members , // its elements are accessed by their order in the tuple. // We start with constructing a tuple. @@ -995,7 +979,7 @@ cout << tuple_size::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. auto concatenated_tuple = tuple_cat(first, second, third); -// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) +// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 -- cgit v1.2.3 From 281ba5b37a9e4e42d33442b0d3a062d99c964a95 Mon Sep 17 00:00:00 2001 From: Ankush goyal Date: Mon, 27 Jun 2016 02:19:51 +0530 Subject: Containers Added (#1942) * Containers Added * Text Formatting Added required spaces between text and // --- c++.html.markdown | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index a02e7e5b..6b81f95f 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Matt Kline", "https://github.com/mrkline"] - ["Geoff Liu", "http://geoffliu.me"] - ["Connor Waters", "http://github.com/connorwaters"] + - ["Ankush Goyal", "http://github.com/ankushg07"] lang: en --- @@ -985,6 +986,99 @@ cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' + +///////////////////// +// CONTAINERS +///////////////////// + +// Containers or the Standard Template Library are some predefined templates +// They manages the storage space for its elements and provide +// member functions to access and manipulate them + +// Few containers are as follows:- + +// Vectors (Dynamic arrays) +// Allow us to Define the Array or list of objects at run time +#include // will include the header file for vector +vector< Data_Type > Vector_name; // used to initialize the vector +cin>>val; +Vector_name.push_back(val); // will push the value of variable into array + +// To iterate through vector, we have 2 choices +// using normal looping +for(int i=0; i::iterator it; // initialize the iteartor for vector +for(it=vector_name.begin(); it!=vector_name.end();++it) + +// For accessing the element of the vector +// Operator [] +var= vector_name[index]; //will assign value at that index to var + + +// Set +// Sets are containers that store unique elements following a specific order +// Very useful container to store unique values in sorted order +// without any other functions or code + +#include // Will include the header file for sets +set< int > ST; // Will initialize the set of int data type +ST.insert(30); // Will insert the value 30 in set ST +ST.insert(10); // Will insert the value 10 in set ST +ST.insert(20); // Will insert the value 20 in set ST +ST.insert(30); // Will insert the value 30 in set ST +// Now elements of sets are as follows +// 10 20 30 + +// To erase an element +ST.erase(20); // Will erase element with value 20 +// Set ST: 10 30 +// To iterate through Set we use iterators +set< int >::iterator it; +for(it=ST.begin();it // Will include the header file for map +map< char, int >mymap; // Will initalize the map with key as char and value as int + +mymap.insert ( pair('A',1) ); +// Will insert value 1 for key A +mymap.insert ( pair('Z',26) ); +// Will insert value 26 for key Z + +// To iterate +map::iterator it; +for (it=mymap.begin(); it!=mymap.end(); ++it) + std::cout << it->first << "->" << it->second <<'\n'; +// Output: +// A->1 +// Z->26 + +// To find the value correponsing to a key +it = mymap.find('Z'); +cout<second; + +// OUTPUT: 26 + + ``` Further Reading: -- cgit v1.2.3 From c50ff9299651ba3c856ee23cbdabeda2784b864c Mon Sep 17 00:00:00 2001 From: Jatin Dhankhar Date: Mon, 27 Jun 2016 19:00:07 +0530 Subject: Added the Logical and bitwise operators section, fixes merge issue with #1817 (#2292) * Added the Logical and bitwise operators section * Added a note for Short Circuit evaluation Excerpt from https://en.wikipedia.org/wiki/Short-circuit_evaluation C++ uses minimal evaluation, or McCarthy evaluation (after John McCarthy (computer scientist)) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true. In some programming languages (Lisp), the usual Boolean operators are short-circuit. --- c++.html.markdown | 57 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 6b81f95f..290633f3 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Geoff Liu", "http://geoffliu.me"] - ["Connor Waters", "http://github.com/connorwaters"] - ["Ankush Goyal", "http://github.com/ankushg07"] + - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] lang: en --- @@ -1005,7 +1006,7 @@ cin>>val; Vector_name.push_back(val); // will push the value of variable into array // To iterate through vector, we have 2 choices -// using normal looping +// using normal looping for(int i=0; i // Will include the header file for sets @@ -1031,7 +1032,7 @@ ST.insert(20); // Will insert the value 20 in set ST ST.insert(30); // Will insert the value 30 in set ST // Now elements of sets are as follows // 10 20 30 - + // To erase an element ST.erase(20); // Will erase element with value 20 // Set ST: 10 30 @@ -1041,7 +1042,7 @@ for(it=ST.begin();it('A',1) ); mymap.insert ( pair('Z',26) ); // Will insert value 26 for key Z -// To iterate +// To iterate map::iterator it; for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << "->" << it->second <<'\n'; -// Output: +// Output: // A->1 // Z->26 @@ -1078,6 +1079,50 @@ cout<second; // OUTPUT: 26 +/////////////////////////////////// +// Logical and Bitwise operators +////////////////////////////////// + +// Most of the operators in C++ are same as in other languages + +// Logical operators + +// C++ uses Short - circuit evaluation for boolean expressions, i.e, the second argument is executed or +// evaluated only if the first argument does not suffice to determine the value of the expression + +true && false // Performs **logical and** to yield false +true || false // Performs **logical or** to yield true +! true // Performs **logcical not** to yield + +// Instead of using symbols equivalent keywords can be used +true and false // Performs **logical and** to yield false +true or false // Performs **logical or** to yield true +not true // Performs **logcical not** to yield + +// Bitwise operators + +// **<<** Left Shift Operator +// << shifts bits to the left +4 << 1 // Shifts bits of 4 to left by 1 to give 8 +// x << n can be thought as x * 2^n + + +// **>>** Right Shift Operator +// << shifts bits to the right +4 >> 1 // Shifts bits of 4 to right by 1 to give 2 +// x << n can be thought as x / 2^n + +~4 // Performs a bitwise not +4 | 3 // Performs bitwise or +4 & 3 // Performs bitwise and +4 ^ 3 // Performs bitwise xor + +// Equivalent keywords are +compl 4 // Performs a bitwise not +4 bitor 3 // Performs bitwise or +4 bitand 3 // Performs bitwise and +4 xor 3 // Performs bitwise xor + ``` Further Reading: -- cgit v1.2.3 From 47679dfcbe82811b7042a5994baab448adc15dd4 Mon Sep 17 00:00:00 2001 From: Valery Cherepanov Date: Tue, 2 Aug 2016 16:32:38 +0300 Subject: Some minor (mostly stylistic) fixes in C++ --- c++.html.markdown | 98 +++++++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 290633f3..5dc1af59 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -808,8 +808,8 @@ void doSomethingWithAFile(const std::string& filename) // have default comparators, but you can override it. class Foo { public: - int j; - Foo(int a) : j(a) {} + int j; + Foo(int a) : j(a) {} }; struct compareFunction { bool operator()(const Foo& a, const Foo& b) const { @@ -948,7 +948,7 @@ f1 = f2; #include -// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members , +// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members, // its elements are accessed by their order in the tuple. // We start with constructing a tuple. @@ -958,10 +958,10 @@ const int maxN = 1e9; const int maxL = 15; auto second = make_tuple(maxN, maxL); -// printing elements of 'first' tuple +// Printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A -// printing elements of 'second' tuple +// Printing elements of 'second' tuple cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15 // Unpacking tuple into variables @@ -989,43 +989,43 @@ cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' ///////////////////// -// CONTAINERS +// Containers ///////////////////// -// Containers or the Standard Template Library are some predefined templates -// They manages the storage space for its elements and provide -// member functions to access and manipulate them +// Containers or the Standard Template Library are some predefined templates. +// They manage the storage space for its elements and provide +// member functions to access and manipulate them. -// Few containers are as follows:- +// Few containers are as follows: -// Vectors (Dynamic arrays) +// Vector (Dynamic array) // Allow us to Define the Array or list of objects at run time -#include // will include the header file for vector -vector< Data_Type > Vector_name; // used to initialize the vector -cin>>val; +#include +vector Vector_name; // used to initialize the vector +cin >> val; Vector_name.push_back(val); // will push the value of variable into array -// To iterate through vector, we have 2 choices -// using normal looping +// To iterate through vector, we have 2 choices: +// Normal looping for(int i=0; i::iterator it; // initialize the iteartor for vector for(it=vector_name.begin(); it!=vector_name.end();++it) // For accessing the element of the vector // Operator [] -var= vector_name[index]; //will assign value at that index to var +var = vector_name[index]; // Will assign value at that index to var // Set -// Sets are containers that store unique elements following a specific order -// Very useful container to store unique values in sorted order -// without any other functions or code +// Sets are containers that store unique elements following a specific order. +// Set is a very useful container to store unique values in sorted order +// without any other functions or code. -#include // Will include the header file for sets -set< int > ST; // Will initialize the set of int data type +#include +set ST; // Will initialize the set of int data type ST.insert(30); // Will insert the value 30 in set ST ST.insert(10); // Will insert the value 10 in set ST ST.insert(20); // Will insert the value 20 in set ST @@ -1037,47 +1037,47 @@ ST.insert(30); // Will insert the value 30 in set ST ST.erase(20); // Will erase element with value 20 // Set ST: 10 30 // To iterate through Set we use iterators -set< int >::iterator it; -for(it=ST.begin();it::iterator it; +for(it=ST.begin();it // Will include the header file for map -map< char, int >mymap; // Will initalize the map with key as char and value as int +#include +map mymap; // Will initalize the map with key as char and value as int -mymap.insert ( pair('A',1) ); +mymap.insert(pair('A',1)); // Will insert value 1 for key A -mymap.insert ( pair('Z',26) ); +mymap.insert(pair('Z',26)); // Will insert value 26 for key Z // To iterate map::iterator it; for (it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << "->" << it->second <<'\n'; + std::cout << it->first << "->" << it->second << '\n'; // Output: // A->1 // Z->26 // To find the value correponsing to a key it = mymap.find('Z'); -cout<second; +cout << it->second; + +// Output: 26 -// OUTPUT: 26 /////////////////////////////////// // Logical and Bitwise operators @@ -1087,17 +1087,17 @@ cout<second; // Logical operators -// C++ uses Short - circuit evaluation for boolean expressions, i.e, the second argument is executed or +// C++ uses Short-circuit evaluation for boolean expressions, i.e, the second argument is executed or // evaluated only if the first argument does not suffice to determine the value of the expression true && false // Performs **logical and** to yield false true || false // Performs **logical or** to yield true -! true // Performs **logcical not** to yield +! true // Performs **logical not** to yield false // Instead of using symbols equivalent keywords can be used true and false // Performs **logical and** to yield false -true or false // Performs **logical or** to yield true -not true // Performs **logcical not** to yield +true or false // Performs **logical or** to yield true +not true // Performs **logical not** to yield false // Bitwise operators @@ -1108,20 +1108,20 @@ not true // Performs **logcical not** to yield // **>>** Right Shift Operator -// << shifts bits to the right +// >> shifts bits to the right 4 >> 1 // Shifts bits of 4 to right by 1 to give 2 -// x << n can be thought as x / 2^n +// x >> n can be thought as x / 2^n -~4 // Performs a bitwise not +~4 // Performs a bitwise not 4 | 3 // Performs bitwise or 4 & 3 // Performs bitwise and 4 ^ 3 // Performs bitwise xor // Equivalent keywords are -compl 4 // Performs a bitwise not -4 bitor 3 // Performs bitwise or +compl 4 // Performs a bitwise not +4 bitor 3 // Performs bitwise or 4 bitand 3 // Performs bitwise and -4 xor 3 // Performs bitwise xor +4 xor 3 // Performs bitwise xor ``` -- cgit v1.2.3 From 042ed5038101d59a9d438cc2410fd9c91a343753 Mon Sep 17 00:00:00 2001 From: Andrew Chellis Date: Mon, 24 Oct 2016 03:43:42 -0400 Subject: [c++/en] Fix typo of iterator (#2501) iteartor -> iterator --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 5dc1af59..7dc9afbe 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1011,7 +1011,7 @@ for(int i=0; i::iterator it; // initialize the iteartor for vector +vector::iterator it; // initialize the iterator for vector for(it=vector_name.begin(); it!=vector_name.end();++it) // For accessing the element of the vector -- cgit v1.2.3 From 1f1f59d9b4c5c194d0e0c9362d9bab951094bbc9 Mon Sep 17 00:00:00 2001 From: George Gognadze Date: Sun, 30 Oct 2016 13:58:59 +0400 Subject: c++/en typo, formatting fixes (#2531) Space should be removed. before: // concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) after: // concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A', 3.14141) Typo initialize is correct form. before: map mymap; // Will initalize the map with key as char and value as int after: map mymap; // Will initialize the map with key as char and value as int Typo corresponding is correct form. before: // To find the value correponsing to a key after: // To find the value corresponding to a key --- c++.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 7dc9afbe..24d38df7 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -981,7 +981,7 @@ cout << tuple_size::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. auto concatenated_tuple = tuple_cat(first, second, third); -// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) +// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A', 3.14141) cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 @@ -1057,7 +1057,7 @@ cout << ST.size(); // will print the size of set ST // and a mapped value, following a specific order. #include -map mymap; // Will initalize the map with key as char and value as int +map mymap; // Will initialize the map with key as char and value as int mymap.insert(pair('A',1)); // Will insert value 1 for key A @@ -1072,7 +1072,7 @@ for (it=mymap.begin(); it!=mymap.end(); ++it) // A->1 // Z->26 -// To find the value correponsing to a key +// To find the value corresponding to a key it = mymap.find('Z'); cout << it->second; -- cgit v1.2.3 From ae16d450781aecd9ff4423deb2cf67317d309080 Mon Sep 17 00:00:00 2001 From: omgee Date: Tue, 3 Jan 2017 09:41:35 +0200 Subject: [c++/en,es,it,br,ru,ch] Fix forgotten namespace std:: (#2619) --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 24d38df7..5220fb0b 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -456,7 +456,7 @@ void Dog::print() const Dog::~Dog() { - cout << "Goodbye " << name << "\n"; + std::cout << "Goodbye " << name << "\n"; } int main() { -- cgit v1.2.3 From eb44c5bc91e5b14a43a1a61cd62775d7b2abd008 Mon Sep 17 00:00:00 2001 From: ven Date: Thu, 18 May 2017 12:48:46 +0200 Subject: Update c++.html.markdown --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 5220fb0b..1461c93e 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -217,7 +217,7 @@ cout << myString + myOtherString; // "Hello World" cout << myString + " You"; // "Hello You" -// C++ strings are mutable and have value semantics. +// C++ strings are mutable. myString.append(" Dog"); cout << myString; // "Hello Dog" -- cgit v1.2.3 From d69afc403942ce7417d35bdd4e573da641ca7016 Mon Sep 17 00:00:00 2001 From: Pratik Karki Date: Fri, 25 Aug 2017 14:24:43 +0545 Subject: fix language code suffix(#2832) --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 1461c93e..a0f06c84 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -8,7 +8,7 @@ contributors: - ["Connor Waters", "http://github.com/connorwaters"] - ["Ankush Goyal", "http://github.com/ankushg07"] - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] -lang: en + --- C++ is a systems programming language that, -- cgit v1.2.3 From c076537cf9a90d0be4848885ef09895b5aa12b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Polykanine=20A=2EK=2EA=2E=20Menelion=20Elens=C3=BA?= =?UTF-8?q?l=C3=AB?= Date: Wed, 30 Aug 2017 02:46:49 +0300 Subject: [c++/en] Added public declaration. Fixed #2805 --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index a0f06c84..23013410 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -8,7 +8,6 @@ contributors: - ["Connor Waters", "http://github.com/connorwaters"] - ["Ankush Goyal", "http://github.com/ankushg07"] - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] - --- C++ is a systems programming language that, @@ -474,6 +473,7 @@ int main() { // without a public or protected method for doing so class OwnedDog : public Dog { +public: void setOwner(const std::string& dogsOwner); // Override the behavior of the print function for all OwnedDogs. See -- cgit v1.2.3 From cd379d9e9eff734ae02708ba986b0fde1fa52d33 Mon Sep 17 00:00:00 2001 From: Mario Date: Tue, 12 Sep 2017 10:21:23 +0200 Subject: [c++/en] container -> vector (#2838) * container -> vector fixed errors: - "vector_name" and "Vector_name" (different case) would have resulted in a compile time error, now: "my_vector" enhancements: - typedef for consistency - two push_backs to show its purpose - both iteration types now have a working execution block (both output the vector's content) - the first "classic loop" now also shows the operator [], which therefor is removed from below - include and for with a white spaces for readability * removed the typedef the `typedef` was used to show that we will be using `string` as our base for all operations, but we are free to use any other type; of course it is technically not needed and might look like a redundancy. the two `cin` also look redundant, so I changed this into one `cin` and two `push_back`s --- c++.html.markdown | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 23013410..8d1c7a26 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1000,24 +1000,24 @@ cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' // Vector (Dynamic array) // Allow us to Define the Array or list of objects at run time -#include -vector Vector_name; // used to initialize the vector +#include +string val; +vector my_vector; // initialize the vector cin >> val; -Vector_name.push_back(val); // will push the value of variable into array +my_vector.push_back(val); // will push the value of 'val' into vector ("array") my_vector +my_vector.push_back(val); // will push the value into the vector again (now having two elements) -// To iterate through vector, we have 2 choices: -// Normal looping -for(int i=0; i::iterator it; // initialize the iterator for vector -for(it=vector_name.begin(); it!=vector_name.end();++it) - -// For accessing the element of the vector -// Operator [] -var = vector_name[index]; // Will assign value at that index to var +// To iterate through a vector we have 2 choices: +// Either classic looping (iterating through the vector from index 0 to its last index): +for (int i = 0; i < my_vector.size(); i++) { + cout << my_vector[i] << endl; // for accessing a vector's element we can use the operator [] +} +// or using an iterator: +vector::iterator it; // initialize the iterator for vector +for (it = my_vector.begin(); it != my_vector.end(); ++it) { + cout << *it << endl; +} // Set // Sets are containers that store unique elements following a specific order. -- cgit v1.2.3