From a05547486450bb8918bce322da2c2a8402989ba2 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Wed, 10 Sep 2014 18:04:18 -0400 Subject: Created c++ tutorial c++ --- c++.html.markdown | 349 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 349 insertions(+) create mode 100644 c++.html.markdown (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown new file mode 100644 index 00000000..8cf72e47 --- /dev/null +++ b/c++.html.markdown @@ -0,0 +1,349 @@ +--- +language: c++ +filename: learncpp.cpp +contributors: + - ["Steven Basart", "http://github.com/xksteven"] +lang: en +--- + +I am writing this to highlight the differences and +additions that C++ has with respect to C. My +suggestion would be to follow the C tutorial first +then look here for the additions and differences. + +```c++ +/////////////////////////////////////// +// C++ differences +/////////////////////////////////////// + + +//In C++ +//cannot use void main() +int main() { //or int main(int argc, char **argv) + //cannot end with return; + return 0; + //Can also end without return statement +} + +//In C++ +/* + //This could lead to compiler errors and is discouraged + //#if 0 #endif pairs are encouraged instead +*/ + +//In C++ +sizeof(10) //Typically 4 +sizeof('c') == 1 + +//In C +sizeof('c') == sizeof(10) //true chars are passed as ints + + +//In C++ strict prototyping +void func(); //function which accepts no arguments + +//In C +void func(); //function which may accept arguments + + +//In C++ +for(int i = 0; i < 10; i++) {;} +//In C must int i must be declared before + + +//C++ Supports Function overloading +//Provided each function takes different +//parameters + +void printing(char const *myString) +{printf("String %s\n",myString);} //Hello + +void printing(int myInt) +{printf("My int is %d",myInt);} //15 + +int main () +{ + printing("Hello"); + printing(15); +} + + + +//C++ Default Function Arguments +void two_ints(int a = 1, int b = 4); + +int main() +{ + two_ints(); // arguments: 1, 4 + two_ints(20); // arguments: 20, 4 + two_ints(20, 5); // arguments: 20, 5 +} + + +//C++ added the nullptr which is different from 0 +int *ip = nullptr; // OK +int value = nullptr; // error: value is no pointer + + +/////////////////////////////////////// +// C++ Additions ontop of C +/////////////////////////////////////// + + +/////////////////////////////////////// +// C++ Namespace +/////////////////////////////////////// + +//Namespaces allow you to define your own +//functions and variables for use + +// Use '::' to change variable (or function) scope +// Putting '::' before a function or variable will +// reference a global scope + +// This allows you to make normal c library calls +// std is for standard library +using namespace std; + +#include + +int counter = 50; // global variable + +int main() +{ + for (int counter = 1; // this refers to the + counter < 2; // local variable + counter++) + { + printf("Global var %d local var %d\n", + ::counter, // global variable + counter); // local variable + // => Global var 50 local var 1 + } +} + +// Namespaces can be nested + + +namespace myFirstNameSpace +{ + namespace myInnerSoul + { + cos(int x) + { + printf("My inner soul was made to program."); + } + } +} + +namespace anotherNameSpace +{ + cos(int x) {;} //does nothing +} + +int main() +{ + //Specify the full path because main is outside of both namespaces. + //Will print out My inner soul was made to program. + myFirstNameSpace::myInnerSoul::cos(60); +} + + +/////////////////////////////////////// +// C++ Strings +/////////////////////////////////////// + +//Strings in C++ are Objects and have many functions +myString = "Hello"; +myOtherString = " World"; + +myString + myOtherString; // => "Hello World" + +myString + ' You'; // => "Hello You" + +myString != myOtherString; //True + +//An example of a string method +myString.append(" Dog"); // => "Hello Dog" + + +/////////////////////////////////////// +// C++ Input Output +/////////////////////////////////////// + +//C++ input and output streams +//cin, cout, cerr, << is insertion and >> is extraction operator +#include + +using namespace std; + +int main() +{ + + int myInt; + + //Prints to stdout (or terminal/screen) + cout << "Enter your fav number:\n" + //Takes in input + cin >> myInt; + + //cout can also be formatted + cout << "Your fav number is " << myInt << "\n" + //Your fav number is ## + + cerr << "Used for error messages" +} + + +/////////////////////////////////////// +// C++ Classes +/////////////////////////////////////// + + +//First example of classes +#include + +//define a class +class Doggie +{ + std::string name; + int weight; + + // These are only the declarations + //Can also have private and protected + public: + //The public methods (can also include variables) + + // Default constructor + Doggie(); + + void setName(std::string dogsName); + void setWeight(int dogsWeight); + void printDog(); + + //Can define functions within class declaration too + void dogBark() {std::cout << "Bark Bark\n"} + + //Destructors are methods that free the allocated space + ~doggieDestructor(); + //if no destructor compiler defines the trivial destructor + +//Classes are similar to structs and must close the } with ; +}; + +// This is the implementation of the class methods +// Also called the definition +void Doggie::Doggie () { + std::cout << "A doggie is born. Woof!\n"; +} + +void Doggie::setName (std::string doggie_name) { + name = doggie_name; +} + +void Doggie::setWeight (int doggie_weight) { + weight = doggie_weight; +} + +void Doggie::printDog () { + std::cout << "Dog is " << name << " weighs" << weight << "\n"; +} + +void Doggie::~doggieDestructor () { + delete[] name; + delete weight; +} + +int main () { + Doggie deedee; // prints out a doggie is born. Woof! + deedee.setName ("Barkley"); + deedee.setWeight(1000000); + deedee.printDog; + //prints => Dog is Barkley weighs 1000000 + return 0; +} + + +//C++ Class inheritance + +class German_Sheperd +{ + //This class now inherits everything public and protected from Doggie class + Doggie d_dog; + + //Good practice to put d_ in front of datatypes in classes + std::string d_type; + + public: + void dogType() {d_type = "German Sheperd";} +}; + + + +/////////////////////////////////////// +// C++ Exception Handling +/////////////////////////////////////// + +try { + throw 12.25; // throws a double no handler declared +} catch (int errorNum) +{ + std::cout << "I caught an int " << errorNum << "\n"; +//default catcher +} catch (...) +{ + std::cout << "I got an error. Not sure what but I can pass it up."; + throw; +} + + +/////////////////////////////////////// +// C++ Operator Overloading +/////////////////////////////////////// + +// In C++ you can overload operators such as +, -, new, etc. + +#include +using namespace std; + +class Vector { + public: + double x,y; + Vector () {}; + Vector (double a, double b) : x(a), y(b) {} + Vector operator + (const CVector&); + Vector operator += (const CVector&); +}; + +Vector Vector::operator+ (const Vector& rhs) +{ + Vector temp; + temp.x = x + rhs.x; + temp.y = y + rhs.y; + return temp; +} + +Vector Vector::operator+= (const Vector& rhs) +{ + x += rhs.x; + y += rhs.y; + return *this; +} + +int main () { + Vector up (0,1); + Vector right (1,0); + Vector result; + // This calls the Vector + operator + // Vector up calls the + (function) with right as its paramater + result = up + right; + // prints out => Result is upright (1,1) + cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; + return 0; +} + +``` +Futher Reading + +for more resources see: http://www.icce.rug.nl/documents/cplusplus/ +for other reference material: http://www.cplusplus.com/doc/tutorial/ -- cgit v1.2.3 From 39b7ae4d188c0cc366db17558d3fdd42941df25f Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 13 Sep 2014 15:04:49 +0600 Subject: Added missing ";" --- 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 8cf72e47..7609dd46 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -183,15 +183,15 @@ int main() int myInt; //Prints to stdout (or terminal/screen) - cout << "Enter your fav number:\n" + cout << "Enter your fav number:\n"; //Takes in input cin >> myInt; //cout can also be formatted - cout << "Your fav number is " << myInt << "\n" + cout << "Your fav number is " << myInt << "\n"; //Your fav number is ## - cerr << "Used for error messages" + cerr << "Used for error messages"; } -- cgit v1.2.3 From 7b95a1e9217c6a40d9300681adf1bc2f217507da Mon Sep 17 00:00:00 2001 From: eternalthinker Date: Wed, 24 Sep 2014 08:42:21 +0530 Subject: Added correct syntax for c++ inheritance. [more] Minor indent correction at namespaces intro example --- c++.html.markdown | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 7609dd46..5bf7e2ea 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -132,7 +132,7 @@ namespace myFirstNameSpace cos(int x) { printf("My inner soul was made to program."); - } + } } } @@ -266,10 +266,9 @@ int main () { //C++ Class inheritance -class German_Sheperd +class German_Sheperd : public Doggie { - //This class now inherits everything public and protected from Doggie class - Doggie d_dog; + //This class now inherits everything public and protected from Doggie class //Good practice to put d_ in front of datatypes in classes std::string d_type; -- cgit v1.2.3 From dbb02b06b588f1d3146e5ea7f4a3701d36a22ab1 Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Thu, 9 Oct 2014 22:57:03 -0700 Subject: Add self as contributor to C++ doc --- 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 5bf7e2ea..f51d49c0 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -3,6 +3,7 @@ language: c++ filename: learncpp.cpp contributors: - ["Steven Basart", "http://github.com/xksteven"] + - ["Matt Kline", "https://github.com/mrkline"] lang: en --- -- cgit v1.2.3 From b39a6827e93edccf09529fdb9e6c346aeec21a73 Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Thu, 9 Oct 2014 22:57:52 -0700 Subject: Remove trailing whitspace from c++.html.markdown --- c++.html.markdown | 529 +++++++++++++++++++++++++++--------------------------- 1 file changed, 265 insertions(+), 264 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index f51d49c0..19946462 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -7,343 +7,344 @@ contributors: lang: en --- -I am writing this to highlight the differences and -additions that C++ has with respect to C. My +I am writing this to highlight the differences and +additions that C++ has with respect to C. My suggestion would be to follow the C tutorial first then look here for the additions and differences. ```c++ -/////////////////////////////////////// -// C++ differences -/////////////////////////////////////// +/////////////////////////////////////// +// C++ differences +/////////////////////////////////////// -//In C++ -//cannot use void main() -int main() { //or int main(int argc, char **argv) - //cannot end with return; - return 0; - //Can also end without return statement -} - -//In C++ -/* - //This could lead to compiler errors and is discouraged - //#if 0 #endif pairs are encouraged instead -*/ - -//In C++ -sizeof(10) //Typically 4 -sizeof('c') == 1 +//In C++ +//cannot use void main() +int main() { //or int main(int argc, char **argv) + //cannot end with return; + return 0; + //Can also end without return statement +} -//In C -sizeof('c') == sizeof(10) //true chars are passed as ints +//In C++ +/* + //This could lead to compiler errors and is discouraged + //#if 0 #endif pairs are encouraged instead +*/ +//In C++ +sizeof(10) //Typically 4 +sizeof('c') == 1 -//In C++ strict prototyping -void func(); //function which accepts no arguments +//In C +sizeof('c') == sizeof(10) //true chars are passed as ints -//In C -void func(); //function which may accept arguments - - -//In C++ -for(int i = 0; i < 10; i++) {;} -//In C must int i must be declared before +//In C++ strict prototyping +void func(); //function which accepts no arguments -//C++ Supports Function overloading -//Provided each function takes different -//parameters +//In C +void func(); //function which may accept arguments -void printing(char const *myString) -{printf("String %s\n",myString);} //Hello -void printing(int myInt) -{printf("My int is %d",myInt);} //15 +//In C++ +for(int i = 0; i < 10; i++) {;} +//In C must int i must be declared before -int main () -{ - printing("Hello"); - printing(15); -} - +//C++ Supports Function overloading +//Provided each function takes different +//parameters -//C++ Default Function Arguments -void two_ints(int a = 1, int b = 4); +void printing(char const *myString) +{printf("String %s\n",myString);} //Hello -int main() -{ - two_ints(); // arguments: 1, 4 - two_ints(20); // arguments: 20, 4 - two_ints(20, 5); // arguments: 20, 5 -} +void printing(int myInt) +{printf("My int is %d",myInt);} //15 +int main () +{ + printing("Hello"); + printing(15); +} -//C++ added the nullptr which is different from 0 -int *ip = nullptr; // OK -int value = nullptr; // error: value is no pointer -/////////////////////////////////////// -// C++ Additions ontop of C -/////////////////////////////////////// +//C++ Default Function Arguments +void two_ints(int a = 1, int b = 4); +int main() +{ + two_ints(); // arguments: 1, 4 + two_ints(20); // arguments: 20, 4 + two_ints(20, 5); // arguments: 20, 5 +} -/////////////////////////////////////// -// C++ Namespace -/////////////////////////////////////// -//Namespaces allow you to define your own -//functions and variables for use +//C++ added the nullptr which is different from 0 +int *ip = nullptr; // OK +int value = nullptr; // error: value is no pointer -// Use '::' to change variable (or function) scope -// Putting '::' before a function or variable will -// reference a global scope -// This allows you to make normal c library calls -// std is for standard library -using namespace std; +/////////////////////////////////////// +// C++ Additions ontop of C +/////////////////////////////////////// -#include -int counter = 50; // global variable +/////////////////////////////////////// +// C++ Namespace +/////////////////////////////////////// -int main() -{ - for (int counter = 1; // this refers to the - counter < 2; // local variable - counter++) - { - printf("Global var %d local var %d\n", - ::counter, // global variable - counter); // local variable - // => Global var 50 local var 1 - } -} +//Namespaces allow you to define your own +//functions and variables for use -// Namespaces can be nested +// Use '::' to change variable (or function) scope +// Putting '::' before a function or variable will +// reference a global scope +// This allows you to make normal c library calls +// std is for standard library +using namespace std; -namespace myFirstNameSpace -{ - namespace myInnerSoul - { - cos(int x) - { - printf("My inner soul was made to program."); - } - } -} +#include -namespace anotherNameSpace -{ - cos(int x) {;} //does nothing -} - -int main() -{ - //Specify the full path because main is outside of both namespaces. - //Will print out My inner soul was made to program. - myFirstNameSpace::myInnerSoul::cos(60); -} +int counter = 50; // global variable +int main() +{ + for (int counter = 1; // this refers to the + counter < 2; // local variable + counter++) + { + printf("Global var %d local var %d\n", + ::counter, // global variable + counter); // local variable + // => Global var 50 local var 1 + } +} -/////////////////////////////////////// -// C++ Strings -/////////////////////////////////////// +// Namespaces can be nested -//Strings in C++ are Objects and have many functions -myString = "Hello"; -myOtherString = " World"; -myString + myOtherString; // => "Hello World" +namespace myFirstNameSpace +{ + namespace myInnerSoul + { + cos(int x) + { + printf("My inner soul was made to program."); + } + } +} -myString + ' You'; // => "Hello You" +namespace anotherNameSpace +{ + cos(int x) {;} //does nothing +} -myString != myOtherString; //True +int main() +{ + //Specify the full path because main is outside of both namespaces. + //Will print out My inner soul was made to program. + myFirstNameSpace::myInnerSoul::cos(60); +} + + +/////////////////////////////////////// +// C++ Strings +/////////////////////////////////////// + +//Strings in C++ are Objects and have many functions +myString = "Hello"; +myOtherString = " World"; + +myString + myOtherString; // => "Hello World" -//An example of a string method -myString.append(" Dog"); // => "Hello Dog" +myString + ' You'; // => "Hello You" +myString != myOtherString; //True -/////////////////////////////////////// -// C++ Input Output -/////////////////////////////////////// +//An example of a string method +myString.append(" Dog"); // => "Hello Dog" -//C++ input and output streams -//cin, cout, cerr, << is insertion and >> is extraction operator -#include -using namespace std; +/////////////////////////////////////// +// C++ Input Output +/////////////////////////////////////// + +//C++ input and output streams +//cin, cout, cerr, << is insertion and >> is extraction operator +#include + +using namespace std; -int main() -{ +int main() +{ + + int myInt; - int myInt; - - //Prints to stdout (or terminal/screen) - cout << "Enter your fav number:\n"; - //Takes in input - cin >> myInt; + //Prints to stdout (or terminal/screen) + cout << "Enter your fav number:\n"; + //Takes in input + cin >> myInt; - //cout can also be formatted - cout << "Your fav number is " << myInt << "\n"; - //Your fav number is ## + //cout can also be formatted + cout << "Your fav number is " << myInt << "\n"; + //Your fav number is ## - cerr << "Used for error messages"; -} + cerr << "Used for error messages"; +} -/////////////////////////////////////// -// C++ Classes +/////////////////////////////////////// +// C++ Classes /////////////////////////////////////// -//First example of classes -#include +//First example of classes +#include -//define a class -class Doggie -{ - std::string name; - int weight; +//define a class +class Doggie +{ + std::string name; + int weight; - // These are only the declarations - //Can also have private and protected - public: - //The public methods (can also include variables) + // These are only the declarations + //Can also have private and protected + public: + //The public methods (can also include variables) - // Default constructor - Doggie(); + // Default constructor + Doggie(); - void setName(std::string dogsName); - void setWeight(int dogsWeight); - void printDog(); + void setName(std::string dogsName); + void setWeight(int dogsWeight); + void printDog(); - //Can define functions within class declaration too - void dogBark() {std::cout << "Bark Bark\n"} + //Can define functions within class declaration too + void dogBark() {std::cout << "Bark Bark\n"} - //Destructors are methods that free the allocated space - ~doggieDestructor(); - //if no destructor compiler defines the trivial destructor + //Destructors are methods that free the allocated space + ~doggieDestructor(); + //if no destructor compiler defines the trivial destructor -//Classes are similar to structs and must close the } with ; -}; +//Classes are similar to structs and must close the } with ; +}; -// This is the implementation of the class methods -// Also called the definition -void Doggie::Doggie () { - std::cout << "A doggie is born. Woof!\n"; -} - -void Doggie::setName (std::string doggie_name) { - name = doggie_name; -} +// This is the implementation of the class methods +// Also called the definition +void Doggie::Doggie () { + std::cout << "A doggie is born. Woof!\n"; +} -void Doggie::setWeight (int doggie_weight) { - weight = doggie_weight; -} +void Doggie::setName (std::string doggie_name) { + name = doggie_name; +} -void Doggie::printDog () { - std::cout << "Dog is " << name << " weighs" << weight << "\n"; -} +void Doggie::setWeight (int doggie_weight) { + weight = doggie_weight; +} -void Doggie::~doggieDestructor () { - delete[] name; - delete weight; -} +void Doggie::printDog () { + std::cout << "Dog is " << name << " weighs" << weight << "\n"; +} -int main () { - Doggie deedee; // prints out a doggie is born. Woof! - deedee.setName ("Barkley"); - deedee.setWeight(1000000); - deedee.printDog; - //prints => Dog is Barkley weighs 1000000 - return 0; -} +void Doggie::~doggieDestructor () { + delete[] name; + delete weight; +} + +int main () { + Doggie deedee; // prints out a doggie is born. Woof! + deedee.setName ("Barkley"); + deedee.setWeight(1000000); + deedee.printDog; + //prints => Dog is Barkley weighs 1000000 + return 0; +} -//C++ Class inheritance +//C++ Class inheritance -class German_Sheperd : public Doggie +class German_Sheperd : public Doggie { //This class now inherits everything public and protected from Doggie class - //Good practice to put d_ in front of datatypes in classes - std::string d_type; - - public: - void dogType() {d_type = "German Sheperd";} -}; - - - -/////////////////////////////////////// -// C++ Exception Handling -/////////////////////////////////////// - -try { - throw 12.25; // throws a double no handler declared -} catch (int errorNum) -{ - std::cout << "I caught an int " << errorNum << "\n"; -//default catcher -} catch (...) -{ - std::cout << "I got an error. Not sure what but I can pass it up."; - throw; -} - - -/////////////////////////////////////// -// C++ Operator Overloading -/////////////////////////////////////// - -// In C++ you can overload operators such as +, -, new, etc. - -#include -using namespace std; - -class Vector { - public: - double x,y; - Vector () {}; - Vector (double a, double b) : x(a), y(b) {} - Vector operator + (const CVector&); - Vector operator += (const CVector&); -}; - -Vector Vector::operator+ (const Vector& rhs) -{ - Vector temp; - temp.x = x + rhs.x; - temp.y = y + rhs.y; - return temp; -} - -Vector Vector::operator+= (const Vector& rhs) -{ - x += rhs.x; - y += rhs.y; - return *this; -} - -int main () { - Vector up (0,1); - Vector right (1,0); - Vector result; - // This calls the Vector + operator - // Vector up calls the + (function) with right as its paramater - result = up + right; - // prints out => Result is upright (1,1) - cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; - return 0; + //Good practice to put d_ in front of datatypes in classes + std::string d_type; + + public: + void dogType() {d_type = "German Sheperd";} +}; + + + +/////////////////////////////////////// +// C++ Exception Handling +/////////////////////////////////////// + +try { + throw 12.25; // throws a double no handler declared +} catch (int errorNum) +{ + std::cout << "I caught an int " << errorNum << "\n"; +//default catcher +} catch (...) +{ + std::cout << "I got an error. Not sure what but I can pass it up."; + throw; +} + + +/////////////////////////////////////// +// C++ Operator Overloading +/////////////////////////////////////// + +// In C++ you can overload operators such as +, -, new, etc. + +#include +using namespace std; + +class Vector { + public: + double x,y; + Vector () {}; + Vector (double a, double b) : x(a), y(b) {} + Vector operator + (const CVector&); + Vector operator += (const CVector&); +}; + +Vector Vector::operator+ (const Vector& rhs) +{ + Vector temp; + temp.x = x + rhs.x; + temp.y = y + rhs.y; + return temp; +} + +Vector Vector::operator+= (const Vector& rhs) +{ + x += rhs.x; + y += rhs.y; + return *this; +} + +int main () { + Vector up (0,1); + Vector right (1,0); + Vector result; + // This calls the Vector + operator + // Vector up calls the + (function) with right as its paramater + result = up + right; + // prints out => Result is upright (1,1) + cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; + return 0; } ``` -Futher Reading +Futher Reading + +for more resources see: http://www.icce.rug.nl/documents/cplusplus/ -for more resources see: http://www.icce.rug.nl/documents/cplusplus/ -for other reference material: http://www.cplusplus.com/doc/tutorial/ +for other reference material: http://www.cplusplus.com/doc/tutorial/ -- cgit v1.2.3 From e1ed5393fc5f44473fbb0acb40ff8a902bcdf1c1 Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Thu, 9 Oct 2014 23:06:05 -0700 Subject: Major overhaul of C++ documentation - Cleaned up and rephrased comments - Removed old and erroneous information - Normalized indentation to four spaces - Normalized style to "Stroustrup style" (http://www.stroustrup.com/bs_faq2.html#layout-style) - Added a section on references In the near future I plan on additional sections covering idiomatic use, such as RAII and C++11 paradigms. --- c++.html.markdown | 527 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 305 insertions(+), 222 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 19946462..ce2d2d65 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -7,323 +7,381 @@ contributors: lang: en --- -I am writing this to highlight the differences and -additions that C++ has with respect to C. My -suggestion would be to follow the C tutorial first -then look here for the additions and differences. +C++ was designed as a systems programming language that + +- is a "better C" +- supports data abstraction +- supports object-oriented programming +- supports generic programming + +Though its syntax can be more difficult or complex than newer languages, +it is widely used because it compiles to native instructions that can be +directly run by the processor and offers tight control over hardware (like C) +while offering high-level features such as generics, exceptions, and classes. +This combination of speed and functionality makes C++ +one of the most widely-used programming languages. ```c++ -/////////////////////////////////////// -// C++ differences -/////////////////////////////////////// +////////////////// +// Comparison to C +////////////////// +// 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: -//In C++ -//cannot use void main() -int main() { //or int main(int argc, char **argv) - //cannot end with return; - return 0; - //Can also end without return statement +// A main() function in C++ should return an int, +// though void main() is accepted by most compilers (gcc, clang, etc.) +int main() // or int main(int argc, char** argv) +{ + return 0; // Can also end without return statement } -//In C++ -/* - //This could lead to compiler errors and is discouraged - //#if 0 #endif pairs are encouraged instead -*/ - -//In C++ -sizeof(10) //Typically 4 +// In C++, character literals are one byte. sizeof('c') == 1 -//In C -sizeof('c') == sizeof(10) //true chars are passed as ints - +// In C, character literals are the same size as ints. +sizeof('c') == sizeof(10) -//In C++ strict prototyping -void func(); //function which accepts no arguments -//In C -void func(); //function which may accept arguments +// C++ has strict prototyping +void func(); // function which accepts no arguments +// In C +void func(); // function which may accept any number of arguments -//In C++ -for(int i = 0; i < 10; i++) {;} -//In C must int i must be declared before +// Use nullptr instead of NULL in C++ +int* ip = nullptr; +// C standard headers are available in C++, +// but are prefixed with "c" and have no .h suffix. +#include -//C++ Supports Function overloading -//Provided each function takes different -//parameters +int main() +{ + printf("Hello, world!\n"); + return 0; +} -void printing(char const *myString) -{printf("String %s\n",myString);} //Hello +/////////////////////// +// Function overloading +/////////////////////// -void printing(int myInt) -{printf("My int is %d",myInt);} //15 +// C++ supports function overloading +// provided each function takes different parameters. -int main () +void print(char const* myString) { - printing("Hello"); - printing(15); + printf("String %s\n", myString); } - - -//C++ Default Function Arguments -void two_ints(int a = 1, int b = 4); +void print(int myInt) +{ + printf("My int is %d", myInt); +} int main() { - two_ints(); // arguments: 1, 4 - two_ints(20); // arguments: 20, 4 - two_ints(20, 5); // arguments: 20, 5 + printing("Hello"); // Resolves to void print(const char*) + printing(15); // Resolves to void print(int) } +///////////////////////////// +// Default function arguments +///////////////////////////// -//C++ added the nullptr which is different from 0 -int *ip = nullptr; // OK -int value = nullptr; // error: value is no pointer - - -/////////////////////////////////////// -// C++ Additions ontop of C -/////////////////////////////////////// - - -/////////////////////////////////////// -// C++ Namespace -/////////////////////////////////////// - -//Namespaces allow you to define your own -//functions and variables for use - -// Use '::' to change variable (or function) scope -// Putting '::' before a function or variable will -// reference a global scope - -// This allows you to make normal c library calls -// std is for standard library -using namespace std; - -#include - -int counter = 50; // global variable +void two_ints(int a = 1, int b = 4); int main() { - for (int counter = 1; // this refers to the - counter < 2; // local variable - counter++) - { - printf("Global var %d local var %d\n", - ::counter, // global variable - counter); // local variable - // => Global var 50 local var 1 - } + two_ints(); // a = 1, b = 4 + two_ints(20); // a = 20, b = 4 + two_ints(20, 5); // a = 20, b = 5 } -// Namespaces can be nested +///////////// +// Namespaces +///////////// -namespace myFirstNameSpace -{ - namespace myInnerSoul - { - cos(int x) +// Namespaces provide separate scopes for variable, function, +// and other declarations. +// Namespaces can be nested + +namespace First { + namespace Nested { + void foo() { - printf("My inner soul was made to program."); + printf("This is First::Nested::foo\n"); } + } // end namespace Nested +} // end namespace First + +namespace Second { + void foo() + { + printf("This is Second::foo\n") } } -namespace anotherNameSpace +void foo() { - cos(int x) {;} //does nothing + printf("This is global foo\n"); } int main() { - //Specify the full path because main is outside of both namespaces. - //Will print out My inner soul was made to program. - myFirstNameSpace::myInnerSoul::cos(60); -} - - -/////////////////////////////////////// -// C++ Strings -/////////////////////////////////////// - -//Strings in C++ are Objects and have many functions -myString = "Hello"; -myOtherString = " World"; + // Assume everything is from the namespace "Second" + // unless otherwise specified. + using namespace Second; -myString + myOtherString; // => "Hello World" - -myString + ' You'; // => "Hello You" - -myString != myOtherString; //True - -//An example of a string method -myString.append(" Dog"); // => "Hello Dog" + foo(); // prints "This is Second::foo" + First::Nested::foo(); // prints "This is First::Nested::foo" + ::foo(); // prints "This is global foo" +} +/////////////// +// Input/Output +/////////////// -/////////////////////////////////////// -// C++ Input Output -/////////////////////////////////////// +// C++ input and output uses streams +// cin, cout, and cerr represent stdin, stdout, and stderr. +// << is the insertion operator and >> is the extraction operator. -//C++ input and output streams -//cin, cout, cerr, << is insertion and >> is extraction operator -#include +#include // Include for I/O streams using namespace std; int main() { - int myInt; - //Prints to stdout (or terminal/screen) + // Prints to stdout (or terminal/screen) cout << "Enter your fav number:\n"; - //Takes in input + // Takes in input cin >> myInt; - //cout can also be formatted + // cout can also be formatted cout << "Your fav number is " << myInt << "\n"; - //Your fav number is ## + // Your fav number is ## - cerr << "Used for error messages"; + cerr << "Used for error messages"; } +////////// +// Strings +////////// -/////////////////////////////////////// -// C++ Classes -/////////////////////////////////////// +// Strings in C++ are objects and have many member functions +#include +using namespace std; // Strings are in the namespace std (standard library) -//First example of classes -#include +string myString = "Hello"; +string myOtherString = " World"; -//define a class -class Doggie -{ - std::string name; - int weight; +// + is used for concatenation. +cout << myString + myOtherString; // "Hello World" - // These are only the declarations - //Can also have private and protected - public: - //The public methods (can also include variables) +cout << myString + " You"; // "Hello You" - // Default constructor - Doggie(); +// C++ strings are mutable and have value semantics. +myString.append(" Dog"); +cout << myString; // "Hello Dog" - void setName(std::string dogsName); - void setWeight(int dogsWeight); - void printDog(); - //Can define functions within class declaration too - void dogBark() {std::cout << "Bark Bark\n"} +///////////// +// References +///////////// - //Destructors are methods that free the allocated space - ~doggieDestructor(); - //if no destructor compiler defines the trivial destructor +// In addition to pointers like the ones in C, +// C++ has _references_. +// These are pointer types that cannot be reassigned once set +// and cannot be null. +// They also have the same syntax as the variable itself: +// No * is needed for dereferencing and +// & (address of) is not used for assignment. -//Classes are similar to structs and must close the } with ; -}; +using namespace std; + +string foo = "I am foo"; +string bar = "I am bar"; + + +string& fooRef = foo; // This creates a reference to foo. +fooRef += ". Hi!"; // Modifies foo through the reference +cout << foo; // Prints "I am foo. Hi!" + +fooRef = bar; // Error: references cannot be reassigned. + +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. -// This is the implementation of the class methods -// Also called the definition -void Doggie::Doggie () { - std::cout << "A doggie is born. Woof!\n"; +////////////////////////////////////////// +// Classes and object-oriented programming +////////////////////////////////////////// + +// First example of classes +#include + +// Declare a class. +// Classes are usually declared in header (.h or .hpp) files. +class Dog { + // Member variables and functions are private by default. + std::string name; + int weight; + +// All members following this are public +// until "private:" or "protected:" is found. +public: + + // Default constructor + Dog(); + + // Member function declarations (implementations to follow) + // Note that we use std::string here instead of placing + // using namespace std; + // above. + // Never put a "using namespace" statement in a header. + void setName(const std::string& dogsName); + + void setWeight(int dogsWeight); + + // Functions that do not modify the state of the object + // should be marked as const. + // This allows you to call them if given a const reference to the object. + // Also note the functions must be explicitly declared as _virtual_ + // in order to be overridden in derived classes. + // Functions are not virtual by default for performance reasons. + virtual void print() const; + + // Functions can also be defined inside the class body. + // Functions defined as such are automatically inlined. + 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. + // This enables powerful paradigms such as RAII + // (http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) + // Destructors must be virtual to allow classes to be derived from this one. + virtual ~Dog(); + +}; // A semicolon must follow the class definition. + +// Class member functions are usually implemented in .cpp files. +void Dog::Dog() +{ + std::cout << "A dog has been constructed\n"; } -void Doggie::setName (std::string doggie_name) { +// Objects (such as strings) should be passed by reference +// if you are modifying them or const reference if you are not. +void Dog::setName(const std::string& dogsName) +{ name = doggie_name; } -void Doggie::setWeight (int doggie_weight) { - weight = doggie_weight; +void Dog::setWeight(int dogsWeight) +{ + weight = dogsWeight; } -void Doggie::printDog () { - std::cout << "Dog is " << name << " weighs" << weight << "\n"; +// Notice that "virtual" is only needed in the declaration, not the definition. +void Dog::print() const +{ + std::cout << "Dog is " << name << " and weighs " << weight << "kg\n"; } -void Doggie::~doggieDestructor () { - delete[] name; - delete weight; +void Dog::~Dog() +{ + cout << "Goodbye " << name << "\n"; } -int main () { - Doggie deedee; // prints out a doggie is born. Woof! - deedee.setName ("Barkley"); - deedee.setWeight(1000000); - deedee.printDog; - //prints => Dog is Barkley weighs 1000000 - return 0; -} +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" + return 0; +} // prints "Goodbye Barkley" +// Inheritance: -//C++ Class inheritance +// This class inherits everything public and protected from the Dog class +class OwnedDog : public Dog { -class German_Sheperd : public Doggie -{ - //This class now inherits everything public and protected from Doggie class + void setOwner(const std::string& dogsOwner) - //Good practice to put d_ in front of datatypes in classes - std::string d_type; + // Override the behavior of the print function for all OwnedDogs. See + // http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping + // for a more general introduction if you are unfamiliar with + // subtype polymorphism. + // The override keyword is optional but makes sure you are actually + // overriding the method in a base class. + void print() const override; - public: - void dogType() {d_type = "German Sheperd";} +private: + std::string owner; }; +// Meanwhile, in the corresponding .cpp file: - -/////////////////////////////////////// -// C++ Exception Handling -/////////////////////////////////////// - -try { - throw 12.25; // throws a double no handler declared -} catch (int errorNum) +void OwnedDog::setOwner(const std::string& dogsOwner) { - std::cout << "I caught an int " << errorNum << "\n"; -//default catcher -} catch (...) -{ - std::cout << "I got an error. Not sure what but I can pass it up."; - throw; + owner = dogsOwner; } +void OwnedDog::print() const +{ + Dog::print(); // Call the print function in the base Dog class + std::cout << "Dog is owned by " << owner << "\n"; + // Prints "Dog is and weights " + // "Dog is owned by " +} -/////////////////////////////////////// -// C++ Operator Overloading -/////////////////////////////////////// +////////////////////////////////////////// +// Initialization and Operator Overloading +////////////////////////////////////////// -// In C++ you can overload operators such as +, -, new, etc. +// In C++ you can overload the behavior of operators such as +, -, *, /, etc. +// This is done by defining a function which is called +// whenever the operator is used. #include using namespace std; -class Vector { - public: - double x,y; - Vector () {}; - Vector (double a, double b) : x(a), y(b) {} - Vector operator + (const CVector&); - Vector operator += (const CVector&); +class Point { +public: + // Member variables can be given default values in this manner. + double x = 0; + double y = 0; + + // Define a default constructor which does nothing + // but initialize the Point to the default value (0, 0) + Point() { }; + + // The following syntax is known as an initialization list + // and is the proper way to initialize class member values + Point (double a, double b) : + x(a), + y(b) + { /* Do nothing except initialize the values */ } + + // Overload the + operator. + Point operator+(const Point& rhs) const; + + // Overload the += operator + Point& operator+=(const Point& rhs); }; -Vector Vector::operator+ (const Vector& rhs) +Point Point::operator+(const Point& rhs) const { - Vector temp; - temp.x = x + rhs.x; - temp.y = y + rhs.y; - return temp; + // Create a new point that is the sum of this one and rhs. + return Point(x + rhs.x, y + rhs.y); } -Vector Vector::operator+= (const Vector& rhs) +Point& Point::operator+=(const Point& rhs) { x += rhs.x; y += rhs.y; @@ -331,20 +389,45 @@ Vector Vector::operator+= (const Vector& rhs) } int main () { - Vector up (0,1); - Vector right (1,0); - Vector result; - // This calls the Vector + operator - // Vector up calls the + (function) with right as its paramater - result = up + right; - // prints out => Result is upright (1,1) + Point up (0,1); + Point right (1,0); + // This calls the Point + operator + // Point up calls the + (function) with right as its paramater + Point result = up + right; + // Prints "Result is upright (1,1)" cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; return 0; } +///////////////////// +// Exception Handling +///////////////////// + +// The standard library provides a few exception types +// (see http://en.cppreference.com/w/cpp/error/exception) +// but any type can be thrown an as exception +#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("A problem occurred"); +} +// Catch exceptions by const reference if they are objects +catch (const std::exception& ex) +{ + std::cout << ex.what(); +// Catches any exception not caught by previous _catch_ blocks +} catch (...) +{ + std::cout << "Unknown exception caught"; + throw; // Re-throws the exception +} ``` -Futher Reading +Futher Reading: -for more resources see: http://www.icce.rug.nl/documents/cplusplus/ +An up-to-date language reference can be found at + -for other reference material: http://www.cplusplus.com/doc/tutorial/ +Additional resources may be found at -- cgit v1.2.3 From 8e9d5af1ea5680bbf5f232d5510f35f3c69619c2 Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Sun, 12 Oct 2014 23:35:49 -0700 Subject: Minor C++ fixes --- c++.html.markdown | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index ce2d2d65..b55a764c 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -150,7 +150,7 @@ int main() #include // Include for I/O streams -using namespace std; +using namespace std; // Streams are in the std namespace (standard library) int main() { @@ -175,7 +175,7 @@ int main() // Strings in C++ are objects and have many member functions #include -using namespace std; // Strings are in the namespace std (standard library) +using namespace std; // Strings are also in the namespace std (standard library) string myString = "Hello"; string myOtherString = " World"; @@ -210,7 +210,7 @@ string bar = "I am bar"; string& fooRef = foo; // This creates a reference to foo. fooRef += ". Hi!"; // Modifies foo through the reference -cout << foo; // Prints "I am foo. Hi!" +cout << fooRef; // Prints "I am foo. Hi!" fooRef = bar; // Error: references cannot be reassigned. @@ -373,6 +373,9 @@ public: // Overload the += operator Point& operator+=(const Point& rhs); + + // It would also make sense to add the - and -= operators, + // but we will skip those for brevity. }; Point Point::operator+(const Point& rhs) const -- cgit v1.2.3 From eadecf8b95eba4afec302c0ab49b0ca0ca921299 Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Fri, 17 Oct 2014 00:03:33 -0700 Subject: Spell out favorite (instead of fav) in C++ doc --- 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 b55a764c..4a070d95 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -157,13 +157,13 @@ int main() int myInt; // Prints to stdout (or terminal/screen) - cout << "Enter your fav number:\n"; + cout << "Enter your favorite number:\n"; // Takes in input cin >> myInt; // cout can also be formatted - cout << "Your fav number is " << myInt << "\n"; - // Your fav number is ## + cout << "Your favorite number is " << myInt << "\n"; + // prints "Your favorite number is " cerr << "Used for error messages"; } -- cgit v1.2.3 From fbf3c6d588cf39525afd23cb01230a997440dfcf Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Fri, 17 Oct 2014 00:57:32 -0700 Subject: Add C++ section about RAII Future contributions will include standard library containers and C++11 features. --- c++.html.markdown | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 4a070d95..dbca751f 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -263,7 +263,7 @@ public: // Along with constructors, C++ provides destructors. // These are called when an object is deleted or falls out of scope. // This enables powerful paradigms such as RAII - // (http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) + // (see below) // Destructors must be virtual to allow classes to be derived from this one. virtual ~Dog(); @@ -427,6 +427,134 @@ catch (const std::exception& ex) std::cout << "Unknown exception caught"; throw; // Re-throws the exception } + +/////// +// RAII +/////// + +// RAII stands for Resource Allocation 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. + +// To understand how this is useful, +// consider a function that uses a C file handle: +void doSomethingWithAFile(const char* filename) +{ + // To begin with, assume nothing can fail. + + FILE* fh = fopen(filename, "r"); // Open the file in read mode. + + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + + fclose(fh); // Close the file handle. +} + +// 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). +// We now have to check each call for failure and close the file handle +// if a problem occurred. +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. + + // Assume each function returns false if it failed + if (!doSomethingWithTheFile(fh)) { + fclose(fh); // Close the file handle so it doesn't leak. + return false; // Propagate the error. + } + if (!doSomethingElseWithIt(fh)) { + fclose(fh); // Close the file handle so it doesn't leak. + return false; // Propagate the error. + } + + fclose(fh); // Close the file handle so it doesn't leak. + return true; // Indicate success +} + +// C programmers often clean this up a little bit using goto: +bool doSomethingWithAFile(const char* filename) +{ + FILE* fh = fopen(filename, "r"); + if (fh == nullptr) + reuturn false; + + if (!doSomethingWithTheFile(fh)) + goto failure; + + if (!doSomethingElseWithIt(fh)) + goto failure; + + fclose(fh); // Close the file + return true; // Indicate success + +failure: + fclose(fh); + return false; // Propagate the error +} + +// If the functions indicate errors using exceptions, +// things are a little cleaner, but still sub-optimal. +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."); + + try { + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + } + catch (...) { + fclose(fh); // Be sure to close the file if an error occurs. + throw; // Then re-throw the exception. + } + + fclose(fh); // Close the file + // Everything succeeded +} + +// 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. +void doSomethingWithAFile(const std::string& filename) +{ + // ifstream is short for input file stream + std::ifstream fh(filename); // Open the file + + // Do things with the file + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + +} // The file is automatically closed here by the destructor + +// This has _massive_ advantages: +// 1. No matter what happens, +// the resource (in this case the file handle) will be cleaned up. +// Once you write the destructor correctly, +// It is _impossible_ to forget to close the handle and leak the resource. +// 2. Note that the code is much cleaner. +// The destructor handles closing the file behind the scenes +// without you having to worry about it. +// 3. The code is exception safe. +// An exception can be thrown anywhere in the function and cleanup +// will still occur. + +// All idiomatic C++ code uses RAII extensively for all resources. +// Additional examples include +// - Memory using unique_ptr and shared_ptr +// - Containers - the standard library linked list, +// 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 ``` Futher Reading: -- cgit v1.2.3 From 03d1bc5ed97f16627afc64d4c7a53e84a0281906 Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Fri, 17 Oct 2014 18:42:30 -0700 Subject: Address @levibostian's concerns for #800 --- c++.html.markdown | 56 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index dbca751f..50de5eff 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -7,12 +7,14 @@ contributors: lang: en --- -C++ was designed as a systems programming language that +C++ is a systems programming language that, +[according to its inventor Bjarne Stroustrup](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote), +was designed to -- is a "better C" -- supports data abstraction -- supports object-oriented programming -- supports generic programming +- be a "better C" +- support data abstraction +- support object-oriented programming +- support generic programming Though its syntax can be more difficult or complex than newer languages, it is widely used because it compiles to native instructions that can be @@ -32,9 +34,21 @@ one of the most widely-used programming languages. // A main() function in C++ should return an int, // though void main() is accepted by most compilers (gcc, clang, etc.) -int main() // or int main(int argc, char** argv) +// 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) { - return 0; // Can also end without return statement + // Command line arguments are passed in by argc and argv in the same way + // they are in C. + // argc indicates the number of arguments, + // and argv is an array of C-style strings (char*) + // representing the arguments. + // The first argument is the name by which the program was called. + // argc and argv can be omitted if you do not care about arguments, + // giving the function signature of int main() + + // An exit status of 0 indicates success. + return 0; } // In C++, character literals are one byte. @@ -82,21 +96,33 @@ void print(int myInt) int main() { - printing("Hello"); // Resolves to void print(const char*) - printing(15); // Resolves to void print(int) + print("Hello"); // Resolves to void print(const char*) + print(15); // Resolves to void print(int) } ///////////////////////////// // Default function arguments ///////////////////////////// -void two_ints(int a = 1, int b = 4); +// You can provide default arguments for a function +// if they are not provided by the caller. + +void doSomethingWithInts(int a = 1, int b = 4) +{ + // Do something with the ints here +} int main() { - two_ints(); // a = 1, b = 4 - two_ints(20); // a = 20, b = 4 - two_ints(20, 5); // a = 20, b = 5 + doSomethingWithInts(); // a = 1, b = 4 + doSomethingWithInts(20); // a = 20, b = 4 + doSomethingWithInts(20, 5); // a = 20, b = 5 +} + +// Default arguments must be at the end of the arguments list. + +void invalidDeclaration(int a = 1, int b) // Error! +{ } @@ -106,7 +132,7 @@ int main() // Namespaces provide separate scopes for variable, function, // and other declarations. -// Namespaces can be nested +// Namespaces can be nested. namespace First { namespace Nested { @@ -362,7 +388,7 @@ public: Point() { }; // The following syntax is known as an initialization list - // and is the proper way to initialize class member values + // and is the proper way to initialize class member values Point (double a, double b) : x(a), y(b) -- cgit v1.2.3 From 00c4a6324e177b86c8b26318ed77d7373d8e716f Mon Sep 17 00:00:00 2001 From: Subramanian Date: Mon, 27 Oct 2014 09:06:43 +0530 Subject: correcting the setName method set the method argument dogsName to name, instead of doggie_name --- 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 50de5eff..9f357b08 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -305,7 +305,7 @@ void Dog::Dog() // if you are modifying them or const reference if you are not. void Dog::setName(const std::string& dogsName) { - name = doggie_name; + name = dogsName; } void Dog::setWeight(int dogsWeight) -- cgit v1.2.3 From bf493c07ed519cc3d41198a0720a5919f67f3ff0 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 1 Dec 2014 21:39:49 -0700 Subject: Reassignment to reference doesn't cause error --- 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 9f357b08..5f80f26f 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -238,7 +238,10 @@ string& fooRef = foo; // This creates a reference to foo. fooRef += ". Hi!"; // Modifies foo through the reference cout << fooRef; // Prints "I am foo. Hi!" -fooRef = bar; // Error: references cannot be reassigned. +// Doesn't reassign "fooRef". This is the same as "foo = bar", and +// foo == "I am bar" +// after this line. +fooRef = bar; 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 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