Monday, 27 October 2014

Basics of C++ with examples

Basics of C++ :

Before going into the programming, You must know almost enough of the basics to create and compile a Program.  The programs that written in C++ will use iostream classes.
These read from and write to files and “standard” input and output.   In this simple program, a stream
object will be used to print a message on the screen.

Escape Sequence:

1. The “\” is an escape character.
2. The “\n” is used to change to a new line
3. The “\t” is used for horizontal tab.
4. The “\r” is used to carriage return – return the cursor to the beginning of current line.
5. The “\a” is an alert. It Sound the system bell.
6. The ‘\\” is used to print out a “\”.
7. The “\”” is used to print out a double quote.

Introduction to Namespace:

#include <iostream>
using namespace std;
int main ()
  cout << “Hello World! \n”;
  std::cout << “Hello World\n”;
  return 0;

What is a namespace? 

The namespace is a collection of software components with the same name prefix – e.g., std.
we need to mention the name space in each header file like
using namepace std;

The above statement tells that we are using the software components from the standard library

Object oriented programming:

C language is the procedure oriented programming, there are many advantages using the object oriented programming. The data encapsulation will cover the majority of them.
Reusing the code is also possible with the procedure programming.

The main purpose of object oriented programming is to simplify the design, programming and most importantly debugging a program. So to modify a particular data, it is easy to identify which function to use. To add additional features it is easy to identify where to add functions and its related data.
The heart of the oo programming is an object. The object has some properties and methods.
 The below are the few concepts that form the foundation of object-oriented programming:

This is the very basic thing for the object-oriented programming. This holds the member data and member functions.

A class is the blue print of the object. And it is a user defined type or the structure that is defined with the key word class. A class has the member data and the methods. This does not define any data, the data and the methods of the class can access through the object.
The data in the class is accessed by the three access specifiers:



The default access specifier of the class is private.

Member Variables - Each object has its unique set of member variables. An object's state is created by the values assigned to these instant variables.

Methods  - Class contains the methods . A method is basically a behavior. It is in methods where the logics are written, data is manipulated and all the actions are executed.

The data abstraction is one of the important features of oops. This will provide only required information to the outside world and hides the valid details.
For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data.


This is the most powerful feature of the object-oriented programming. This will allow the code reusability. Once the code   implemented and tested, without any doubt we can use it and which saves our time, cost, reduce the code size.. Inheritance is the process of the creating a new class using an existing class, which we called as base class, the new class is formed called as derived class.


This feature is to keep the data and the methods in the same place. But using the procedural language it is not sure that which functions work on which data, but object-oriented programming provides you framework to place the data and the relevant functions together in the same object.


Poly means many. A function or the operator works on many ways, which is called as polymorphism. Polymorphism is also the ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism.


This is also the part if the polymorphism. Overloading is the exiting operator or function is ready to operate on new data type.

No comments: