Lecture 2 & 3 - Basic C++ Structure
Lecture 2 & 3 - Basic C++ Structure
Component Role
Executes instructions and processes data.
Central Processing Unit (CPU) Consists of the Arithmetic Logic Unit (ALU) and Control
Unit (CU).
Performs mathematical and logical operations (e.g.,
Arithmetic Logic Unit (ALU)
addition, subtraction, AND, OR).
Directs operations within the CPU, decodes instructions,
Control Unit (CU)
and coordinates data flow between components.
Memory Unit (RAM) Stores both instructions and data (as per the Stored
Program Concept).
Input Devices Allow users to enter data (e.g., keyboard, mouse).
Output Devices Display processed data (e.g., monitor, printer).
System Bus Transfers data and instructions between the CPU, memory,
and peripherals.
• The Stored Program Concept states that both instructions and data are
stored in the same memory and treated the same way by the CPU.
• Allows programs to be written, modified, and executed dynamically.
• For example, a C++ program can read input, modify its behavior, and store
results all in memory.
• The Von Neumann Bottleneck refers to the limited data transfer speed
between the CPU and memory.
• Since data and instructions share the same bus, the CPU often has to wait for
memory access, causing slowdowns.
• Registers are small, high-speed storage locations inside the CPU that
temporarily hold data and instructions during processing.
• Registers help reduce the Von Neumann bottleneck, but they do not
completely eliminate it.
Computer Programming 10
Types of Software Programs
• System software
• Operating System Applications
• e.g. Windows, Linux
• Utilities
System
• e.g. Antivirus, registry cleaner OS
software
Utilities
• Application software
• e.g. Photoshop, MS Word
Computer Programming 11
Why do Programming?
Amazon Forest in 1975 Amazon Forest in 1995
Computer Programming 13
How do we write a program?
Computer Programming 14
Computer Programming
Algorithm.....
Computer Programming
16
What do Computers Understand?
Computer Programming 17
Machine Language – 1st Generation Language
Computer Programming 18
Programming in Machine Language
• Task: Add two numbers
Decimal Binary
• Possible Steps? 0 0000
• Get first number 0001 1 0001
2 0010
• Get second number 3 0011
0110 4 0100
• Create a placeholder to keep the result 5 0101
6 0110
• Sum both numbers 0111 7 0111
8 1000
• Store the result in a placeholder 9 1001
Computer Programming 19
What are Programming Languages?
Computer Programming 20
What are Programming Languages?
Computer Programming 21
Assembly Language – 2nd Generation Language
Computer Programming 22
Assembly
Language –
2nd
Generation
Language
23
Computer Programming
Key Terms
Computer Programming
24
Computer Programming 25
Characteristics of a Programming Language
• Syntax
• How to write instructions to a computer
• Keywords
• Vocabulary of special/reserved words
with special meaning
• Explanation of the functionality of these
keywords
Computer Programming 26
Characteristics of a Programming Language
• Data structures
• What data types are supported by the
language
• Which operations can be performed on them
Computer Programming 27
Programming Language Hierarchy
5 GL
4 GL
High-Level Language
(3 GL)
Hardware (Computers)
Computer Programming 28
High-Level Languages (3 GL)
int x = 5;
• Codes like everyday English int y = 6;
• Use mathematical notations int sum = x + y;
system.out.println(sum);
• E.g., BASIC, C, C++, COBOL, FORTRAN, Java, …
Computer Programming 29
Fourth-Generation Languages (4GL)
• Above high-level languages
Computer Programming 30
Fifth-Generation Languages (5 GL)
• Designed to make the computer solve a given problem without
programming
• User only worry about what problems need to be solved and what
conditions need to be met
Computer Programming 31
Integrated Development
Environment (IDE)
Computer Programming 32
What is Integrated
Development
Environment (IDE)?
Computer Programming 33
Important Features of IDEs
Computer Programming
34
https://multiqos.com/blogs/guide-to-integrated-development-environment/
IDEs for C/C++ Development
Computer Programming 35
Next on the List…..
• What is C++?
• Features of C++
Bjarne Stroustrup
Getting Started with C++ 37
Features of C++ (1/4)
Object
Portable Rich Library Case Fast and
Oriented
Language of Functions Sensitive Efficient
Programming
Dynamic
Platform Exception Compiler
Memory
Independent Handling Based
Allocation
• Zero-cost Abstractions
• High-level abstractions like classes and templates do not add extra
runtime cost
• Efficient execution similar to lower-level languages is ensured
• Multiple Paradigms
• C++ supports procedural, object-oriented, and generic programming
paradigms
• Developers can choose the most efficient paradigm for each task
• Input
• Get data/value from a user via keyboard, mouse, or from other sources like file, other devices
• Output
• Display data/values on the screen or send them to a file, other devices
• Expression
• Combination of values, variables, operators, and calls to functions
• Need to be evaluated by performing calculations like addition, multiplication, etc.
• Conditional execution
• Check for certain conditions and execute the appropriate sequence of statements
• Repetition
• Perform some action repeatedly, usually with some variation
Getting Started with C++ 45
Basic C++ Program Structure
Preprocessor directives
Starts with #(hash)
Adds functionality – Let’s us work
#include <iostream> with I/O objects
Namespace Library - use names for
using namespace std; objects and variables from the standard
library
int main() Main Function Header
{ Block Opening
//Output on the screen Comments
#include <iostream>
int main() {
std::cout << “Welcome to NUST!";
return 0;
}
https://slideplayer.com/slide/13925899/
#include <iostream>
using namespace std;
int main()
Executable {
Linker main.exe //Output on the screen
cout<< "Welcome to NUST.\n";
return 0;
Getting Started with C++ } 48
Example 1.1
• Design an algorithm to find the perimeter and area of a rectangle.
Computer Programming 49
In C++…...
#include <iostream>
using namespace std;
int main() {
double length, width, perimeter, area;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
perimeter = 2 * (length + width);
area = length * width;
cout << "Perimeter of the rectangle: " << perimeter << endl;
cout << "Area of the rectangle: " << area << endl;
return 0;
}
Computer Programming 50
Additional Resources
Computer Programming 51
Additional Resources
• C++ Introduction (W3schools)
• C++ Programming Language (GeeksforGeeks)