Master C++ Programming: A Comprehensive Tutorial for Beginners and Beyond

 Introduction to C++ :

C++ programstructure : 

To write C++ program, we have to follow step by step code-based statement’s structure called program structure. It has following structure of C++ program.  

t tructure : To write C++ program, we have to follow step
by step code-based statement’s structure called program structure.
It has following structure of C++ program.
Basic Program structure:
#include <iostream.h>

void main() 
{ 
 // program 
}
      
#include<iostream.h>
void main()
{
// main program
}

1. In this first line include Header file also called library of C++ like iostream.h,
conio.h, stdio.h,math.h etc.
2. The function named main () is a special function in all C++ programs; it is the
function called when the program is run. The execution of all C++ programs
begins with the main() function. In this void is keyword used as data type that
means it won’t t return value and main () is function that conveys information to
compiler that actual program is going to start from this place.
3. Curly bracket opening and closing (both brackets must match) have code area is
used to write the program. Every program must consist only one main().

Introduction to C++ programstructure : To write C++ program, we have to follow step
by step code-based statement’s structure called program structure.
It has following structure of C++ program.
Basic Program structure(POP)
#include<iostream.h>
void main()
{
// main program
}

In case of C++, it uses streams to perform input and output operations in standard input output devices (keyboard and monitor). A stream is an object which can either insert or extract the character from it.

1) cin (console-input) is Input statement used to enter the value from user by the keyboard. It is predefined object of istream class that follows redirection/ extractionoperator>>to read the input value from the console. This statement cannot be use to print information. 
EX. int num; cin>>num; 

2) cout (console-output) is output statement used to show the output. cout is
predefined object of ostream class that follows insertion operator<< to display
information.

int num;
cout<<”Enter the value of num : ”;
cin>>num;
cout<<” Entered num value is : “<<num; //cascade or chain of statement

Note : Here statement enclosed in “ “ is information that will print as it is and num is
variable that has some value, so it won’t enclosed in “ “.
First program
#include <iostream.h>

void main() 
{ 
 cout<< “ Hello this is my first  program in C++ \n “; 
 cout<< “ Welcome to my first program “; 
}
      

o/p 
Hello this is my first program in C++ 
Welcome to my first program 


C++ char set : 
It is set of valid chars used in C++ and recognized by language compiler. C++, can process any of the 256 ASCII characters as data or as literals(often referred to as constants). 
For Ex. 
1. Letters : A-Z,a-z 
2. Digits : 0-9 
3. Special Character : $,#,%,* etc. 

4. Formatting/Escape Sequence Character ( \ ) : Escape sequence char is used to modify the format of output by using special charswithin string literal i.e. “ “ . 
Ex.: \n(new line),\a (makes audible sound), \b(backspace(moves the cursor one space back),\t(Horizontal tab),\0(null character(most useful to terminate string),\?(print ?) etc

2. Token( Keywords, Identifiers, constants, operators) 
Token :It is smallest element of the program that is very useful to the compiler. 
C++ uses the following types of Tokens: 
1.Identifiers 
2. Keywords 
3. Constant 
4. Variables 
5. Operators


1.Identifier: An identifier is a name given to an entity. It is used to refer the name of variable, function, array, class or structure etc. which distinctly identifies an entity in a program at the time of its execution. 

Ex. age, dob, int, max etc. 
Ex. int n1 ;//int is keyword and a is variable. 
     float sum (); // a is function 
Variable is also an identifier; its name uniquely identifies itself in a program. A variable is a name given to a memory location that can hold a value. 
An identifier can be a variable, but not all identifiers are variables


Here, the fundamental difference between an identifier and variable:

An identifier is a “name given to entity” in a program whereas, a variable is a “name given to memory location”, that is used to hold value, which may get modified during program execution.

2. Keywords: (Identifier that has been reserved like int, float etc.)

1. It is predefined word that has been defined and stored in header file also known as reserved wordused for specific purpose. 
2.Keyword cannot be used as Identifier or variable. All keywords are written in lower case. 
Ex: int, float ,char ,do ,for, while, case,const, class, private, public etc

3. Constant: Constant is like normal variable, only difference is that their value cannot be modified by the program, once they have been defined. 

Syntax: 

const datatype variable_name=value; 
Ex. const int a=5; const pi=3.14; etc. 

4.Variable : a.A Variable is a name that is used to assignto a memory location, which is used to contain the corresponding value in it. 
b.A variable is user defined names consist sequence of characters/letters and digits. 
c.It start with first letter or _underscore. 
d. It store value that can be changed. 
Rules of Naming Variable : 
a. They must start with alphabet or _ underscore. 
b. No special char is allowed to form a variable. 
c. It should not be keyword. 
d. No space is allowed to form a variable. 
e. Variables are case sensitive. 

Declaration of variable:
Ex. 
int n1,n2,marks; //Declaration of variable

Program : Addition of two numbers

Data types is continued next. .. 

Post a Comment

0 Comments