Lecture Notes 02 (CSI2372 - Advanced Programming Concepts With C++)
Lecture Notes 02 (CSI2372 - Advanced Programming Concepts With C++)
Course Overview:
Advanced Programming Concepts with C++ (CSI2372) is designed to expand your programming skills and deepen your
understanding of C++ and its applications. Throughout the course, we will explore various advanced programming
concepts, ranging from differences between C++ and Java programming to numerical computation and interfacing with
hardware.
Teaching Assistant:
1. Bhattacharjee/Mayukh, email: [email protected]
2. Dai/Lansu, email: [email protected]
3. Emami Afshar/Bahar, email: [email protected]
4. Khare/Akshat, email: [email protected]
5. Kumar/Preyank, email: [email protected]
6. Yathirajulu/Ruchitha, email: [email protected]
7. Keswani /Yash, email: [email protected]
1
CSI2372 - Advanced Programming Concepts with C++
Course Outline:
2
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
3
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Origins of C++ Data Types
❑ The origins of C++ data types can be outlined back to the early development of the C programming
language. In 1973, Dennis Ritchie added improvements to the C language, including better arrays and
pointers, and added new data types to the language, such as char. For example, the original char type
was invented at Bell Labs in 1971 when Dennis Ritchie started extending the B
❑ C++ also supports a wide variety of data types, including primitive or Primitive (built-in) data
types, derived data types, and user-defined data types
❑ The primitive data types available in C++ are integer, character, boolean, floating point, double floating
point, valueless or void, and wide character
❑ C++ also fully supports object-oriented programming, including the four pillars (Read More☺) of object-
oriented development
4
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Introduction to C++ Data Types
5
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Evolution and Development of C++
❑ C++ data types have evolved over time, with new data types being added to the language and
changes in the use of certain data types with the following developed characteristics:
Primitive (Built-in) data types: C++ supports a rich assortment of built-in data types, with
several sizes and memory models
Derived data types: C++ also supports derived data types that can created by modifying
primitive types, offering advanced functionality..
6
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Fundamental Data Types in C++
Seven Primitive (Built-in) C++ data types: C++ has seven basic data types, including:
1- Character (char): Represents single characters, like letters or symbols, and is stored as a single byte.
char myChar = 'A'; // Replace 'A' with the character you want to store
2- Integer (int): Stores whole numbers (positive and negative) and is typically 4 bytes in size on most
systems.
int myInt = 42; // Replace 42 with the integer value you want to store
A byte is a collection of 8 bits, and each bit can represent a 0 or 1. Therefore, with 8 bits, there
are 2^8 = 256 possible combinations, which is enough to represent all the characters in the
American Standard Code for Information Interchange (ASCII) character set
7
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Fundamental Data Types in C++
Seven Primitive (Built-in) C++ data types: C++ has seven basic data types, including:
3- Boolean (bool): Represents true or false values, taking up a single byte of memory.
8
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Fundamental Data Types in C++
Seven Primitive (Built-in) C++ data types: C++ has seven basic data types, including:
4- Floating Point (float): Stores single-precision floating-point numbers, suitable for real numbers with limited
decimal precision. Can I store 42.123456789 in a float variable?
#include <iostream>
#include <cmath>
int main() {
float myFloat = 3.75;
int roundedValue = std::round(myFloat);
std::cout << "Original float: " << myFloat << std::endl;
std::cout << "Rounded value: " << roundedValue << std::endl;
return 0;}
9
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Fundamental Data Types in C++
Seven Primitive (Built-in) C++ data types: C++ has seven basic data types, including:
5. Double Floating Point (double): Stores double-precision floating-point numbers, offering greater decimal
precision compared to float. We also have (long double) for higher number of decimals.
#include <iostream>
#include <gmpxx.h>
int main() {
mpz_class myNumber;
mpf_set_default_prec(199 * 3.33); // Set precision for mpf_class (3.33 bits per
decimal place)
myNumber =
"12345.6789012345678901234567890123456789012345678901234567890";
std::cout << "myNumber: " << myNumber << std::endl;
return 0;}
10
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Fundamental Data Types in C++
#include <gmpxx.h>
11
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Fundamental Data Types in C++
Seven Primitive (Built-in) C++ data types: C++ has seven basic data types, including:
6. Valueless (void): Used to define functions that don't return values or to indicate a lack of data type.
• We have an int variable named number with the value 2. #include <iostream>
• We declare a void* (void pointer) named voidPointer and int main() {
assign it the address of number. This creates a generic int number = 2;
pointer that can point to variables of different types. void* voidPointer = &number;
// Use a type cast to access the value pointed to by the void
• We use type casting (static_cast) to convert the void* to pointer
an int* (integer pointer) so that we can access the value int* intPointer = static_cast<int*>(voidPointer);
pointed to by the voidPointer. std::cout << "Value of number: " << *intPointer <<
std::endl;
• We print out the value of number through the intPointer. return 0;}
12
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Fundamental Data Types in C++
Seven Primitive (Built-in) C++ data types: C++ has seven basic data types, including:
7. Wide Character (wchar_t): Represents wide characters, used for internationalization and dealing with a
broader range of characters, and is typically 2 or 4 bytes in size.
#include <iostream>
int main() {
// Define a wide character
wchar_t wideCharacter = L'世'; // '世' is a Chinese
character
// Print the wide character
std::wcout << "Wide character: " << wideCharacter <<
std::endl;
return 0;}
13
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Fundamental Data Types in C++
14
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Derived Data Types
Derived data types in C++ are data types that are created by combining primitive (built-in) data types. There are
four different types of derived data types in C++: arrays, functions, pointers, and references
1. Arrays are collections of items stored at contiguous memory locations
2. Functions are blocks of code or program segments that are defined to perform a specific well-defined task
3. Pointers are variables that store the memory addresses of other variables,
4. References are assigned name for other variables
These derived data types are used to create more complex data structures and to enable more advanced
programming techniques, such as dynamic memory allocation and passing functions as arguments to other
functions.
15
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Derived Data Types
Arrays Functions
• int num[5] = {1, 2, 3, 4, 5}; int Add-function(int A, int B) {
• double grades[100]; return A + B;}
• char vowelsounds[] = {'a', 'e', 'i', 'o', int result = Add-function(3, 4);
'u’}; cout << result << endl;
• string names[3] = {“Mom", “Dad", ---------------------------------
“Jay"};
bool isEven-function(int num) {
return num % 2 == 0;}
bool result = isEven-function(5);
Cout << result << endl;
16
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Derived Data Types-Pointers
17
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Derived Data Types - Pointers
The calculate function is designed to perform a mathematical operation on two integer values, a and b, based on a pointer to a
function that defines the specific operation to be performed.
int (*operation)(int, int): This part of the function signature declares a parameter named operation, which is a pointer to a
function that takes two int parameters and returns an int. This pointer operation is used to specify the mathematical operation
(either add or subtract in this case) that will be performed on a and b.
int a and int b: These are the two integer values on which the specified operation will be performed.
❑ One advantage of using pointer functions, as demonstrated in the calculate function, is enhanced flexibility and extensibility in
your code. By passing a function pointer as an argument, you can dynamically select and execute different operations at
runtime. This approach allows you to build more generic and adaptable functions, making it easier to add new operations or
modify existing ones without having to change the core logic of the function. It promotes code reusability and modularity,
contributing to more maintainable and scalable software systems.
18
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Derived Data Types -References
.
• Direct Modification: References enable direct modification of original data, making it ideal for
functions that need to alter variables and preserve changes beyond the function scope.
• Cleaner Interfaces: Functions with reference parameters often result in cleaner and more
intuitive interfaces, eliminating the need for return values or pointers.
int x = 300;
int &ref = x; // 'ref' is a reference to an integer
int y = 100;
int *ptr = &y;
// 'ptr' is a pointer to an integer, and it's initialized with the address of ‘y’
// The asterisk symbol can be attached to the int or variable.
19
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Derived Data Types -References
.
• Null Safety: Unlike pointers, references can't be null, reducing the risk of null reference errors
and enhancing program stability.
20
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Derived Data Types -References
21
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Derived Data Types - References and pointers usage
Part A
Part B
User-defined data types in C++ are data types that are defined by the programmer. User-defined data types allow
programmers to create more complex data structures and to encapsulate data and functionality into a single unit
These data types can be based on primitive (built-in) data types, or they can be more complex data structures
created by the programmer. User-defined data types in C++ include:
➢ Class
➢ Structure
➢ Object ❑ The class data type is used exclusively with object-oriented programming in
C++ and is a building block of the language's object-oriented features.
➢ Typedef
➢ Enumeration
23
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• User-Defined Data Types
Class: A class is a blueprint for creating objects in C++, defining the structure and behavior of
those objects through member variables and member functions.
Structure: A structure is a user-defined data type that groups variables of different data
types together under a single name for convenient data organization.
Object: An object is an instance of a class, representing a specific entity with its own set of
attributes (data members) and behaviors (member functions).
24
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• User-Defined Data Types
Typedef: Typedef is a C++ keyword used to create aliases or alternative names for existing data types,
making code more readable and maintainable.
Enumeration: An enumeration (enum) is a user-defined data type used for defining a set of named
integer constants, making code more readable and expressive when working with related values.
25
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• User-Defined Data Types
26
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• User-Defined Data Types
In C++, both classes and structures are used to define user-defined data types that can encapsulate
data (member variables) and behavior (member functions). The primary difference between classes
and structures lies in their default access control and their historical use:
Default Access Control:
• Class: In a class, the default access control for member variables and member functions is private.
This means that members are not accessible from outside the class unless explicitly marked as
public.
Structure: In a structure, the default access control for member variables and member functions is
public. This means that members are accessible from outside the structure by default.
27
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• User-Defined Data Types
Historical Use:
• Historically, classes were introduced earlier in C++, and they were primarily used for Object-Oriented Programming (OOP)
concepts, encapsulating data and behavior.
• Structures were initially meant for plain data structures with minimal behavior.
❑ However, with the introduction of C++11 and later standards, the distinction between classes and structures has become
less significant. In modern C++, you can use either a class or a structure to define data types, and you can specify the access
control explicitly using access specifiers (public, private, and protected) regardless of whether you're using a class or a
structure.
28
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• User-Defined Data Types
29
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• User-Defined Data Types
30
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• User-Defined Data Types
31
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Enumerated Types (enum)
The benefits of using an enum in C++ for users include:
• Readability: Enums provide meaningful and self-explanatory names to numeric values, enhancing code
readability by making it clear what each value represents (e.g., days of the week).
• Maintenance: Enumerations simplify code maintenance by centralizing related constants, reducing the risk of
errors when adding or modifying values.
• Type Safety: Enums offer type safety, preventing accidental assignment of unrelated integer values to
variables, enhancing code correctness.
32
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Enumerated Types (enum)
The benefits of using an enum in C++ for users include:
• Compactness: Enums are memory-efficient, as they store values as integers while allowing developers to work
with descriptive names, improving code understanding.
• Switch Statements: Enums are commonly used with switch statements, enabling efficient and structured
branching based on enum values, enhancing code organization
• Documentation: Enum names serve as documentation, reducing the need for comments to explain the
meaning of numeric constants in the code.
• Ease of Debugging: When debugging, enum names provide human-readable information, simplifying the
identification of issues related to specific values.
33
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
34
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• The Standard Template Library (STL) - How STL works
The STL is a collection of C++ template classes and functions that provide pre-built, reusable
components for common programming tasks.
It encompasses various components, including containers (e.g., vectors, lists), algorithms (e.g., sorting,
searching), iterators, and utility classes (e.g., pair, tuple).
The STL leverages the power of templates, allowing you to create generic algorithms and data
structures that work with different data types.
35
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• The Standard Template Library (STL) - How STL works
It uses iterators to abstractly traverse and manipulate elements within containers, providing a uniform
interface for various container types.
Functors (function objects) are used to encapsulate operations and can be passed to algorithms for
custom behavior.
STL components are highly efficient and well-tested, designed to work seamlessly with each other.
36
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• The Standard Template Library (STL) - Benefits
Reusability: STL components are pre-implemented and extensively tested, saving you time and effort in
coding common functionalities.
Efficiency: STL algorithms and data structures are optimized for performance, often outperforming
handcrafted solutions.
Productivity: The STL promotes clean, concise code by abstracting complex operations into simple
function calls.
Standardization: STL is a part of the C++ Standard Library, ensuring portability and compatibility across
different compilers and platforms.
37
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• The Standard Template Library (STL) - Benefits
Scalability: STL components can handle data of varying sizes, making them suitable for small-scale
projects to large applications.
Safety: STL's type safety and bounds checking help prevent common programming errors and enhance
code reliability.
Flexibility: The use of templates allows you to work with diverse data types while maintaining code
integrity.
Community and Resources: A vast community and extensive documentation and tutorials are available
to support learning and troubleshooting.
38
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• The Standard Template Library (STL) - Benefits
39
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Applications of C++ Data Types
int:
Application: Storing integers (whole numbers) such as counts, indices, and mathematical values.
double:
Application: Storing floating-point numbers (real numbers) with decimal precision, often used for
scientific calculations and measurements.
char:
Application: Storing single characters, such as letters, symbols, and digits.
bool:
Application: Storing boolean values (true or false) for conditional statements and logical operations.
string:
Application: Storing sequences of characters (text), used extensively for text processing and
manipulation.
portant.
40
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Applications of C++ Data Types
array:
Application: Storing fixed-size collections of elements of the same data type.
vector:
Application: Storing dynamic arrays with resizable capacity, commonly used for dynamic collections of
elements.
struct:
Application: Defining custom data structures to group related data fields under a single name, often
used in organizing data.
class:
Application: Creating user-defined types with data members and member functions, forming the basis
of object-oriented programming.
enum:
Application: Defining symbolic names for a set of related integer constants to improve code readability.
41
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Applications of C++ Data Types
enum:
Application: Defining symbolic names for a set of related integer constants to improve code readability.
pointer:
Application: Storing memory addresses of other variables or objects, used for dynamic memory
allocation, data manipulation, and advanced programming techniques.
reference:
Application: Creating aliases to existing variables, often used to pass variables to functions without
copying their values.
unsigned:
Application: Storing non-negative integers, useful when negative values are not needed.
long and long long:
Application: Storing larger integer values when greater precision or range is required.
42
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
• Session 2: C++ Data Types and Their Application
• Applications of C++ Data Types
float:
Application: Storing single-precision floating-point numbers for memory efficiency when decimal
precision is not critical.
wchar_t:
Application: Storing wide characters for extended character sets and internationalization.
short:
Application: Storing smaller integer values, used when memory conservation is a concern.
void:
Application: Representing an empty data type, often used for pointers and function return types when
the specific data type is not important.
43
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
44
Thank you
45
The four pillars of object-oriented development are Abstraction, Encapsulation, Inheritance,
and Polymorphism. These principles are software design principles that help developers write
clean and efficient object-oriented code.
1- Abstraction refers to the process of hiding complex implementation details and focusing on
essential features.
2- Encapsulation is the process of packaging data and functions that manipulate the data into a
single unit.
3- Inheritance allows a class to inherit properties and methods from another class.
4- Polymorphism allows objects of different classes to be treated as if they are of the same class,
enabling code reuse and flexibility.
These four pillars are essential to object-oriented programming and are used in many
programming languages, including C++.
46