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

Lecture Notes 01 (CSI2372 - Advanced Programming Concepts With C++)

C++ notes at the University of Ottawa

Uploaded by

Yosri Ketata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture Notes 01 (CSI2372 - Advanced Programming Concepts With C++)

C++ notes at the University of Ottawa

Uploaded by

Yosri Ketata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CSI2372 - Advanced Programming Concepts with C++

Instructor: Shahram Moradi


Contact: [email protected]
Office Hours: Tuesday 9:30 to 10:30 AM
Office: Room 344 ARC Building, 25 Templeton St., Ottawa, ON, Canada

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:

Throughout this course, we will explore the following topics:


Module 1: Foundations of C++ Programming
• Session 1: Introduction and Contrasting C++ with Java Programming
• Session 2: C++ Data Types and Their Applications
• Session 3: Pointers, Memory Management, and Efficiency
❑ Midterm 1: Assessment covering Sessions 1-3
Module 2: Object-Oriented Paradigm and File Handling
• Session 4: Embracing Object-Oriented Programming in C++
• Session 5: Navigating File and Stream Input/Output in C++
• Session 6: Preprocessor Macros and Their Role
❑ Midterm 2: Assessment covering Sessions 4-6
Module 3: Templates, Computation, and Practical Applications
• Session 7: Harnessing Templates and the Standard Template Library
• Session 8: Numerical Computation Techniques using C++
• Session 9: Interface Integration with Hardware Components
• Session 10: Exploring Engineering Applications
❑ Final Exam: Comprehensive Assessment of All Modules

2
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming

• Session 1: Introduction and Contrasting C++ with Java Programming


• History and Origins
• Overview of C++ and Java
• Compilation platform
• Programming Paradigms
• Syntax Comparison
• Memory Management
• Performance and Execution
• Platform Independence
• Standard Libraries
• Usage Scenarios
• Community and Ecosystem
• Conclusion: Pros and Cons

3
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming

• Session 1: Introduction and Contrasting C++ with Java Programming


• History and Origins

Matters of choices for a user :


C++: Java:
❑ Historical context
• Extension of C programming language • Emerged in mid-1990s
❑ Creators' motivations
• Developed in early 1980s • James Gosling and Sun Microsystems
❑ Challenges faced
• Bjarne Stroustrup at Bell Labs • Aimed at consumer electronics applications
❑ Object-oriented concepts
• Object-oriented programming concepts • Portability across diverse devices
❑ Efficiency and portability
• Retaining C's efficiency and portability • Java Virtual Machine (JVM)
❑ Low-level vs. high-level
• Incremental improvement over C • Platform independence without recompilation
❑ Systems vs. consumer electronics
• Low-level memory manipulation • Robustness and simplicity
❑ Portability mechanisms
• High-level abstractions • Enterprise software development
❑ Platform independence
• Systems programming • Web applications
❑ Robustness and simplicity
• Performance-critical applications
❑ Enterprise and web applications

4
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming

• Session 1: Introduction and Contrasting C++ with Java Programming


• Overview of C++ and Java

C++ main strengths?


Java main strengths?
➢ Powerful and versatile
➢ Platform independence
➢ Low-level memory manipulation
➢ "Write once, run anywhere"
➢ High level of abstraction
➢ Java Virtual Machine (JVM)
➢ Procedural and object-oriented
➢ Robustness and reliability
➢ Flexibility in programming paradigms
➢ Object-oriented nature
➢ Performance optimization emphasis
➢ Automatic memory management (garbage collection)
➢ Pointers and manual memory management
➢ Extensive standard library
➢ Resource-intensive applications
➢ Web development
➢ Game development
➢ Enterprise applications
➢ System programming
➢ Mobile app development
➢ Embedded systems

5
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming

• Session 1: Introduction and Contrasting C++ with Java Programming


• Compilation platform

C++ Compilation: Platform Dependency:

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

• Session 1: Introduction and Contrasting C++ with Java Programming


• Programming Paradigms

C++ Programming Paradigms: Java Programming Paradigms:

1. Procedural Programming 1. Object-Oriented Programming (OOP)


