0% found this document useful (0 votes)
2 views

c++ programming complete

The document outlines the C++ Programming course for the VI semester at BVCOE, New Delhi, detailing course objectives and outcomes, including understanding programming constructs, data manipulation, and object-oriented approaches. It also covers the differences between C and C++, emphasizing C++ features such as polymorphism, encapsulation, and inheritance. Additionally, the document explains fundamental concepts of object-oriented programming, including classes, objects, and data abstraction.

Uploaded by

soyaon265
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c++ programming complete

The document outlines the C++ Programming course for the VI semester at BVCOE, New Delhi, detailing course objectives and outcomes, including understanding programming constructs, data manipulation, and object-oriented approaches. It also covers the differences between C and C++, emphasizing C++ features such as polymorphism, encapsulation, and inheritance. Additionally, the document explains fundamental concepts of object-oriented programming, including classes, objects, and data abstraction.

Uploaded by

soyaon265
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

C++ Programming

VI SEMESTER

1 Department of Electrical and Electronics Engineering, BVCOE, New Delhi


Subject:C++ Programming, Instructor: Dr. Sandeep Sharma
C++ Programming
Course Objectives
Understand and use the basic programming constructs of
C/C++
Manipulate various C/C++ datatypes, such as arrays, strings,
and pointers
Isolate and fix common errors in C++ programs

Apply object-oriented approaches to software problems in


C++
Department of Electrical and Electronics Engineering, BVCOE, New Delhi
2 Subject: C++ Programming, Instructor: Dr. Sandeep Sharma
C++ Programming
Course Outcomes (CO)

CO 1 Understand tokens, expressions, and control structures


CO 2 Explain arrays and strings and create programs using them
CO 3 Describe and use constructors and destructors
CO 4 Understand and employ file management
.

Department of Electrical and Electronics Engineering, BVCOE, New Delhi


