C++ Programming
VI SEMESTER
Department of Electronics and Communication Engineering, BVCOE, New Delhi
Subject:: C++ Programming
1
�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++ |
�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
�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 |
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
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
UNIT 3
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
7
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
Review of C, Difference between C and C++,
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
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 |
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. |
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++. |
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++. |
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. | cin and cout 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 |
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++ |
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. |
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. |
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. |
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
Basic Concepts: Objects, classes
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
20
Basic Concepts: Objects, classes
Characteristics of an Object-Oriented Programming Language
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
21
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
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
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
Abstraction�
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
25
Encapsulation�
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
26
Inheritance
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
27
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
Dynamic Binding
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
29
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
Characteristics of Object-Oriented Languages: Abstract data types
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
31
Characteristics of Object-Oriented Languages: Abstract data types
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
32
Characteristics of Object-Oriented Languages: Abstract data types
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
33
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.
Object & classes
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
34
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
attributes, methods
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
36
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
attributes, methods
Department of Electronics and Communication Engineering, BVCOE, New Delhi Subject:: C++ Programming
38
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
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