1
UNIT 1-OOPS C++ BASICS
2
Review of C, Difference between C and C++,
C is a procedural programming language initially developed by Dennis Ritchie in the
year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system
programming language to write the UNIX operating system.
The main features of the C language include:
General Purpose and Portable
Low-level Memory Access
Fast Speed
Clean Syntax
These features make the C language suitable for system programming like an operating
system or compiler development.
Many later languages have borrowed syntax/features directly or indirectly from the C
language. Like syntax of Java, PHP, JavaScript, and many other languages are mainly based
on the C language.
3
Review of C, Difference between C and C++,
C C++
C was developed by Dennis
C++ was developed by Bjarne
Ritchie between the year 1969
Stroustrup in 1979.
and 1973 at 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.
C is (mostly) a subset of C++. C++ is (mostly) a superset of C.
Review of C, Difference between C and C++,
4
C C++
C++ is known as hybrid language because C++ supports
For the development of code, C supports procedural
both procedural and object oriented programming
programming.
paradigms.
Data and functions are separated in C because it is a Data and functions are encapsulated together in form of
procedural programming language. an object in C++.
Data is hidden by the Encapsulation to ensure that data
C does not support information hiding.
structures and operators are used as intended.
Built-in data types is supported in C. Built-in & user-defined data types is supported in C++.
C is a function driven language because C is a procedural C++ is an object driven language because it is an object
programming language. oriented programming.
5
Review of C, Difference between C and C++,
C C++
C is a function-driven language. C++ is an object-driven language
Functions in C are not defined inside Functions can be used inside a structure
structures. in C++.
Namespace features are not present Namespace is used by C++, which avoid
inside the C. name collisions.
Standard IO header is stdio.h. Standard IO header is iostream.h.
Reference variables are not supported Reference variables are supported by
by C. C++.
6
Review of C, Difference between C and C++,
C 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.
Instead of focusing on data, C focuses C++ focuses on data instead of focusing
on method or process. on method or procedure.
C provides malloc() and calloc()
C++ provides new operator for memory
functions for dynamic memory
allocation and delete operator for
allocation, and free() for memory de-
memory de-allocation.
allocation.
Direct support for exception handling is
Exception handling is supported by C++.
not supported by C.
7
Review of C, Difference between C and C++,
C C++
scanf() and printf() functions are used for
cin and cout are used for input/output in C++.
input/output in C.
C structures don’t have access modifiers. C ++ structures have access modifiers.
C follows the top-down approach C++ follows the Bottom-up approach
Strict type checking in done in C++. So many
There is no strict type checking in C programs that run well in C compiler will result
programming language. in many warnings and errors under C++
compiler.
C does not support overloading C++ does support overloading
8
Review of C, Difference between C and C++,
C C++
Type punning with unions is undefined
Type punning with unions is allows (C99 and
behavior (except in very specific
later)
circumstances)
Named initializers must match the data
Named initializers may appear out of order
layout of the struct
File extension is “.cpp” or “.c++” or “.cc” or
File extension is “.c”
“.cxx”
Meta-programming: templates (macros are
Meta-programming: macros + _Generic()
still supported but discouraged)
There are 32 keywords in the C There are 97 keywords in the C++
9
Procedure Oriented and Object Oriented Approach
Procedural Oriented
Object-Oriented Programming
Programming
In procedural programming, the In object-oriented programming, the
program is divided into small parts program is divided into small parts
called functions. called objects.
Procedural programming follows a Object-oriented programming
top-down approach. follows a bottom-up approach.
Object-oriented programming has
There is no access specifier in
access specifiers like private, public,
procedural programming.
protected, etc.
Adding new data and functions is Adding new data and function is
not easy. easy.
10
Procedure Oriented and Object Oriented Approach
Procedural Oriented Programming Object-Oriented Programming
In procedural programming, Overloading is possible in object-
overloading is not possible. oriented programming.
In procedural programming, In object-oriented programming,
there is no concept of data the concept of data hiding and
hiding and inheritance. inheritance is used.
In procedural programming, the In object-oriented programming,
function is more important than data is more important than
the data. function.
Procedural programming is based Object-oriented programming is
on the unreal world. based on the real world.
11
Procedure Oriented and Object Oriented Approach
Procedural Oriented Programming Object-Oriented Programming
Object-oriented programming is
Procedural programming is used for
used for designing large and
designing medium-sized programs.
complex programs.
Procedural programming uses the Object-oriented programming uses
concept of procedure abstraction. the concept of data abstraction.
Code reusability absent in Code reusability present in object-
procedural programming, oriented programming.
Examples: C, FORTRAN, Pascal, Examples: C++, Java, Python, C#,
Basic, etc. etc.
12
Basic Concepts: Objects, classes
Object-oriented programming – As the name suggests uses objects in
programming. Object-oriented programming aims to implement
real-world entities like inheritance, hiding, polymorphism, etc. in
programming. The main aim of OOP is to bind together the data and
the functions that operate on them so that no other part of the code
can access this data except that function.
13
Basic Concepts: Objects, classes
There are some basic concepts that act as the building blocks of
OOPs i.e.
Class
Objects
Encapsulation
Abstraction
Polymorphism
Inheritance
Dynamic Binding
Message Passing
14
Basic Concepts: Objects, classes
Characteristics of an Object-Oriented Programming Language
15
Basic Concepts: Objects, classes
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. For Example: Consider the Class of Cars.
There may be many cars with different names and brands but all of
them will share some common properties like all of them will have 4
wheels, Speed Limit, Mileage range, etc. So here, the Car is the class,
and wheels, speed limits, and mileage are their properties.
16
Basic Concepts: Objects, classes
Object :
An Object is an identifiable entity with some characteristics and
behavior. 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. Objects take up space in
memory and have an associated address like a record in pascal or
structure or union. When a program is executed the objects interact
by sending messages to one another. Each object contains data
and code to manipulate the data.
17
Basic Concepts: Objects, classes
Object :
An Object is an identifiable entity with some characteristics and
behavior. 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. Objects take up space in
memory and have an associated address like a record in pascal or
structure or union. When a program is executed the objects interact
by sending messages to one another. Each object contains data
and code to manipulate the data.
18
Abstraction
Data abstraction is one of the most essential and important
features of object-oriented programming in C++. Abstraction
means displaying only essential information and hiding the details.
Data abstraction refers to providing only essential information
about the data to the outside world, hiding the background
details or implementation.
Consider a real-life example of a man driving a car. The man only
knows that pressing the accelerator will increase the speed of the
car or applying brakes will stop the car but he does not know how
on pressing the accelerator the speed is actually increasing, he
does not know about the inner mechanism of the car or the
implementation of the accelerator, brakes, etc in the car.
19
Encapsulation
Encapsulation in C++ is defined as the wrapping up of data and
information in a single unit. In Object Oriented Programming,
Encapsulation is defined as binding together the data and the
functions that manipulate them.
Consider a real-life example of encapsulation, in a company,
there are different sections like the accounts section, finance
section, sales section, etc. Now,
The finance section handles all the financial transactions and
keeps records of all the data related to finance.
Similarly, the sales section handles all the sales-related activities
and keeps records of all the sales.
20
Inheritance
The capability of a class to derive properties and characteristics
from another class is called Inheritance. Inheritance is one of the
most important features of Object-Oriented Programming.
Inheritance is a feature or a process in which, new classes are
created from the existing classes. The new class created is called
“derived class” or “child class” and the existing class is known as
the “base class” or “parent class”. The derived class now is said to
be inherited from the base class.
When we say derived class inherits the base class, it means, the
derived class inherits all the properties of the base class, without
changing the properties of base class and may add new features
to its own. These new features in the derived class will not affect
the base class. The derived class is the specialized class for the
base class.
21
Polymorphism
The word “polymorphism” means having many forms. In simple
words, we can define polymorphism as the ability of a message to
be displayed in more than one form. A real-life example of
polymorphism is a person who at the same time can have different
characteristics. A man at the same time is a father, a husband, and
an employee. So the same person exhibits different behavior in
different situations. This is called polymorphism. Polymorphism is
considered one of the important features of Object-Oriented
Programming.
22
Dynamic Binding
Dynamic binding in C++ is a practice of connecting the function
calls with the function definitions by avoiding the issues with static
binding, which occurred at build time. Because dynamic binding
is flexible, it avoids the drawbacks of static binding, which
connected the function call and definition at build time.
In simple terms, Dynamic binding is the connection between the
function declaration and the function call.
23
Message Passing
Message passing in C++ is the process by which objects
communicate by exchanging messages, typically in the form of
method or function calls. This mechanism enables objects to interact
and collaborate, facilitating the accomplishment of desired
objectives.
24
Characteristics of Object-Oriented Languages: Abstract data types
Abstract or User-Defined Data Types: Abstract or User-Defined
data types are defined by the user itself. Like, defining a class in
C++ or a structure. C++ provides the following user-defined
datatypes:
Class
Structure
Union
Enumeration
Typedef defined Datatype
25
Characteristics of Object-Oriented Languages: Abstract data types
•Integer: The keyword used for integer data types is int. Integers typically
require 4 bytes of memory space and range from -2147483648 to
2147483647.
•Character: Character data type is used for storing characters. The keyword
used for the character data type is char. Characters typically require 1 byte of
memory space and range from -128 to 127 or 0 to 255.
•Boolean: Boolean data type is used for storing Boolean or logical values. A
Boolean variable can store either true or false. The keyword used for the
Boolean data type is bool.
•Floating Point: Floating Point data type is used for storing single-precision
floating-point values or decimal values. The keyword used for the floating-
point data type is float. Float variables typically require 4 bytes of memory
space.
26
Characteristics of Object-Oriented Languages: Abstract data types
•Double Floating Point: Double Floating Point data type is used for
storing
double-precision floating-point values or decimal values.
The keyword used for the double floating-point data type is
double. Double variables typically require 8 bytes of memory space.
•void: Void means without any value. void data type represents a valueless
entity. A void data type is used for those function which does not return a
value.
•Wide Character: Wide character data type is also a character data type
•but this data type has a size greater than the normal 8-bit data type.
•Represented by wchar_t. It is generally 2 or 4 bytes long.
•sizeof() operator: sizeof() operator is used to find the number of
•bytes occupied by a variable/data type in computer memory.
27
Object & classes
A Class is a user-defined data type that has data members and
member functions.
Data members are the data variables and member functions are
the functions used to manipulate these variables together, these
data members and member functions define the properties and
behavior of the objects in a Class.
In the above example of class Car, the data member will be
speed limit, mileage, etc, and member functions can be applying
brakes, increasing speed, etc.
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.
28
attributes, methods
Attributes are one of the key features of modern C++ which allows
the programmer to specify additional information to the compiler to
enforce constraints(conditions), optimise certain pieces of code or
do some specific code generation. In simple terms, an attribute acts
as an annotation or a note to the compiler which provides additional
information about the code for optimization purposes and enforcing
certain conditions on it. Introduced in C++11, they have remained
one of the best features of C++ and are constantly being evolved
with each new version of C++. Syntax:
29
attributes, methods
Purpose of Attributes in C++
1. To enforce constraints on the code: Here constraint refers to a
condition, that the arguments of a particular function must meet
for its execution (precondition). In previous versions of C++, the
code for specifying constraints was written in this manner
2. To give additional information to the compiler for optimisation
purposes: Compilers are very good at optimization but compared
to humans they still lag at some places and propose generalized
code which is not very efficient. This mainly happens due to the
lack of additional information about the “problem” which humans
have. To reduce this problem to an extent C++ standard has
introduced some new attributes that allow specifying a little more
to the compiler rather than the code statement itself. Once such
example is that of likely.
30
attributes, methods
3. Suppressing certain warnings and errors that programmer intended
to have in his code: It happens rarely but sometimes the programmer
intentionally tries to write a faulty code which gets detected by the
compiler and is reported as an error or a warning. One such example
is that of an unused variable which has been left in that state for a
specific reason or of a switch statement where the break statements
are not put after some cases to give rise to fall-through conditions. In
order to circumvent errors and warnings on such conditions, C++
provides attributes such as [maybe_unused] and [fallthrough] that
prevent the compiler from generating warnings or errors.