3 Subject: C++ Programming, Instructor: Dr. Sandeep Sharma
C++ Programming CO-PO Mapping
Course Outcomes (CO to Programme Outcomes (PO) Mapping (scale 1: low, 2: Medium, 3:
High

CO/PO PO01 PO02 PO03 PO04 PO05 PO06 PO07 PO08 PO09 PO10 PO11 PO12

CO1
3 - - - 2 - 2 - - 3 2
CO2
- 2 - - - 2 - - 3 - -
CO3
- - - 2 3 - - 3 - - 2 -
CO4
3 - 3 - - 3 3 - 3 - - 3
Department of Electrical and Electronics Engineering, BVCOE, New Delhi
4 Subject: C++ Programming , Instructor: Dr. Sandeep Sharma
UNIT 1
Review of C, Difference between C and C++, Procedure Oriented
and Object Oriented Approach. Basic Concepts: Objects, classes,
Principals like Abstraction, Encapsulation, Inheritance and
Polymorphism. Dynamic Binding, Message Passing. Characteristics of
Object-Oriented Languages. Abstract data types, Object & classes,
attributes, methods, C++ class declaration, Local Class and Global
Class, State identity and behaviour of an object, Local Object and
Global Object, Scope resolution operator, Friend Functions, Inline
functions, Constructors and destructors, instantiation of objects,
Types of Constructors, Static Class Data, Array of Objects, Constant
member functions and Objects, Memory management Operators.

5 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
UNIT 2
Inheritance, Types of Inheritance, access modes public, private
& protected, Abstract Classes, Ambiguity resolution using
scope resolution operator and Virtual base class, Aggregation,
composition vs classification hierarchies, Overriding
inheritance methods, Constructors in derived classes, Nesting
of Classes.

6 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
UNIT 3
 Polymorphism, Type of Polymorphism Compile time and
runtime, Function Overloading, Operator Overloading
(Unary and Binary) Polymorphism by parameter, Pointer to
objects, this pointer, Virtual Functions, pure virtual
functions.

7 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
UNIT 4
Manipulating strings, Streams and files handling, formatted and
Unformatted Input output. Exception handling, Generic
Programming function template, class Template Standard
Template Library: Standard Template Library, Overview of
Standard Template Library, Containers, Algorithms, Iterators,
Other STL Elements, The Container Classes, General Theory of
Operation,Vectors.

8 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.
9 Department of Electrical and Electronics Engineering, BVCOE New Delhi
Subject: C++ Programming, Instructor: SANDEEP SHARMA
Review of C, Difference between C and C++,
C C++
C was developed by Dennis Ritchie between C++ was developed by Bjarne Stroustrup in
the year 1969 and 1973 at AT&T Bell Labs. 1979.
C does no support polymorphism,
C++ supports polymorphism,
encapsulation, and inheritance which means
encapsulation, and inheritance because it is
that C does not support object oriented
an object oriented programming language.
programming.
C is (mostly) a subset of C++. C++ is (mostly) a superset of C.
Number of keywords in C: Number of keywords in C++:
* C90: 32 * C++98: 63
* C99: 37 * C++11: 73
* C11: 44 * C++17: 73
* C23: 59 * C++20: 81

10 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Review of C, Difference between C and C++,
C C++
C++ is known as hybrid language because
For the development of code, C supports
C++ supports both procedural and object
procedural programming.
oriented programming paradigms.
Data and functions are separated in C
Data and functions are encapsulated together
because it is a procedural programming
in form of an object in C++.
language.
Data is hidden by the Encapsulation to
C does not support information hiding. ensure that data structures and operators are
used as intended.
Built-in & user-defined data types is
Built-in data types is supported in C.
supported in C++.
C is a function driven language because C is C++ is an object driven language because it
a procedural programming language. is an object oriented programming.
11 Department of Electrical and Electronics Engineering, BVCOE New Delhi
Subject: C++ Programming, Instructor: SANDEEP SHARMA
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 in
structures. C++.

Namespace features are not present inside Namespace is used by C++, which avoid
the C. name collisions.

Standard IO header is stdio.h. Standard IO header is iostream.h.

Reference variables are not supported by C. Reference variables are supported by C++.

12 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Review of C, Difference between C and C++,
C C++

Virtual and friend functions are not Virtual and friend functions are supported
supported by C. by C++.

C does not support inheritance. C++ supports inheritance.

Instead of focusing on data, C focuses on C++ focuses on data instead of focusing on


method or process. method or procedure.

C provides malloc() and calloc() functions C++ provides new operator for memory
for dynamic memory allocation, and free() allocation and delete operator for memory
for memory de-allocation. de-allocation.
Direct support for exception handling is not
Exception handling is supported by C++.
supported by C.

13 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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
input/output in C. 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


There is no strict type checking in C many programs that run well in C compiler
programming language. will result in many warnings and errors
under C++ compiler.

C does not support overloading C++ does support overloading

14 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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++

15 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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,
procedural programming.
public, protected, etc.
Adding new data and functions is Adding new data and function is
not easy. easy.

16 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Procedure Oriented and Object
Oriented Approach
Procedural Oriented
Object-Oriented Programming
Programming
In procedural programming, Overloading is possible in object-
overloading is not possible. oriented programming.
In object-oriented programming, the
In procedural programming, there is no
concept of data hiding and inheritance is
concept of data hiding and inheritance.
used.
In procedural programming, the
In object-oriented programming, data is
function is more important than the
more important than function.
data.
Procedural programming is based on Object-oriented programming is based
the unreal world. on the real world.

17 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Procedure Oriented and Object
Oriented Approach
Procedural Oriented
Object-Oriented Programming
Programming
Object-oriented programming is used
Procedural programming is used for
for designing large and complex
designing medium-sized programs.
programs.
Procedural programming uses the Object-oriented programming uses the
concept of procedure abstraction. concept of data abstraction.
Code reusability absent in procedural Code reusability present in object-
programming, oriented programming.
Examples: C, FORTRAN, Pascal, Examples: C++, Java, Python, C#,
Basic, etc. etc.

18 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

19 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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

20 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Basic Concepts: Objects, classes
Characteristics of an Object-Oriented Programming
Language

21 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

22 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

23 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

24 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

25 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

26 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

27 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

28 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

29 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

30 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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

31 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

32 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

33 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

34 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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:

35 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

36 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
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.

37 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
attributes, methods
1. noreturn: indicates that the function does not return a value
2. deprecated: Indicates that the name or entity declared with
this attribute has become obsolete and must not be used for
some specific reason. This attribute can be applied to
namespaces, functions, classes structures or variables.
3. nodiscard: The entities declared with nodiscard should not
have their return values ignored by the caller. Simply saying if a
function returns a value and is marked nodiscard then the return
value must be utilized by the caller and not discarded.

38 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
attributes, methods
4. maybe_unused: Used to suppress warnings on any unused
entities (For eg: An unused variable or an unused argument to a
function).
5. fallthrough: [[fallthrough]] indicates that a fallthrough in a
switch statement is intentional. Missing a break or return in a switch
statement is usually considered a programmer’s error but in some
cases fallthrough can result in some very terse code and hence it is
used. Note: Unlike other attributes a fallthrough requires a
semicolon after it is declared
6.likely: For optimisation of certain statements that have more
probability to execute than others. Likely is now available in latest
version of GCC compiler for experimentation purposes.

39 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
attributes, methods
7. no_unique_address: Indicates that this data member need
not have an address distinct from all other non-static data
members of its class. This means that if the class consist of an
empty type then the compiler can perform empty base
optimisation on it.
8. expects: It specifies the conditions (in form of contract)
that the arguments must meet for a particular function to be
executed.

40 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types

Abstract Data type (ADT) is a type (or class) for objects whose
behavior is defined by a set of values and a set of operations.
The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented.
It does not specify how data will be organized in memory and
what algorithms will be used for implementing the operations.
It is called “abstract” because it gives an implementation-
independent view.

41 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types

42 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types

 The user of data type does not need to know how that data
type is implemented, for example, we have been using
Primitive values like int, float, char data types only with the
knowledge that these data type can operate and be
performed on without any idea of how they are
implemented.
 So a user only needs to know what a data type can do, but
not how it will be implemented. Think of ADT as a black box
which hides the inner structure and design of the data type.
Now we’ll define three ADTs namely List ADT, Stack ADT,
Queue ADT.

43 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types List

44 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types List
 The data is generally stored in key sequence in a list which has a head structure
consisting of count, pointers and address of compare function needed to compare the
data in the list.
 The data node contains the pointer to a data structure and a self-referential pointer
which points to the next node in the list.
 The List ADT Functions is given below:
 get() – Return an element from the list at any given position.
 insert() – Insert an element at any position of the list.
 remove() – Remove the first occurrence of any element from a non-empty list.
 removeAt() – Remove the element at a specified location from a non-empty
list.
 replace() – Replace an element at any position by another element.
 size() – Return the number of elements in the list.
 isEmpty() – Return true if the list is empty, otherwise return false.
 isFull() – Return true if the list is full, otherwise return false.

45 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types Stack

46 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types Stack
 In Stack ADT Implementation instead of data being stored in each node, the pointer to
data is stored.
 The program allocates memory for the data and address is passed to the stack ADT.
 The head node and the data nodes are encapsulated in the ADT. The calling function can
only see the pointer to the stack.
 The stack head structure also contains a pointer to top and count of number of entries
currently in stack.
 push() – Insert an element at one end of the stack called top.
 pop() – Remove and return the element at the top of the stack, if it is not empty.
 peek() – Return the element at the top of the stack without removing it, if the stack is
not empty.
 size() – Return the number of elements in the stack.
 isEmpty() – Return true if the stack is empty, otherwise return false.
 isFull() – Return true if the stack is full, otherwise return false.

47 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types Queue

48 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types Queue
 The queue abstract data type (ADT) follows the basic design of the stack
abstract data type.
 Each node contains a void pointer to the data and the link pointer to the
next element in the queue. The program’s responsibility is to allocate
memory for storing the data.
 enqueue() – Insert an element at the end of the queue.
 dequeue() – Remove and return the first element of the queue, if the
queue is not empty.
 peek() – Return the element of the queue without removing it, if the
queue is not empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise return false.
 isFull() – Return true if the queue is full, otherwise return false.

49 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types
Features
 Abstract data types (ADTs) are a way of encapsulating
data and operations on that data into a single unit. Some
of the key features of ADTs include:
 Abstraction: The user does not need to know the
implementation of the data structure only essentials are provided.
 Better Conceptualization: ADT gives us a better
conceptualization of the real world.
 Robust: The program is robust and has the ability to catch errors.
 Encapsulation: ADTs hide the internal details of the data and
provide a public interface for users to interact with the data. This
allows for easier maintenance and modification of the data
structure.

50 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types
Features
 Data Abstraction: ADTs provide a level of abstraction from the implementation
details of the data. Users only need to know the operations that can be performed on the
data, not how those operations are implemented.
 Data Structure Independence: ADTs can be implemented using different data
structures, such as arrays or linked lists, without affecting the functionality of the ADT.
 Information Hiding: ADTs can protect the integrity of the data by allowing access
only to authorized users and operations. This helps prevent errors and misuse of the data.
 Modularity: ADTs can be combined with other ADTs to form larger, more complex
data structures. This allows for greater flexibility and modularity in programming.
 Overall, ADTs provide a powerful tool for organizing and manipulating data in a
structured and efficient manner.
 Abstract data types (ADTs) have several advantages and disadvantages that should be
considered when deciding to use them in software development.

51 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types
Advantage
 Encapsulation: ADTs provide a way to encapsulate data and
operations into a single unit, making it easier to manage and modify the
data structure.
 Abstraction: ADTs allow users to work with data structures without
having to know the implementation details, which can simplify
programming and reduce errors.
 Data Structure Independence: ADTs can be implemented using
different data structures, which can make it easier to adapt to changing
needs and requirements.
 Information Hiding: ADTs can protect the integrity of data by
controlling access and preventing unauthorized modifications.
 Modularity: ADTs can be combined with other ADTs to form more
complex data structures, which can increase flexibility and modularity
in programming.

52 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Abstract Data Types
Disadvantage
 Overhead: Implementing ADTs can add overhead in terms of
memory and processing, which can affect performance.
 Complexity: ADTs can be complex to implement, especially for
large and complex data structures.
 Learning Curve: Using ADTs requires knowledge of their
implementation and usage, which can take time and effort to
learn.
 Limited Flexibility: Some ADTs may be limited in their
functionality or may not be suitable for all types of data structures.
 Cost: Implementing ADTs may require additional resources and
investment, which can increase the cost of development.

53 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Local Classes in C++

 A class declared inside a function becomes local to that


function and is called Local Class in C++.
 A local class name can only be used locally i.e., inside the
function and not outside it.
 The methods of a local class must be defined inside it only.
 A local class can have static functions but, not static data
members.

54 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Local Classes in C++

 // C++ program without any compilation error


 // to demonstrate a Local Class
 #include <iostream>
 using namespace std;

 // Creating the class


 void fun()
 {
 // local to fun
 class Test {
 // members of Test class
 };
 }

 // Driver Code
 int main() { return 0; }

55 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
 A class declared inside a function becomes local to that
function and is called Local Class in C++.
 A local class name can only be used locally i.e., inside the
function and not outside it.
 The methods of a local class must be defined inside it only.
 A local class can have static functions but, not static data
members.

56 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
State, Behavior & Identity of an object

 State:
 Definition: For any given moment, the state of an object is formed from its
characteristics.
Example: The state of ‘car’ can include properties like color, speed, fuel level and
present gear.
 Behavior:
 Definition: This is a terminology that refers to the actions or methods that are
performed by an object. They describe how the given object relates with others or its
environment.
 Example: Behaviors for cars can include accelerate, brake, changeGear, turnLeft.
 Identity:
 Definition: An object’s identity is what separates it from other objects. It helps
differentiate between multiple instances of the same class in the system.
 Example: Every car on the road has certain unique identity such as a license plate
number or Vehicle Identification Number (VIN).

57 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Scope resolution operator in C++

 To access a global variable when there is a local variable with same name:
// C++ program to show that we can access a global variable
// using scope resolution operator :: when there is a local
// variable with same name
#include<iostream>
using namespace std;

int x; // Global x

int main()
{
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}

58 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Scope resolution operator in C++

 To define a function outside a class.


// C++ program to show that scope resolution operator :: is
// used to define a function outside a class
#include <iostream>
using namespace std;

class A {
public:
// Only declaration
void fun();
};

// Definition outside class using ::


void A::fun() { cout << "fun() called"; }

int main()
{
A a;
a.fun();
return 0;
}

59 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Friend Class and Function in C++
 A friend class can access private and protected members of
other classes in which it is declared as a friend. It is
sometimes useful to allow a particular class to access private
and protected members of other classes. For example, a
LinkedList class may be allowed to access private members of
Node.
 We can declare a friend class in C++ by using the friend
keyword.

60 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Friend Class and Function in C++

61 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Friend Class and Function in C++
 Like a friend class, a friend function can be granted special
access to private and protected members of a class in C++.
They are the non-member functions that can access and
manipulate the private and protected members of the class
for they are declared as friends.
 A friend function can be:
 A global function
 A member function of another class

62 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Inline Functions in C++
C++ provides inline functions to reduce the function call
overhead. An inline function is a function that is expanded in
line when it is called. When the inline function is called whole
code of the inline function gets inserted or substituted at the
point of the inline function call. This substitution is performed
by the C++ compiler at compile time. An inline function may
increase efficiency if it is small.

63 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Inline Functions in C++

64 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Inline Functions Advantage
 Function call overhead doesn’t occur.
 It also saves the overhead of push/pop variables on the stack when
a function is called.
 It also saves the overhead of a return call from a function.
 When you inline a function, you may enable the compiler to
perform context-specific optimization on the body of the
function. Such optimizations are not possible for normal function
calls. Other optimizations can be obtained by considering the
flows of the calling context and the called context.
 An inline function may be useful (if it is small) for embedded
systems because inline can yield less code than the function called
preamble and return.

65 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Inline Functions Disadvantage

•If you use too many inline functions then the size of the binary executable
file will be large, because of the duplication of the same code.
•Too much inlining can also reduce your instruction cache hit rate, thus
reducing the speed of instruction fetch from that of cache memory to that
of primary memory.
•The inline function may increase compile time overhead if someone
changes the code inside the inline function then all the calling location has
to be recompiled because the compiler would be required to replace all
the code once again to reflect the changes, otherwise it will continue with
old functionality.

66 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Difference Between Constructor and
Destructor in C++
 A constructor is a member function of a class that has the
same name as the class name. It helps to initialize the object
of a class. It can either accept the arguments or not. It is used
to allocate the memory to an object of the class. It is called
whenever an instance of the class is created. It can be defined
manually with arguments or without arguments. There can
be many constructors in a class. It can be overloaded but it
can not be inherited or virtual. There is a concept of copy
constructor which is used to initialize an object from another
object.

67 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Difference Between Constructor and
Destructor in C++
 Like a constructor, Destructor is also a member function of a
class that has the same name as the class name preceded by a
tilde(~) operator. It helps to deallocate the memory of an
object. It is called while the object of the class is freed or
deleted. In a class, there is always a single destructor without
any parameters so it can’t be overloaded. It is always called in
the reverse order of the constructor. if a class is inherited by
another class and both the classes have a destructor then the
destructor of the child class is called first, followed by the
destructor of the parent or base class.

68 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Difference Between Constructor and
Destructor in C++
S. No. Constructor Destructor
Constructor helps to initialize the Whereas destructor is used to destroy
1.
object of a class. the instances.
It is declared as className(
Whereas it is declared as ~
2. arguments if any
className( no arguments ){ }.
){Constructor’s Body }.
Constructor can either accept
3. While it can’t have any arguments.
arguments or not.
A constructor is called when an
It is called while object of the class is
4. instance or object of a class is
freed or deleted.
created.
Constructor is used to allocate the While it is used to deallocate the
5.
memory to an instance or object. memory of an object of a class.
69 Department of Electrical and Electronics Engineering, BVCOE New Delhi
Subject: C++ Programming, Instructor: SANDEEP SHARMA
Difference Between Constructor and
Destructor in C++
S. No. Constructor Destructor

6. Constructor can be overloaded. While it can’t be overloaded.

Here, its name is also same as the


The constructor’s name is same as
7. class name preceded by the tiled (~)
the class name.
operator.
In a class, there can be multiple While in a class, there is always a
8.
constructors. single destructor.
There is a concept of copy
constructor which is used to While here, there is no copy
9.
initialize an object from another destructor concept.
object.
They are often called in successive They are often called in reverse order
10.
order. of constructor.
70 Department of Electrical and Electronics Engineering, BVCOE New Delhi
Subject: C++ Programming, Instructor: SANDEEP SHARMA
Different ways to instantiate an object
in C++

 In C++, there are different ways to instantiate an objects and one


of the method is using Constructors.These are special class
members which are called by the compiler every time an object of
that class is instantiated. There are three different ways of
instantiating an object through constructors:
 Through Default constructors.
 Through Parameterized constructors.
 Through Copy constructors.
 Through Default Constructor: An object can be instantiated
through a default constructor in either static way or dynamic way

71 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Different ways to instantiate an object
in C++
 Through Parameterized Constructor: Object
instantiation can be done through parameterized constructor
in static and dynamic way. It is possible to pass arguments to
parameterized constructors. Typically, these arguments help
initialize an object when it is created.
 3. Copy constructor: A copy constructor can be used to
instantiate an object statically or dynamically. Static or
dynamic initialization of object using a copy constructor has
the following general function prototype.

72 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Constructors in C++
 Constructor in C++ is a special method that is invoked automatically
at the time of object creation. It is used to initialize the data members of
new objects generally. The constructor in C++ has the same name as the
class or structure. It constructs the values i.e. provides data for the
object which is why it is known as a constructor.
 Constructor is a member function of a class, whose name is the same as
the class name.
 Constructor is a special type of member function that is used to initialize
the data members for an object of a class automatically when an object
of the same class is created.
 Constructor is invoked at the time of object creation. It constructs the
values i.e. provides data for the object that is why it is known as a
constructor.
 Constructors do not return value, hence they do not have a return type.

73 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Constructors in C++
 A constructor gets called automatically when we create the object
of the class.
 Constructors can be overloaded.
 A constructor can not be declared virtual.
 Types of Constructor in C++
 Constructors can be classified based on in which situations they
are being used. There are 4 types of constructors in C++:
 Default Constructor
 Parameterized Constructor
 Copy Constructor
 Move Constructor

74 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Constructors in C++

75 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Constructors in C++

 Default Constructor in C++


 A default constructor is a constructor that doesn’t take any
argument. It has no parameters. It is also called a zero-argument
constructor.
 Parameterized Constructor in C++
 Parameterized Constructors make it possible to pass arguments to
constructors. Typically, these arguments help initialize an object
when it is created. To create a parameterized constructor, simply
add parameters to it the way you would to any other function.
When you define the constructor’s body, use the parameters to
initialize the object

76 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Constructors in C++

 Copy Constructor in C++


 A copy constructor is a member function that initializes an object
using another object of the same class.
 Move Constructor in C++
 The move constructor is a recent addition to the family of
constructors in C++. It is like a copy constructor that constructs
the object from the already existing objects., but instead of
copying the object in the new memory, it makes use of move
semantics to transfer the ownership of the already created object
to the new object without creating extra copies.

77 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
C++ Static Data Members
 Static data members are class members that are declared using
static keywords. A static member has certain special
characteristics which are as follows:
 Only one copy of that member is created for the entire class and is
shared by all the objects of that class, no matter how many objects
are created.
 It is initialized before any object of this class is created, even
before the main starts.
 It is visible only within the class, but its lifetime is the entire
program
 .

78 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Array of Objects in C++ with Examples
 An array in C/C++ or be it in any programming language is
a collection of similar data items stored at contiguous
memory locations and elements can be accessed randomly
using indices of an array. They can be used to store the
collection of primitive data types such as int, float, double,
char, etc of any particular type. To add to it, an array in
C/C++ can store derived data types such as structures,
pointers, etc. Given below is the picture representation of an
array.

79 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Const member functions in C++
 Constant member functions are those functions that are denied
permission to change the values of the data members of their class.
To make a member function constant, the keyword const is
appended to the function prototype and also to the function
definition header.
 Like member functions and member function arguments, the
objects of a class can also be declared as const. An object declared
as const cannot be modified and hence, can invoke only const
member functions as these functions ensure not to modify the
object. A const object can be created by prefixing the const
keyword to the object declaration. Any attempt to change the data
member of const objects results in a compile-time error.

80 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA
Memory Management operator
 Dynamic memory allocation in C/C++ refers to performing memory
allocation manually by a programmer. Dynamically allocated memory is
allocated on Heap, and non-static and local variables get memory
allocated on Stack (Refer to Memory Layout C Programs for details).
 What are applications?
 One use of dynamically allocated memory is to allocate memory of
variable size, which is not possible with compiler allocated memory
except for variable-length arrays.
 The most important use is the flexibility provided to programmers. We
are free to allocate and deallocate memory whenever we need it and
whenever we don’t need it anymore. There are many cases where this
flexibility helps. Examples of such cases are Linked List, Tree, etc.

81 Department of Electrical and Electronics Engineering, BVCOE New Delhi


Subject: C++ Programming, Instructor: SANDEEP SHARMA

You might also like