This post deals with definitions of few terms, understanding of which are important for having a strong foundation in C++. This is a living blog and you can expect appends with logs at the bottom of the post.
Logs:
1. 14 definitions added on September 9, 2018.
- Object: Most commonly accepted definition of an object is a region of memory that has a type.
- Variable: A named storage that can be manipulated.
- Scope:A scope is a part of program in which a name has a particular meaning.
'::' is the scope resolution operator used to refer to names from a different scope.
#include<iostream> using namespace std; int variable_1 = 10; int main(){ int variable_1 = 20; cout << :: variable_1 << endl; //prints 10 on console. ::variable_name fetches the variable from global scope. return 0; }
- Lifetime:
The lifetime of an object is the time during the programs execution that the object exists. - Declaration:
A declaration makes a name known to the program. We can declare a name without defining it by the use of extern keyword which is used to make compiler aware that the name has been defined somewhere else, may be in some other file (C++ supports separate compilation.).
Function declaration, also known as function prototype, composes of the return type, the name and the parameter list without a function body. A name can be declared multiple times but defined only once. - Definition:
A definition creates the associated entity. Any declaration that uses an explicit initializer is a definition even if it is with keyword extern. It is an error to provide an initializer on an extern inside a function. - Types:
A type defines both the contents of a data element and the operations that are possible on those data. - Manipulator:
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for example [1].
They are still regular functions and can also be called as any other function using a stream object as argument, for example [2].
Manipulators are used to change formatting parameters on streams and to insert or extract certain special characters.
[1] cout << boolalpha; [2] boolalpha(cout);
- Initialize:
Provide a value to the object at the time it is created. - Statement:
A part of program that specifies an action to take place when a program is executed. - Declarators:
The part of a declaration that includes the name being defined and an optional type modifier.
In the below snippet, '&' is the declarator. - Constant Reference:
There is no way to make a reference refer to different object, in some sense all references are const. const reference is generally used as an abbreviation for "reference to const". - Top and Low Level Constants:
Low level constants are integral to types and cannot be ignored. Top level constants specifies that the objects may not be changed.
int variable_one = 23; int* variable_ptr = &variable_one;
Applying a top level constant on the definition of variable_ptr would mean that pointer object itself is constant that is, variable_ptr can point to only a single object variable_one as in following example:
int* const variable_ptr = &variable_one;
Low level constants can only be applied to compound types such as references and pointers, with a meaning that the object they are associated to is constant. The following example would mean that we cannot change the value of variable_one with the help of variable_ptr.
const int* variable_ptr = &variable_one;
- Lvalues and Rvalues:
When we use an object as use an object as an rvalue, we use the object's value (its contents). When we use an object as an lvalue, we use the object's identity (its location in the memory.)
int array_var[20]; int &array_reference = array_var;
const int variable_one = 32; const int& variable_reference_one = variable_one; //ok int& variable_reference_two = variable_one; //error
References:
1. C++ Primer 5th Edition, Lippman - Lajoie - Moo
2. www.cplusplus.com
Logs:
1. 14 definitions added on September 9, 2018.
Wow! Important concepts put in such lucidity.
ReplyDeleteAnd, I loved the idea of a live blog. Way to go!