Introduction to C++ | Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

C++Introduction to C++
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Introduction to C++
C++ is an object oriented programming (OOP) language. It was developed by AT&T Bell Laboratories in the early 1979s by Bjarne Stroustrup. Its initial name was C with classes, but later on in 1983 it was renamed as C++. It is a deviation from procedural languages in the sense that it follows object oriented programming (OOP) approach which is quite suitable for managing large and complex programs. C++ language is an extension to C language and supports classes, inheritance, function overloading and operator overloading which were not supported by C language. The best way to start learning a programming is by writing a program. A simple C++ program has four sections as follows;

Section 1: The include Directive
Section 2: Class declaration and member functions
Section 3: Main function definition
Section 4: Declaration of an object

Simple C++ Program
#include<iostream.h>
Using namespace std;
int main()
{
cout<<”Hello World”;
return 0;
}
This is one of the simplest programs that can be written in C++ programming language. It contains all the fundamental components which every C++ program can have. Explanation of the codes of this program and sections is given below;

Section 1 – The include Directive
#include
Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indicates for the compiler’s pre-processor. In this case the directive #include tells the pre-processor to include the iostream standard file. This specific file (iostream) includes the declaration of the basic input-output library in C++, and it is included because its functionality is going to be used later in the program.

Section 2 – Class declaration and member function
Using namespace std;
All the elements of the standard ANSI C++ library are declared within namespace std;. The syntax of this command is (using namespace std;). In order to access its functionality we declare all the entities inside namespace std; This line is very often used in C++ programs that use the standard library and defines a scope for the identifiers that are used in a program.

Section 3 – Main function definition
int main()
This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it – the instructions contained within this function’s definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. The word main is followed in the code by a pair of parentheses ‘()’. That is because it is a function declaration. In C++, what differentiates a function declaration from other types of expressions is these parenthesis that follow its name. Optionally, these parentheses may enclose a list of parameters within them.

Secction 4 – Declaration of an object
Right after these parentheses we can find the body of the main function enclosed in braces ‘{ }’. What is contained within these braces is what the function does when it is executed.
Cout<<”Hello World”;
This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement is used to display output on the screen of the computer. ‘cout’ is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters. ‘cout’ is declared in the iostream standard file within the std namespace, so that’s why we needed to include that specify file and to declare that we were going to use this specific namespace earlier in our code. Notice that the statement ends with a semicolon character ‘;’. This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs.

return 0;
The return statement causes the main function to finish.

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag : ,

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary