C++ Program Structure, Data Types and Type Conversion

In this part we will explain in detail about C++ Program Structure, Data Types and Type Conversion.

C++ Program Structure

The structure of a C++ program is:

  • Include Files 
  • Global Declaration Section  
  • Class Declaration or Definition Section 
  • Member Functions Section 
  • Main Function Section 


Character set and tokens

A. Character Set
Just like Nepali language have several characters like "Ka", "Kha","Ga",...etc and Greek language have several characters like "Alpha", Beta", "Theta", "Kappa", etc, the C++ programming language also have its own character set. The characters supported by C++ programming language can be grouped as follows:
1. Letters: 
Uppercase A…….Z, and lowercase a……..z 

2. Digits: 
All decimal digits 0…..9 

3. Special Characters:
  1. , comma
  2. . period
  3. ; semicolon
  4. : colon
  5. ? question mark
  6. ‘ apostrophe
  7. “ quotation mark
  8. ! exclamation mark / slash
  9. \ back slash
  10. ~ tilde
  11. _ underscore
  12. $ dollar sign
  13. % percentage sign
  14. & ampersand
  15. ^ caret * asterisk
  16. < less than sign
  17. > greater than sign
  18. ( left parenthesis
  19. ) right parenthesis
  20. [ left bracket
  21. ] right bracket
  22. { left brace
4. White Space Characters: horizontal tab, blank space, new line, carriage return and form feed.

B. Tokens

Smallest possible units of C++ programming language, which when combined together, can form a complete program are known as tokens. Just like subject, verb, object are the components of a sentence, tokens are the smallest possible units when a program written is broken into pieces. Following are the different tokens in C++:

1. Identifiers
2. Keywords
3. Integer constants
4. Real constants
5. Character Constants
6. String Constants
7. Operators
8. Special Symbols

1. Identifiers
Identifiers are the names given to the variables, function, array, structure etc in C++ programming language. Just like we name people like "Ram", "Shyam", "Rita", "Gita", "Sita", etc, we can name variables in C++ programming language. The rules for writing identifier are as follow:-
An identifier can have alphanumeric characters(a-z,A-Z,0-9) and underscore(_).
The first character of an identifier can only contain alphabet or underscore.
Identifiers are case sensitive. i.e. ram and Ram are different in C++ 
Keywords are not allowed to be used as identifiers.
→ No special characters such as semicolon, period, whitespace, slash or comma are permitted to be used in as an identifier.

2. Keywords
The special reserved words in C++ programming language are known as keyword. These words have their built in definitions which cannot be altered by the programmer.

Examples:
and, and_eq ,bool, catch ,delete, dynamic_cast ,friend, inline, ,not, not_eq ,private, protected,template, this, ,typeid, typename, xor, xor_eq


3. Integer constants
→ The integer numbers such as 5, 6, 12, 199, etc are known as integer constants.
→ Consider the following code snippet:
            int a; 
            a = 5;
→ In the above code snippet, a is an integer variable and 5 is integer constant.

4. Real constants
→ The decimal numbers such as 5.33, 6.98, 3.1414, etc are known as real constants.
→ Consider the following code snippet: 
            float b;
            b = 8.97;
→ In the above code snippet, b is a float type variable and 8.97 is real constant.
 
5. Character constants
→ The constants such as 'A', 'D', 'Y', etc are known as character constant.
→ Consider the following code snippet:
     char ch;
    ch = „A‟;
→ In the above code snippet, ch is a character variable and „A‟ is character constant.

6. String constants
→ The constants such as "Ram", "Shyam", "Hari", etc are known as string constants.
→ Consider the following code snippet: 
        char str[30];
        strcpy(str,”Pulchowk”);
→ In the above code snippet, str is a string variable and “Pulchowk” is a string constant.
→ Inclusion of the header file <cstring> has been assumed.

7. Operators
→ The symbols such as +, -, *, /, &, ->, etc are known as operators. Each of these operators perform some operation on operands.

8. Special Symbols
→ Following symbols are special symbols:
# . , () []  {}
Variable Declaration and Expression 
→ In C++, the variable can be declared by using following syntax:

data_type variable_name; or data_type
list_of_variables;
Example:int a; //Declares an integer variable a
float b; //Declares a float variable b
char c; //Declares a character variable c
long int ph; //Declares a long integer ph
int a,b,c; //Declares three integer vars a,b and c



When a variable is declared, the compiler automatically allocates a memory address for the variable. It is at that memory address, the values of the variable are stored.
→ The combination of operands and operators is known as an expression.
The expression has a value. 

For example:
int x=5,y; y=x*x-4*x+4;


In the above example the expression (x*x-4*x+4) has a value equal to 9 which is assigned to another variable y.

int a;
a=5;
cout<<a; //Displays value of a
cout<<&a; //Displays memory address Of a



cin and cout are the objects that are used for input and output purpose in C++.
→ These objects together with overloaded extraction(>>) and insertion(<<) operators helps to perform the function similar to scanf() and printf() function in C.
The general syntax of cin in C++ are given below:
        cin>>arg1>>arg2>>arg3 >> … … … >>argn;

Where, arg means argument and they are numbered from 1 to n. Each argument might be a variable. 

The general syntax of cout in C++ is given below: 
        cout<<arg1<<arg2<<arg3<< … … … <<argn;
Where, arg means argument and they are numbered from 1 to n. Each argument might be a variable, constant, expression, function call, etc representing a value.

Statements

Any line which terminates with a semicolon are known as statements.

For example:
int x,y,z;
cin>>x>>y;
z=x+y;
cout<<x<<y<<z;


Data Type

A data type is defined as a name or label for a set of values. They are used to define the kind of data being stored or manipulated.
→ The data type specifies the range of values that a variable or constant can hold and how that information is stored in the computer memory.

In C++, there are basically two types of type conversions. They are: 

A. Implicit Type Conversion:

  1. This type of conversion is automatically done by the compiler on its own without any external trigger from the user.
  2. It generally takes place when in an expression more than one data type is present.
  3. In such condition type conversion (type promotion) takes place to avoid lose of data.
  4. All the data types of the variables are upgraded to the data type of the variable with largest data type.
  5. The promotion rule in implicit type conversion is depicted below:
                i. bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double

Example: WAP in CPP to illustrate the concept of implicit type conversion.
#include<iostream>
using namespace std;
int main()
{ int x=5,z;
float y=2.2;
z=x+y;
cout<<"Direct value of x+y is "<<x+y<<endl;
cout<<"Value of Z="<<z; return 0; } Output:
Direct value of x+y is 7.2
Value of Z=7



B. Explicit Type Conversion:

This type of type conversion is  user defined. User can define how the type is to be converted.
The general syntax for explicit type conversion is
(type) expression;

Example: WAP in CPP to illustrate the concept of Explicit type conversion


#include<iostream>
using namespace std;
int main()
{ int x=5,z;
float y=2.2;
z=(int)(x+y);
cout<<"Direct value of x+y is "<<x+y<<endl;
cout<<"Value of Z="<<z; return 0;
}
Output:Direct value of x+y is 7.2
Value of Z=7


Preprocessor directives

Preprocessor directives are lines included in a program that begin with the character #, which makes them different from a typical statement or instruction.
→ They are known as preprocessor directives because they are read by the preprocessor during the start of the compilation process.
→ Preprocessor directives change the text of the source code and the result is a new source code without these directives.

Examples:
#include<iostream>
This type of preprocessor directive is used to include a header file iostream.h #define PI 3.1414
This type of preprocessor directive is used to define a symbolic constant called PI with a value 3.1414 #define f(x) x*x-4*x+5
This type of preprocessor directive is used to define a macro function f(x) whose value is given by the expression (x*x - 4*x + 5)


Some examples:

1.display the string “Hello, World”
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello, World";
return 0; }


Next:

2. find the sum of two integers entered by the user.
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two numbers:"<<endl;
cin>>a>>b;
cout<<"The sum of "<<a<<" and "<<b<<" is "<<a+b;
return 0; }


next:

3.convert temperature from ◦C to ◦F
#include<iostream>
using namespace std;
int main() {
float c,f;
cout<<"Enter a temperature in degree celcius:";
cin>>c;
f=9.0/5*c+32;
cout<<c<<" degree C = "<<f<<" degree F"<<endl;
return 0; }


One Last Example:

4.calculate the area of a circle using preprocessor directive
#include<iostream>
#define PI 3.1414
#define area(radius) PI*radius*radius
using namespace std;
int main()
{
float radius,a;
cout<<"Enter the value of radius:";
cin>>radius;
cout<<"Area is "<<area(radius);
return 0; }


Hope you obtained enough information about C++ Program Structure, Data Types and Type Conversion.