http://learnxinyminutes.com/docs/c++/

C++ is a systems programming language that, according to its inventor Bjarne Stroustrup, was designed to

  • 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 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.

//////////////////
// 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: // A main() function in C++ should return an int,
// though void main() is 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)
{
// 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 ;
} // In C++, character literals are one byte.
sizeof('c') == // In C, character literals are the same size as ints.
sizeof('c') == sizeof() // C++ has strict prototyping
void func(); // function which accepts no arguments // In C
void func(); // function which may accept any number of arguments // 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 <cstdio> int main()
{
printf("Hello, world!\n");
return ;
} ///////////////////////
// Function overloading
/////////////////////// // C++ supports function overloading
// provided each function takes different parameters. void print(char const* myString)
{
printf("String %s\n", myString);
} void print(int myInt)
{
printf("My int is %d", myInt);
} int main()
{
print("Hello"); // Resolves to void print(const char*)
print(); // Resolves to void print(int)
} /////////////////////////////
// Default function arguments
///////////////////////////// // You can provide default arguments for a function
// if they are not provided by the caller. void doSomethingWithInts(int a = , int b = )
{
// Do something with the ints here
} int main()
{
doSomethingWithInts(); // a = 1, b = 4
doSomethingWithInts(); // a = 20, b = 4
doSomethingWithInts(, ); // a = 20, b = 5
} // Default arguments must be at the end of the arguments list. void invalidDeclaration(int a = , int b) // Error!
{
} /////////////
// Namespaces
///////////// // Namespaces provide separate scopes for variable, function,
// and other declarations.
// Namespaces can be nested. namespace First {
namespace Nested {
void foo()
{
printf("This is First::Nested::foo\n");
}
} // end namespace Nested
} // end namespace First namespace Second {
void foo()
{
printf("This is Second::foo\n")
}
} void foo()
{
printf("This is global foo\n");
} int main()
{
// Assume everything is from the namespace "Second"
// unless otherwise specified.
using namespace Second; 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 and output uses streams
// cin, cout, and cerr represent stdin, stdout, and stderr.
// << is the insertion operator and >> is the extraction operator. #include <iostream> // Include for I/O streams using namespace std; // Streams are in the std namespace (standard library) int main()
{
int myInt; // Prints to stdout (or terminal/screen)
cout << "Enter your favorite number:\n";
// Takes in input
cin >> myInt; // cout can also be formatted
cout << "Your favorite number is " << myInt << "\n";
// prints "Your favorite number is <myInt>" cerr << "Used for error messages";
} //////////
// Strings
////////// // Strings in C++ are objects and have many member functions
#include <string> using namespace std; // Strings are also in the namespace std (standard library) string myString = "Hello";
string myOtherString = " World"; // + is used for concatenation.
cout << myString + myOtherString; // "Hello World" cout << myString + " You"; // "Hello You" // C++ strings are mutable and have value semantics.
myString.append(" Dog");
cout << myString; // "Hello Dog" /////////////
// References
///////////// // 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. 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 << fooRef; // 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. //////////////////////////////////////////
// Classes and object-oriented programming
////////////////////////////////////////// // First example of classes
#include <iostream> // 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
// (see below)
// 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";
} // 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 = dogsName;
} void Dog::setWeight(int dogsWeight)
{
weight = dogsWeight;
} // 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 Dog::~Dog()
{
cout << "Goodbye " << name << "\n";
} int main() {
Dog myDog; // prints "A dog has been constructed"
myDog.setName("Barkley");
myDog.setWeight();
myDog.printDog(); // prints "Dog is Barkley and weighs 10 kg"
return ;
} // prints "Goodbye Barkley" // Inheritance: // This class inherits everything public and protected from the Dog class
class OwnedDog : public Dog { 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
// 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; private:
std::string owner;
}; // Meanwhile, in the corresponding .cpp file: void OwnedDog::setOwner(const std::string& dogsOwner)
{
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 <name> and weights <weight>"
// "Dog is owned by <owner>"
} //////////////////////////////////////////
// Initialization and Operator Overloading
////////////////////////////////////////// // 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 <iostream>
using namespace std; class Point {
public:
// Member variables can be given default values in this manner.
double x = ;
double y = ; // 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); // It would also make sense to add the - and -= operators,
// but we will skip those for brevity.
}; Point Point::operator+(const Point& rhs) const
{
// Create a new point that is the sum of this one and rhs.
return Point(x + rhs.x, y + rhs.y);
} Point& Point::operator+=(const Point& rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
} int main () {
Point up (,);
Point right (,);
// 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 ;
} /////////////////////
// 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 <exception> // 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
} ///////
// 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

