HOME C,C++ HOME



tally erp9 training

PROGRAMMING IN C++

INTRODUCTION TO C++

INTRODUCTION

INTRODUCTION: The C++ programming language was developed at AT & T Bell Laboratories in the early 1980’s by Bjarne Stroustrup. The features of C++ were a combination of the object-oriented features of a language called simula 67 and the power and elegance of C. Therefore, C++ is an extension of C with a major addition of the class construct features of simula 67.

C++ is a versatile language for handling very large programs. It is suitable for virtually any programming task including development of editors, compilers, databases, communication systems and any complex real-life application systems.

COMMENTS: In C++, a new symbol for comment ‘//’ (double slash) is introduced. A comment starts with a double slash symbol and terminals at the end of the line. Note that there is no closing symbol. The ‘C’ comment symbols ‘/*’ and ‘*/’ are also valid in C++ and can be used for multi-line comments.

The main() function:


// Ex-1: PGM ILLUSTRATES THE FUNCTION
#include
void main()
 {
cout<<”Hello Dear”;
}
 
Hello Dear

Here, #include causes the contents of the file iostream.h to be added to the program. It contains declarations for the identifier cout, cin and operator << and >>. This header file must be included at the beginning of all programs that use input/output statements.

In above program cout statement introduces two features specific to C++, cout and <<. The identifier cout is a predefined object that represents the standard output stream in C++.

A stream is an abstraction that refers to a flow of data.

The operator ‘<<’ is called the insertion operator or put to operator. It inserts the contents of the variable on its right, to the object on its left. The operator ‘<<’ has a simple interface, i.e., it is not necessary to specify the data type of the variable on its right. The insertion operation automatically assumes the data type of the variable.

Usage of the identifier ‘cin’:
The cin object is an instance of the class, istream. The class, istream is associated with the standard input device. This means that the cin object is capable of accepting input from the keyboard. The cin object also contents the input operator ‘>>’, which is used to obtain data from the standard input device and place it in a variable.

Built-in Data types in C++:
char - Occupies 1 Byte - To store characters and strings
int - Occupies 2 Bytes - To store integers
float - Occupies 4 Bytes - To store numbers with decimals.

The if..else construct:
The if conditional construct, is followed by a logical expression in which data is compared and a decision is made, based on the result of the comparison.

Syntax:
 if(boolean_expression)
{
 stmt(s);
}
else
{
stmt(s);
}

// EX-2: PGM ILLUSTRATES THE CONSTRUCT
#include
void main()
{
char ch;
clrscr();
cout<<"Enter a character between A to Z(Upper Case): ";
cin>>ch;
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
cout<<"Given character is vowel";
else
cout<<"Given character is consonent";
getch();
}
 
 

Note: #include performs the function clrscr() and getch()

In an if..else block of statements, the condition is evaluated first. If the condition is true, that is the value is non-zero, the statements in the immediate block are executed. If the condition is false, that is the value is 0, the statements in the else block are executed.