The Structure of C++ Program
The structure of C++ program contains:
- Include files.
- Class declarations
- Member function definitions
- Main Function
#include<iostream>
#include<string>
using namespace std;
class<class-name>
{
[private:]
member declaration;
[protected:]
member declaration;
[public:]
member declaration;
};
[class member function declarations]
---
---
int main()
{
statements;
---
}
[other user defined functions]
---
---
Tokens in C++
Tokens are smallest individual units in a program.
- Tokens in C++ are
1) Keywords
2) Identifiers (Variables)
3) Literals (Constants)
4) Strings
5) Operators
6) Punctuators & Special Symbols - C++ program consists of tokens, white spaces and syntax of the language.
Keywords in C++
- Many of the keywords in C and C++ are common.
- Keyword implements the language features.
- Keywords are the identifiers reserved by language.
- Keywords have predefined meaning to compiler. They cannot be used as variable names or other user defined program elements.
- Following is the list of C++ keywords:
asm auto break case
catch char class const
continue default delete do
double else enum extern
float for friend goto
if inline int long
new operator private protected
public register return shot
signed sizeof static struct
switch template this throw
try typedef union unsigned
virtual void volatile while
- ANSI C++ added keywords are:
bool const_cast dynamic_cast
explicit export falsemutable
namespace true typename
static_cast typeid wchar_t
reinterpret_ cast
Identifiers in C++
- Identifier is a common term that represents name of variable, name of function, name of array, name of classes or in general any user defined element.
- Identifier is a fundamental requirement of programming language.
- An identifier can consists of alphabets,digits and underscores.
Variable in C++
- Variable is the name given to the memory location that stores data.
- Variable is a data item whose value varies during the program. Each variable used in program must be declared at the beginning of the function.
- All the variables must be declared before their use.
- The declaration syntax for variable is
data_type<variable_name>;
where data_type is any valid C++ datatype. - Variable declaration does two things.
a) It tells the compiler what the variable name is, and
b) It specify what type of data the variable will hold. - An integer variable holds integer constant, floating point variable hold float(real) value etc.
- A variable may be local or global.
- According to storage type, variable may be auto, external, static or register.
- Variable definition ends with semicolon.
- The value of variable can be changed in a program.
- Example.
Int a, b, c=45;
float p,q,r=2.5;
char z=’#’;
Literals in C++
- As per google character literals is a type of literal in programming for the representation of single characters value within source code of a computer program.
- Literals are the data items whose value remains unchanged throughout the program execution.
- They refer to fixed values.
- C++ supports several type of literal;
1. Integer constant
2. Character constant
3. Floating point constant
4. String constant. - Example:
1234 Decimal integer constant
123.456 Floating pint integer constant
O52 Octal integer constant
OX25 Hexadecimal integer constant
“Bangalore” String Constant
‘H’ Character constant
Symbolic Name in C++
- Symbolic name is the name declared at the beginning of the program and assigned a constant value.
- It is defined using preprocessor directive as below:
#define symbolic_name value - Generally symbolic name are written in uppercase to visually distinguish them from other variables.
- Its definition does not end with semicolon.
- These are not declared for any data type.
- In the program we can not change the value of symbolic constant.
- Example:
#define MAX 100
#define PI 3.1415
#define TOTAL_MARKS 50
Punctuators and Special Symbols
- [ ] Square bracket – These are used to indicate single and multidimensional array subscript.
- ( ) Round bracket or Parentheses – These are used to indicate function calls.
- { } Curly bracket or braces – These are used to indicate the start and end of a block or a compound statement.
- ,Comma – This is used to separate arguments in a list.
- * Asterisk – This is used to indicate pointer declaration.
- = equals to – This sign is used as an assignment operator.
- # Pound – This is used as pre-processor directive.
Rules for Constructing Variable Name
- Variable name may be a combination of alphabets (a—z, A…Z), digits (0…9) or underscores ( _ ).
- Sometimes, compiler in which case its length should not exceed 8 characters impose an additional constraint on the number of characters in the name.
- First character must be alphabet or an underscore.
- No comma or blank space is allowed.
- Among the special symbols, only underscore can be used in variable name.
- No word, having a reserved meaning in C++can be used for variable name.
- C++ distinguish between uppercase and lowercase characters.
- Variable name cannot start with a digit.
- In ANSI C++, there is no limit on the length of the variable name.
- Example:
roll_Number
name
city
mark1
mark2
address
employee_name
One thought on “Decoding the Structure of C++ Program: A Comprehensive Guide in 2023”