2. Object-Oriented Programming (OOP) 2. Platform Independence ("write once, run anywhere")
3. Generic Programming (Templates) 3. Automatic Memory Management (Garbage collection)
4. Limited Functional Programming (Lambda expressions, higher-order functions)

❖ 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

• Session 1: Introduction and Contrasting C++ with Java Programming


• Syntax Comparison

C++ Syntax: Java Syntax:

1. Pointers for manual memory management 1. No pointers for enhanced security


2. Header files for modularity 2. Packages for namespace management
3. Dynamic memory allocation with new and delete 3. Automatic memory management through garbage collection
4. Function overloading for multiple functions with same name but different 4. Method overloading for multiple methods with same name but
parameters different parameters

❖ 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

• Session 1: Introduction and Contrasting C++ with Java Programming


• Memory Management

C++ Memory Management: Java Memory Management:

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

• Session 1: Introduction and Contrasting C++ with Java Programming


• Performance and Execution

C++ Advantages: C++ vs. Java for Performance:

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.

Java Limitations: ❑ Use Cases:

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

• Session 1: Introduction and Contrasting C++ with Java Programming


• Platform Independence

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

• Session 1: Introduction and Contrasting C++ with Java Programming


• Platform Independence

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

• Session 1: Introduction and Contrasting C++ with Java Programming


• Standard Libraries

C++ Standard Libraries:


C++ offers a diverse set of standard libraries that cater to different needs:
❑ Standard Template Library (STL): Provides data structures (like vectors, maps) and algorithms (sorting, searching).
❑ I/O and File Handling: Supports input/output operations and file manipulation.
❑ Numerical Computing: Offers mathematical functions and libraries for numerical analysis.
❑ Concurrency: Provides tools for multithreaded and concurrent programming.

Java Standard Libraries:


Java's libraries promote consistency and facilitate development across various domains:
❑ Java Standard Library: Offers collections, I/O, networking, and utility classes.
❑ Java API Documentation: Comprehensive documentation for easy reference and learning.
❑ GUI Components: Libraries for creating graphical user interfaces.
❑ Security and Cryptography: Tools for secure coding and data encryption.

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

• Session 1: Introduction and Contrasting C++ with Java Programming


• Usage Scenarios

❖ Understanding the contexts in which each language shines allows us to harness their strengths effectively.

C++ Usage Scenarios:


C++ finds its strengths in scenarios requiring:
❑ Performance-Critical Applications: System-level programming, game development, real-time simulations.
❑ Resource-Intensive Tasks: Applications with heavy computational requirements.
❑ Low-Level Hardware Interaction: Embedded systems, device drivers.
❑ Optimized Libraries: Implementing efficient data structures and algorithms.

Java Usage Scenarios:


Java's versatility makes it a go-to choice for:
❑ Web and Enterprise Applications: Web development, server-side applications.
❑ Platform Independence: Applications that need to run consistently on different platforms.
❑ GUI Applications: Building user-friendly graphical interfaces.
❑ Mobile App Development: Android app development using Android Studio.

14
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming

• Session 1: Introduction and Contrasting C++ with Java Programming


• Community and Ecosystem

❖ Understanding the resources available within these communities helps in accessing support, libraries, and tools to enhance your programming journey.

C++ Community and Ecosystem:


C++ boasts a vibrant community and ecosystem with:
❑ Rich Documentation: Extensive online resources, tutorials, and forums.
❑ Open Source Projects: A plethora of open-source libraries and frameworks.
❑ Performance Tools: Profilers, debuggers, and optimization tools.
❑ Game Development Community: Thriving community in the gaming industry.

Java Community and Ecosystem:


Java's ecosystem is equally robust and offers:
❑ Java Community Process (JCP): An organization that oversees Java's development.
❑ Java Development Kit (JDK): Tools, documentation, and libraries for Java development.
❑ Java Virtual Machine (JVM): Enabling compatibility across platforms.
❑ Enterprise Integration: Java's strong presence in enterprise software development.

15
CSI2372 - Advanced Programming Concepts with C++
Module 1: Foundations of C++ Programming

• Session 1: Introduction and Contrasting C++ with Java Programming


• Conclusion: Pros and Cons

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

You might also like