1 of 40

C++ Programming

VI SEMESTER

Department of Electronics and Communication Engineering, BVCOE, New Delhi

Subject:: C++ Programming

1

2 of 40

�C++ Programming�Course Objectives

Department of Electronics and Communication Engineering, BVCOE, New Delhi

Subject:: C++ Programming

2

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++

3 of 40

�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 Electronics and Communication Engineering, BVCOE, New Delhi

Subject:: C++ Programming

3

4 of 40

�C++ Programming CO-PO Mapping

Department of Electronics and Communication Engineering, BVCOE, New Delhi

Subject:: C++ Programming

4

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

5 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

5

6 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

6

7 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

7

8 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

8

9 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

9

10 of 40

Review of C, Difference between C and C++,

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

10

C

C++

C was developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Labs.

C++ was developed by Bjarne Stroustrup in 1979.

C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming.

C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language.

C is (mostly) a subset of C++.

C++ is (mostly) a superset of C.

Number of keywords in C:�* C90: 32�* C99: 37�* C11: 44�* C23: 59

Number of keywords in C++:�* C++98: 63�* C++11: 73�* C++17: 73�* C++20: 81

11 of 40

Review of C, Difference between C and C++,

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

11

C

C++

For the development of code, C supports procedural programming.

C++ is known as hybrid language because C++ supports both procedural and object oriented programming paradigms.

Data and functions are separated in C because it is a procedural programming language.

Data and functions are encapsulated together in form of an object in C++.

C does not support information hiding.

Data is hidden by the Encapsulation to ensure that data 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 programming language.

C++ is an object driven language because it is an object oriented programming.

12 of 40

Review of C, Difference between C and C++,

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

12

C

C++

C is a function-driven language.

C++ is an object-driven language

Functions in C are not defined inside structures.

Functions can be used inside a structure in C++.

Namespace features are not present inside the C.

Namespace is used by C++, which avoid 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++.

13 of 40

Review of C, Difference between C and C++,

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

13

C

C++

Virtual and friend functions are not supported by C.

Virtual and friend functions are supported by C++.

C does not support inheritance.

C++ supports inheritance.

Instead of focusing on data, C focuses on method or process.

C++ focuses on data instead of focusing on method or procedure.

C provides malloc() and calloc() functions for dynamic memory allocation, and free() for memory de-allocation.

C++ provides new operator for memory allocation and delete operator for memory de-allocation.

Direct support for exception handling is not supported by C.

Exception handling is supported by C++.

14 of 40

Review of C, Difference between C and C++,

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

14

C

C++

scanf() and printf() functions are used for 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

There is no strict type checking in C programming language.

Strict type checking in done in C++.  So many programs that run well in C compiler will result in many warnings and errors under C++ compiler.

C does not support overloading

C++ does support overloading

15 of 40

Review of C, Difference between C and C++,

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

15

C

C++

Type punning with unions is allows (C99 and later)

Type punning with unions is undefined behavior (except in very specific circumstances)

Named initializers may appear out of order

Named initializers must match the data layout of the struct

File extension is “.c”

File extension is “.cpp” or “.c++” or “.cc” or “.cxx”

Meta-programming: macros + _Generic()

Meta-programming: templates (macros are still supported but discouraged)

There are 32 keywords in the C

There are 97 keywords in the C++

16 of 40

Procedure Oriented and Object Oriented Approach

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

16

Procedural Oriented Programming

Object-Oriented Programming

In procedural programming, the program is divided into small parts called functions.

In object-oriented programming, the program is divided into small parts called objects.

Procedural programming follows a top-down approach.

Object-oriented programming follows a bottom-up approach.

There is no access specifier in procedural programming.

Object-oriented programming has access specifiers like private, public, protected, etc.

Adding new data and functions is not easy.

Adding new data and function is easy.

17 of 40

Procedure Oriented and Object Oriented Approach

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

17

Procedural Oriented Programming

Object-Oriented Programming

In procedural programming, overloading is not possible.

Overloading is possible in object-oriented programming.

In procedural programming, there is no concept of data hiding and inheritance.

In object-oriented programming, the concept of data hiding and inheritance is used.

In procedural programming, the function is more important than the data.

In object-oriented programming, data is more important than function.

Procedural programming is based on the unreal world.

Object-oriented programming is based on the real world.

18 of 40

Procedure Oriented and Object Oriented Approach

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

18

Procedural Oriented Programming

Object-Oriented Programming

Procedural programming is used for designing medium-sized programs.

Object-oriented programming is used for designing large and complex programs.

Procedural programming uses the concept of procedure abstraction.

Object-oriented programming uses the concept of data abstraction.

Code reusability absent in procedural programming,

Code reusability present in object-oriented programming.

Examples: C, FORTRAN, Pascal, Basic, etc.

Examples: C++, Java, Python, C#, etc.

19 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

19

20 of 40

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

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

20

21 of 40

Basic Concepts: Objects, classes

Characteristics of an Object-Oriented Programming Language

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

21

22 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

22

23 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

23

24 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

24

25 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

25

26 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

26

27 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

27

28 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

28

29 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

29

30 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

30

31 of 40

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

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

31

32 of 40

Characteristics of Object-Oriented Languages: Abstract data types

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

32

  • 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. 

33 of 40

Characteristics of Object-Oriented Languages: Abstract data types

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

33

  • 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.

34 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

34

35 of 40

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:

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

35

36 of 40

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. 

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

36

37 of 40

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. 

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

37

38 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

38

39 of 40

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.

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

39

40 of 40

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. 

Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming

40