Lecture Notes 01 (CSI2372 - Advanced Programming Concepts With C++)
Lecture Notes 01 (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
4
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
5
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
Source code to machine code C++: Compiled machine code tied to platform
Direct compilation to executable C++: Different binaries for different platforms
Preprocessing, compiling, assembling, linking C++: Hardware-specific optimizations
Platform-specific binaries Java: JVM interprets bytecode Summary (Read More☺):
Requires recompilation for different platforms Java: Same bytecode on all platforms
Java: Platform abstraction by JVM C++: Only compiled
Java: Compiled and interpreted
C++: Source code to machine code
Java Compilation: Platform Independence: Java: Source code to bytecode
C++: Platform-dependent
Source code to bytecode Java: Write once, run anywhere Java: Platform-independent
Compilation to intermediate code C++: Platform-specific compilation
Bytecode executed by JVM Java: JVM manages platform differences
Platform-independent bytecode C++: No intermediate bytecode
Same bytecode on different platforms Java: Bytecode portable across platforms
C++: Requires platform-specific executables
6
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
❖ Importance:
C++ primarily follows a multi-paradigm approach, supporting object-oriented, procedural, and generic programming. In contrast, Java predominantly adheres to the object-
oriented programming paradigm, emphasizing encapsulation, inheritance, and polymorphism. (Read More☺)
7
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
❖ Important:
➢ In C++, class members are declared with the "public," "private," or "protected" keywords, while in Java, all class members are private by default. In C++, neglecting to
deallocate memory associated with a pointer can lead to memory leaks, where resources aren't released. Additionally, attempting to access freed memory may cause
undefined behavior and potential program instability
➢ On the other hand, Java is generally considered more secure due to its strict memory management through garbage collection and strong type checking, which helps prevent
common programming errors and security vulnerabilities.
8
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
1. Manual memory management with pointers. 1. Automatic memory management through garbage collection.
2. Operators like new and delete for dynamic allocation and deallocation. 2. Objects no longer in use are automatically identified and reclaimed.
3. Provides more control but can lead to memory leaks and segmentation faults if 3. Simplifies memory-related errors, enhancing reliability.
mismanaged.
Example:
---------------------------------------------------------------------------------
Example: // Java code to create a new object and assign it to a variable
--------------------------------------------------------------------------------- MyObject obj = new MyObject();
// C++ code to create a new integer and assign it to a pointer
int* ptr = new int(42); // Java code to assign a new object to the same variable, releasing the old object
obj = new MyObject();
// C++ code to deallocate the memory used by the integer -----------------------------------------------------------------------------------
delete ptr; ❖ In this code, the new operator is used to allocate memory for a new MyObject
----------------------------------------------------------------------------------- instance, and the resulting reference is assigned to the obj variable. When a
❖ In this code, the new operator is used to allocate memory for a new integer, new object is assigned to the same variable, the previous object is
and the resulting pointer is assigned to the ptr variable. To deallocate this automatically released by the garbage collector.
memory, the delete operator is used when the integer is no longer needed.
9
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
1. Direct hardware access and manipulation. ✓ C++ excels in speed and efficiency, particularly for tasks needing low-level
2. Compiled to machine code for efficient execution. hardware control or high-performance computing.
3. Harnesses hardware-specific optimizations. ✓ C++ is compiled to machine code, while Java is interpreted with virtual machine
4. Utilizes processor instructions and system-level resources. overhead, potentially impacting speed.
1. Interpreted language with virtual machine layer. ✓ C++ outperforms Java in tasks requiring direct hardware interaction.
2. Overhead can affect performance. ✓ C++ preferred for graphics programming, scientific computing, and
3. Lacks direct hardware access and low-level optimizations. performance-intensive applications.
10
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
C++ doesn't inherently offer the same level of platform independence. Compiled Examples of executable files:
C++ code is platform-dependent, requiring recompilation for each target platform.
Here's a contrasting example: • Windows Executable (.exe): C++ code compiled on Windows generates ".exe"
executable files specific to the Windows OS. Not compatible with other platforms
Consider a C++ program that calculates the cube of a number:. without specialized tools or emulation.
• Linux Executable (No Extension or .out): C++ code on Linux compiles into executables
--------------------------------------------------------------------------------------- with no extension or ".out". Designed for Linux, not directly compatible with other
#include <iostream> platforms.
• macOS Executable (No Extension): Compiled C++ code on macOS creates executables
int main() { without extensions. Similar to Linux, tailored for macOS and may need adjustments
int number = 5; for other platforms.
int cube = number * number * number; • Cross-Platform Executable (Using Cross-Compilation): Developers can cross-compile
std::cout << "The cube of " << number << " is: " << cube << std::endl; to create executables for various platforms from a single environment. Compiled with
return 0; target platform specs, executable on respective platforms.
} • Shared Libraries (Dynamic Link Libraries): Shared libraries like ".dll" (Windows) or
--------------------------------------------------------------------------------------- ".so" (Linux) contain compiled code for linking. May require separate compilation for
To run this program on different platforms, you'd need to compile it separately for each platform due to linking and binary compatibility differences.
each platform, resulting in different executable files.
11
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
Java achieves platform independence through its "write once, run anywhere"
principle. Java code is compiled into bytecode, which can be executed on any
system with a compatible Java Virtual Machine (JVM). Here's a simple example:
Imagine you have a Java program that calculates the square of a number:
---------------------------------------------------------------------------------------
public class SquareCalculator {
public static void main(String[] args) {
int number = 5;
int square = number * number;
System.out.println("The square of " + number + " is: " + square);
}
}
---------------------------------------------------------------------------------------
You can compile and run this Java program on different platforms (Windows,
macOS, Linux) without modification. As long as a compatible JVM exists, the
bytecode runs consistently, showcasing Java's platform independence.
12
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
Understanding the richness of standard libraries assists in streamlining development tasks and leveraging pre-built solutions.
13
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
❖ Understanding the contexts in which each language shines allows us to harness their strengths effectively.
14
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
❖ Understanding the resources available within these communities helps in accessing support, libraries, and tools to enhance your programming journey.
15
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming
C++:
C++ is a powerful and versatile programming language known for its ability to perform low-level memory manipulation
while maintaining a high level of abstraction. It supports both procedural and object-oriented programming paradigms,
offering developers the flexibility to choose the most suitable approach for their projects. C++ emphasizes performance
optimization and provides features like pointers and manual memory management, making it an ideal choice for
resource-intensive applications such as game development, system programming, and embedded systems.
Java:
Java, on the other hand, is renowned for its platform independence and robustness. It operates on the "write once, run
anywhere" principle, thanks to its execution on the Java Virtual Machine (JVM). Java's object-oriented nature simplifies
complex programming tasks through features like automatic memory management (garbage collection). This
characteristic, along with its extensive standard library, makes Java an excellent candidate for web development,
initiative applications, and mobile app development.
16
Thank You
17
18
Password
19
CSI-Quiz01
20
Read More☺
Java predominantly adheres to the object-oriented programming (OOP) paradigm, placing a strong
emphasis on core OOP principles such as encapsulation, inheritance, and polymorphism. Encapsulation
ensures data privacy and controlled access through classes and access modifiers. Inheritance enables the
creation of hierarchical class structures, fostering code reuse and extension. Polymorphism allows objects
of different classes to be treated as instances of a common superclass, facilitating flexibility and
extensibility in Java applications.
21
Read More☺
Source-to-source compilation, unlike traditional compilation, translates high-level source code into
another high-level programming language. In Java, this is evident through the compilation to bytecode,
which is a higher-level representation than machine code. C++, however, compiles to lower-level machine
code directly. Source-to-source compilation in Java aids platform independence, while C++ directly targets
a specific platform."
22