Introduction to C++

C++ is a multi-paradigm programming language that supports object-oriented programming (OOP), created by Bjarne Stroustrup in 1983 at Bell Labs, C++ is an extension(superset) of C programming and the programs are written in C language can run in C++ compilers.

 

INSTRUCTION MUST READ
  • C++ Is very very important for CDAC. 
  • Before taking admission in CDAC you have to be very clear in C++ 
  • So please watch this 70 videos series very beautiful tutorial from Naresh  IT
  • If you complete this C++ tutorial video with all assignments I am giving here you will get huge benefit in CDAC
  • So if you really want your CDAC journey easy so must follow this instructions.

Why to learn C++ :

 

C++ is used by many programmers of different types and coming from different fields. C++ is mostly used to write device driver programs, system software, and applications that depend on direct hardware manipulation under real-time constraints. It is also used to teach the basics of object-oriented features because it is simple and is also used in the fields of research. It is used to create general systems software, drivers for various computer devices, software for servers and software for specific applications and also widely used in the creation of video games.

Advantages of C++ :

 

C++ is a highly portable language and is often the language of choice for multi-device, multi-platform app development.
C++ is an object-oriented programming language and includes classes, inheritance, polymorphism, data abstraction and encapsulation.
C++ has a rich function library.
C++ allows exception handling, and function overloading which are not possible in C.
C++ is a powerful, efficient and fast language. It finds a wide range of applications – from GUI applications to 3D graphics for games to real-time mathematical simulations.

Difference between C and C++ :

 

Diff NoCC++
1C is a structural or procedural programming language.C++ is an object oriented programming language.
2Functions are the fundamental building blocks.Objects are the fundamental building blocks.
3In C, the data is not secured.Data is hidden and can’t be accessed by external functions.
4C follows top down approach.C++ follows bottom up approach
5C uses scanf() and printf() function for standard input and output.C++ uses cin>> and cout<< for standard input and output.
6Variables must be defined at the beginning in the function.Variables can be defined anywhere in the function.
7In C, namespace feature is absent.In C++, namespace feature is present (we will see later).
8C is a middle level language.C++ is a high level language.
9Programs are divided into modules and functions.Programs are divided into classes and functions.
10C doesn’t support exception handling directly. Can be done by using some other functions.C++ supports exception handling. Done by using try and catch block.
11Features like function overloading and operator overloading is not
present.
C++ supports function overloading and operator overloading.
12C program file is saved with .C extension.C++ program file is saved with .CPP extension.

Introduction to OOPS in C++

Object oriented programming is a way of solving complex problems by breaking them into smaller problems using objects. Before Object Oriented Programming (commonly referred as OOP), programs were written in procedural language, they were nothing but a long list of instructions.

In Object oriented programming we write programs using classes and objects utilising features of OOPs such as abstraction, encapsulation, inheritance and polymorphism.

Class and Objects :


A class is like a blueprint of data member and functions and object is an instance of class. For example, lets say we have a class Car which has data members (variables) such as speed, weight, price and functions such as gearChange(), slowDown(), brake() etc. Now lets say I create a object of this class named FordFigo which uses these data members and functions and give them its own values. Similarly we can create as many objects as we want using the blueprint(class).

A Class is a user-defined data-type which has data members and member functions.
Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the properties and behaviour of the objects in a Class.
In of class Car, the data member will be speed limit, mileage etc and member functions can apply brakes, increase speed etc.

For example :
Consider the Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are their properties.

What is Objects ? :

My name is Abhijeet, and I am an instance/object of class Male. When we say, Human Being, Male or Female, we just mean a kind, you, your friend, me we are the forms of these classes. We have a physical existence while a class is just a logical definition. We are the objects.

Lets take an example :

                class person 
                { 
                    char name[20]; 
                    int id; 
                public: 
                    void getdetails(){} 
                }; 
                
                int main() 
                { 
                person p; // p is a object  
                }
        

 

 

 

Abstraction :


Abstraction refers to showing only the essential features of the application and hiding the details. In C++, classes can provide methods to the outside world to access & use the data variables, keeping the variables hidden from direct access, or classes can even declare everything accessible to everyone, or maybe just to the classes inheriting it. This can be done using access specifiers.

Take an real life example :

Suppose you are driving your car.., so when you are driving, at that time you only need to know about the basic functionalities of the car., like how steering works n all. And you are not going into the much detail about the internal working of the car. Same is in the case of programming, there are some information which is not important for users. So that kind of information is made private by the programmers. So in short, abstraction is a mechanism with the help of which programmers hide the unnecessary details from the users.

Encapsulation :


Encapsulation is all about binding the data variables and functions together in class.

Take an real life example :

For example, the pencil box which you use daily, that pencil box contains other things also like eraser, sharpner etc. Same in the case of programming,.the OOPS gives us the advantage of gathering the data and functions in the same box which is called “Class”.

Inheritance :


Inheritance is a way to reuse once written code again and again. The class which is inherited is called the Base class & the class which inherits is called the Derived class. They are also called parent and child class.

Take an real life example :

when child takes birth it inherit some qualities from his parents. Same in the case of programming., the derived class takes some characteristics from the base class. The inheritance is beneficial for programming because with the help of this we can make our code as short as possible and there will be not any kind of redundancy.

 

Polymorphism In OOPS :


Typically the pen has a method Write() which implements the writing part. However this could vary depending on the type of pen used. Writing with a fountain pen is much different from writing with a sketch pen. I could use a sketch pen only for drawing and coloring, which I could not with an ordinary fountain pen. So here you have the pen implementing write in different ways, based on the kind of pen you use.

Take an real life example :

Let’s take an simple example, we use milk for drinking..,but milk can also be used to make curd, butter etc. So, the term polymorphism means to use something in different forms. In programming, to maintain the code and to maintain the simplicity of the code we use the concept of polymorphism. Very popular example of this is, “function overloading”, in which we use the same name but the operations are different.