Skip to main content

Posts

Showing posts from March, 2017

C++ Primer Sec.2.4 const qualifier

const Qualifier Because we can’t change the value of a const object after we create it, it must be initialized just as reference const int i = get_size(); // ok: initialized at run time const int j = 42; // ok: initialized at compile time const int k; // error: k is uninitialized const when we use an object to initialize another object, it doesn’t matter whether either or both of the objects are consts. int i = 42; const int ci = i; // ok: the value in i is copied into ci int j = ci; // ok:the value in ci is copied into j By Default, const Objects Are Local to a File When we really need to share const between files, define a single instance of a const variable, we use the keyword extern on both its definition and declaration(s). // file_1.cc defines and initializes a const that is accessible to other files extern const int bufSize = fcn(); // file_1.h extern const int bufSize; // same bufSize as defined in file_1.cc References to const const int ci = 102...

C++ Primer Sec. 2.3 Compound Types

Compound Types References Reference is not an object. It is an alias. It is just another name for an already existing object. int ival = 1024; int &refVal = ival; //refVal referes to (is another name for) ival int &refVal2; // error: a reference must be initialized When we define a reference, instead of copying the initializer's value, we bind the reference to its initializer. Once initialized, a reference remains bound to its initial object. There is no way to rebind a reference to refer to a different object. Because there is no way to rebind a reference, references must be initialized. When we assign to a reference, we are assigning to the object to which the reference is bound. int i = refVal; //OKay i= 1024 Because references are not objects, we may not define a reference to a reference. We can define multiple references in a single definition. Each identifier that is a reference must be preceded by the & symbol: int ...

C++ Primer Sec. 2.1 2.2 Primitive Build-In Types and Variables

Primitive Build-in Types void type arithmetic type: bool, char, wchar_t, char16_t, char32_t, short, int, long, long long, float, double, long double Literals 'a' // character literal "Hello World!" // string literal L'a' // wide character literal, type is wchar_t u8"hi!" // utf-8 string literal (utf-8 encodes a Unicode character in 8 bits) 42ULL // unsigned integer literal, type is unsigned long long 1E-3F // single-precision floating-point literal, type is float 3.14159L // extended-precision floating-point literal, type is long double Variables Initialization is not assignment. Initialization happens when a variable is given a value when it is created. Assignment obliterates an object’s current value and replaces that value with a new one. Four different ways to define an int variable named units_sold and initialize it to 0. int units_sold = 0; int units_sold = {0}; int units_sold{0}; int units_sold(0); When used with va...

C++ Primer Ch1. Introduction

Different compilers use different suffix conventions; the most common include .cc , .cxx , .cpp , .cp , and .C .  To compile: $ CC prog1.cc prog1.exe is generated on Windows, a.out is generated on UNIX To run: $prog1 or $.\prog1 The command used to run the C++ compiler varies across compilers and operating systems. $ g++ -o prog1 prog1.cc Depending on the release of the GNU compiler you are using, you may need to specify -std=c++0x to turn on C++ 11 support. Typically, we put all the #include directives for a program at the beginning of the source file. std::cout << "Enter two numbers:" << std::endl;  Our output statement uses the << operator twice. Because the operator returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second.    Equivalent to: (std::cout << "Enter two numbers:") << std::end;   ...