Learn X in Y minutes Where X=c++的更多相关文章

  1. Learn clojure in Y minutes

    Learn X in Y minutes Where X=clojure Get the code: learnclojure.clj Clojure is a Lisp family languag ...

  2. Learn X in Y minutes(python一页纸代码)

    一篇非常好的文章,解释了python基本语法的方方面面: # Single line comments start with a hash. """ Multiline ...

  3. Learn Lua in 15 Minutes

    原文地址:http://tylerneylon.com/a/learn-lua/ Learn Lua in 15 Minutes more or less For a more in-depth Lu ...

  4. 十分钟入门less(翻译自:Learn lESS in 10 Minutes(or less))

    十分钟入门less(翻译自:Learn lESS in 10 Minutes(or less)) 注:本文为翻译文章,因翻译水平有限,难免有缺漏不足之处,可查看原文. 我们知道写css代码是非常枯燥的 ...

  5. Learn HTML5 in 5 Minutes!

    There's no question, HTML5 is a hot topic for developers. If you need a crash course to quickly unde ...

  6. 快速入门:十分钟学会PythonTutorial - Learn Python in 10 minutes

    This tutorial is available as a short ebook. The e-book features extra content from follow-up posts ...

  7. Learn jQuery in y seconds

    [兼容IE8以下没办法][虽不是Modern Web(不建议直接操作DOM)但也是一大利器] 个人推荐书[CSS 网站实录][JavaScript Dom 编程艺术][刚开始学不能太纠结机制机理原理因 ...

  8. 用Y分钟学会X

    Learn X in Y minutes是一个有趣的网站,里面列举了对很多编程语言和工具的极简教程,有各种语言版本的.

  9. 在 Y 分钟内学会 Python

    在 Y 分钟内学会 Python 这是翻译, 原文地址: Learn Python in Y Minutes 在 90 年代初, Python 由 Guido van Rossum 创造, 现在, 它 ...

随机推荐

  1. Python turtle绘图实例分析

    画一个红色的五角星 from turtle import * color('red','red') begin_fill() for i in range(5): fd(200) rt(144) en ...

  2. cherokee +php fastcgi 出现 No input file specified 故障一例

    在arch上编译cherokee 时用的--with-wwwroot=/srv/http.在建立虚拟服务器时,只要虚拟服务器的根目录位于/srv/http下,php页面都能正确运行.但只要将拟服务器的 ...

  3. QMsgPack简介

    QMsgPack简介 首先,关于MessagePack协议,访问http://msgpack.org可以了解详细的格式约定及各种语言的实现. MessagePack协议号称比JSON快,但速度的快慢这 ...

  4. Selenium2+python自动化70-unittest之跳过用例(skip)

    前言 当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例 ...

  5. 夏书祥-苹果iPhone多开

    微信多开:就是在手机上开多个微信应用,比方同一时候打开3个微信软件. 安卓微信多开下载地址:http://yunpan.cn/cZBvE42E7qQkf  訪问password 8509 苹果微信多开 ...

  6. C#编程兵书

    <C#编程兵书> 基本信息 作者: 张志强 胡君 丛书名: 程序员藏经阁 出版社:电子工业出版社 ISBN:9787121207402 上架时间:2013-8-26 出版日期:2013 年 ...

  7. JMeter压力测试和性能测试工具

    Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测 试但后来扩展到其他测试领域. 它可以用于测试静态和动态资源例如静态文件 ...

  8. 数学图形(1.20)N叶草

    有N个叶子的草 相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815 vertices = t = to (*PI) r = n ...

  9. [转]nginx折腾记(HTTP性能能测试,与Apache对比)

      话说nginx在大压力的环境中比apache的表现要好,于是下载了一个来折腾一下. 下载并编译安装,我的编译过程有点特别: 1.去除调试信息,修改$nginx_setup_path/auto/cc ...

  10. [Todo]对于thrift和protobuf比较好的描述

    比较跨语言通讯框架:thrift和Protobuf 全部thrift protobuf 前两天想在微博上发表一个观点:在现在的技术体系中,能用于描述通讯协议的方式很多,xml,json,protobu ...