2.4 const Qualifier
The const type qualifier transforms an object into a constant.
When use const to define a variable, it's still an lvalue, but now the lvalue is unmodifiable.
Because we cannot subsequently change the value of an object declared to be const,we must initialize it when it is defined:
const std::string hi = "hello"; //ok: initialized
const int i, j = 0; //error: i is uninitialized const
When we define nonconst variable at global scope, it's accessible throughout the program. However, const variables declared at global scope are local to the file in which the object is defined.
We can make a const object accessible throughout the program by specifying that it is extern:
// file_1.cc
// define and initializes a const that is accessible to other files
extern const int bufsize = 1000;
// file_2.cc
extern const int bufsize; //declare
//uses bufsize defined in file_1
for (int = 1; int != bufsize; int++)
nonconst variables are extern by default.
2.5 Reference
A reference serves as an alternative name for an object.A reference is a compound type that is defined by preceding a variable name by the& symbol.A compound type is a type that is defined in terms of another type.
A reference must be initialized using an object of the same type (const or nonconst, int or double...) as the reference.
A reference is just another name for its alias. When a reference is initialized, it remains bound to that object as long as the reference exist. There is no way to rebind a reference to a different object.
int ival = 1024;
int &refval = ival; //ok:refval refers to ival
int &refval2; //error: a reference must be initialized
int &refval3 = 10; //error: initializer must be an object
A const reference can be initialized to an object of a different type or to an rvalue (The same initializations are not legal for nonconst references)
int = 42;
// legal for const references only
const int &r = 42;
const int &r2= r + i;
A nonconst reference may be attached only to an object of the same type as the reference itself.
A const reference may be bound to an object of a different but related type or to an rvalue.
2.6 Typedef Names
typedef double wages; //wages is a synonym for double
wages hourly, weekly; //double hourly, weekly
2.7 Enumerations
// input is 0, output is 1, test is 2
enum open_modes (input, output, test);
The value used to initialize an enumerator must be a constant expression.
// a is 2, b is 3, c is 3, d is 4
enum points (a = 2, b, c = 3, d);
points t = a; //ok: a is a Points enumerator
points s = 3; //error: s initialized with int
s = pi; //error: pi is not a Points enumerator
s = t; //ok: both are objects of Points enum type
2.8 Class Types
In C++ we define our own data types by defining a class.
Each class defines an interface and implementation:
Interface: consists of operations that we expect code that uses the class to execute.
Implementation: includes the data needed by the class. It also includes any functions needed to define the class but that are not intended for general use.
A class definition starts withe the keyword class followed by an identifier that names the class. The body of the class appears inside curly braces. The close curly must be followed by a semicolon.
class Sales_item {
public:
// operations on sales_item objects
private:
std::string isbn;
unsigned units_sold;
double revenue;
}; //must have a semicolon at the end
The operations and data that are part of a class are referred to as its
members.The operations are referred to as the
member functions and the data as
data members.
The class also may contain zero or more publicorprivateaccess label. An access label controls whether a member is accessible outside the class. Code that uses the class may access only the public members. A given label applies until the next access label is seen.
Member functions of the class may use any member of their own class, regardless of the access level.
We have two keyword to define class:
Class: default access label is private
Struct: default access label is public (like C)
2.9 Writing our own header files
2.9.1 Designing Our Own Headers
2.9.2 A Brief Introduction to the Preprocessor
The #include facility is a part of C++preprocessor.The preprocessor replaces each #include by the contents of the specified header.#ifndef SALE_H
#define SALE_H
// Definition of sale class and related functions goes here
#endif
The
#include directive takes one of two forms:
wiki of #includeguard:http://en.wikipedia.org/wiki/Include_guard
wiki of header file:http://en.wikipedia.org/wiki/Header_file