Iit Delhi 22
Iit Delhi 22
Iit Delhi 22
C C++
C was developed by Dennis Ritchie C++ was developed by Bjarne
between the year 1969 and 1973 at Stroustrup in 1979.
AT&T Bell Labs.
C does no support polymorphism, C++ supports polymorphism,
encapsulation, and inheritance encapsulation, and inheritance
which means that C does not because it is an object oriented
support object oriented programming language.
programming.
scanf() and printf() functions are cin and cout are used
used for input/output in C. for input/output in C++.
For the development of code, C C++ is known as hybrid language
supports procedural programming. because C++ supports
both proceduraland object
oriented programming paradigms.
Data and functions are separated Data and functions are
in C because it is a procedural encapsulated together in form of
programming language. an object in C++.
C C++
Functions in C are not defined Functions can be used inside a
inside structures. structure in C++.
Virtual and friend functions are not Virtual and friend functions are
supported by C. supported by C++.
C does not support inheritance. C++ supports inheritance.
C provides malloc() and calloc() C++ provides new operator for
functions for dynamic memory memory allocation and delete
allocation, and free() for memory operator for memory de-allocation.
de-allocation.
2. Explaining Object Oriented
Language.(C++)
OBJECT-ORIENTED PROGRAMMING (OOP) IS THE TERM
USED TO DESCRIBE A PROGRAMMING APPROACH BASED
ON OBJECTS AND CLASSES. THE OBJECT-ORIENTED
PARADIGM ALLOWS US TO ORGANIZE SOFTWARE AS A
COLLECTION OF OBJECTS THAT CONSIST OF BOTH DATA
AND BEHAVIOUR. THIS IS IN CONTRAST TO CONVENTIONAL
FUNCTIONAL PROGRAMMING PRACTICE THAT ONLY
LOOSELY CONNECTS DATA AND BEHAVIOUR.
Features of OOP
1. CLASS AND OBJECTS
2. INHERITANCE
3. POLYMORPHISM
4. ABSTRACTION
5. ENCAPSULATION
6. EXCEPTION HANDLING
Class
THE BUILDING BLOCK OF C++ THAT LEADS TO OBJECT ORIENTED PROGRAMMING IS
A CLASS. IT IS A USER DEFINED DATA TYPE, WHICH HOLDS ITS OWN DATA MEMBERS AND
MEMBER FUNCTIONS, WHICH CAN BE ACCESSED AND USED BY CREATING AN INSTANCE OF
THAT CLASS. A CLASS IS LIKE A BLUEPRINT FOR AN OBJECT.
OBJECTS
AN OBJECT IS AN INSTANCE OF A CLASS. WHEN A CLASS IS DEFINED, NO MEMORY IS
ALLOCATED BUT WHEN IT IS INSTANTIATED (I.E. AN OBJECT IS CREATED) MEMORY IS
ALLOCATED. EG: PEN, PENCIL, INKPOT ETC.
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
OBJECTS WILL BE TYRES, ENGINE, HEADLIGHT, MIRRORS, PAINT ETC.
Declaration of Class and Objects
Class Declerartion
Object Decleration
ClassName ObjectName;
Inheritance
WHEN ONE OBJECT ACQUIRES ALL THE PROPERTIES AND BEHAVIOURS
OF PARENT OBJECT I.E. KNOWN AS INHERITANCE. IT PROVIDES CODE
REUSABILITY. IT IS USED TO ACHIEVE RUNTIME POLYMORPHISM.
Polymorphism
! Logical not Reverse the result, returns !(x < 5 && x < 10)
false if the result is true
Bitwise Operators
BITWISE OPERATORS WORKS ON EACH BIT VALUE OF THE OPERANDS
AND GIVES THE RESULT.
Syntax of Function
Call by Reference :-
Inline Functions :-
Friend class and function
NOTE: THE PUBLIC DATA MEMBERS OF OBJECTS OF A CLASS CAN BE ACCESSED USING
THE DIRECT MEMBER ACCESS OPERATOR (.).
7. Memory Management and pointers
DYNAMIC MEMORY ALLOCATION IN C/C++ REFERS TO PERFORMING
MEMORY ALLOCATION MANUALLY BY PROGRAMMER.
ONE USE OF DYNAMICALLY ALLOCATED MEMORY IS TO ALLOCATE MEMORY
OF VARIABLE SIZE WHICH IS NOT POSSIBLE WITH COMPILER ALLOCATED
MEMORY EXCEPT VARIABLE LENGTH ARRAYS.
C++ HAS TWO OPERATORS NEW AND DELETE THAT PERFORM THE TASK OF
ALLOCATING AND FREEING THE MEMORY IN A BETTER AND EASIER WAY.
New Operator
THE NEW OPERATOR DENOTES A REQUEST FOR MEMORY ALLOCATION
ON THE HEAP. IF SUFFICIENT MEMORY IS AVAILABLE, NEW OPERATOR
INITIALIZES THE MEMORY AND RETURNS THE ADDRESS OF THE NEWLY
ALLOCATED AND INITIALIZED MEMORY TO THE POINTER VARIABLE.
SYNTAX:
EXAMPLE:
Delete Operator
SINCE IT IS PROGRAMMER’S RESPONSIBILITY TO DEALLOCATE
DYNAMICALLY ALLOCATED MEMORY, PROGRAMMERS ARE
PROVIDED DELETE OPERATOR BY C++ LANGUAGE.
SYNTAX:
EXAMPLE:
8. Constructors in C++ :-
What is constructor?
A constructor is a member function of a class which initializes objects of
a class. In C++, Constructor is automatically called when
object(instance of class) create. It is special member function of the
class.