C++ 1
C++ 1
Dr Ahene Emmanuel
Introduction
Programming Languages
● Programming languages are like special sets of instructions used to communicate with
computers and tell them what tasks to perform.
● Just like we have different languages for different purposes (e.g., English for everyday
use, French for romance), there are many programming languages, each suited for
specific tasks.
Examples of Programming Languages
● Machine Code
● Low level languages
○ Assembly language
● High level languages
○ Java
○ Dart
○ R
○ C
○ C#
○ Python
○ c++
C++
● What is C++?
○ A general-purpose programming language known for its speed, efficiency, and control over
hardware.
○ Used in game development, system programming, and various other applications.
● Why Learn C++?
○ Develops strong programming fundamentals applicable to other languages.
○ Enables efficient and powerful application creation.
○ Opens doors to various programming fields.
Programming Environments
● Setting Up:
○ We'll need a C++ compiler (e.g., GCC) and an Integrated Development Environment (IDE)
like
○ Visual Studio Code
○ Code::Blocks (we'll discuss installation briefly).
○ Dev c++
○ C Lion
Building Blocks
variables
○ Named storage locations for data. We'll declare variables with a data type (e.g., int age;) to hold integers.
○ Examples:
■ double pi = 3.14159;
○ Define the type of data a variable can hold (integers, floating-point numbers, characters, etc.).
● double (floating-point): Stores decimal numbers with higher precision than float.
Examples: scientific_data (1.234567890123), distance (3.1415926535).
● bool (boolean): Stores logical values (true or false). Represented by true or false.
Operators
Arithmetic Operators
■ age + 10,
■ pi * radius,
■ x / y,
Operators
Comparison Operators
○ Perform operations on data. We'll use Comparison operators ( ==, !=, <, >)
○ Examples:
■ name == "Alice";
■ x<10;
■ x != 10
Operators
Logical Operators
■ True || False
○ Syntax: Refers to the grammar and structure of the code. It's like the language's set of rules for how to
write instructions that the computer can understand. Incorrect syntax will result in errors during compilation
(the process of translating code into machine code).
Semantics: Deals with the meaning of the code. It's what the code actually does when it's executed. Even if
the syntax is correct, the code might not produce the intended outcome if the semantics are wrong (e.g.,
using division by zero).
Basic Structure:
A typical C++ program follows this general structure:
1. Preprocessor Directives: Lines starting with # (hash sign) are preprocessor directives. They
instruct the compiler to perform actions before actual compilation, like including header files
(#include <iostream>) or defining constants (#define PI 3.14159).
2. Namespace Declarations: Namespaces help organize code and avoid naming conflicts. You might
use using namespace std; to access standard library elements (like cout for output).
3. Functions: Reusable blocks of code that perform specific tasks. A function definition includes a
return type, name, parameter list (in parentheses), curly braces ({}) containing the function body.
Basic Structure:
4. Main Function: The entry point of your program execution. It usually has the
signature int main().
5. Statements: Instructions that tell the program what to do. Statements end with a
semicolon ;. Examples include variable declarations, input/output operations, and
control flow statements (if, else, for, while).
6. Comments: Lines explaining your code, ignored by the compiler. Use // for single-
line comments and /* */ for multi-line comments.
● Input and Output:
○ C++ provides ways to interact with the user. We'll use cin to take input and cout to
display output.
○ Example: cout << "Enter your name: "; cin >> name; (reads user's name and
int main() {
string name;
cout << "Hello, " << name << "!" << std::endl; // endl adds a newline
● Conditional Statements:
○ Control the program's flow based on certain conditions. We'll use if, else if, and else statements.
○ Example:
■ if (age >= 18) {
else {
}
● Loops:
○ Repeat a block of code a certain number of times. We'll cover for loops and while loops.
(prints numbers 0 to 4)
Functions:
○ Reusable blocks of code that perform a specific task. We'll define functions
two numbers)
#include <iostream>
int main() {
int age;
} else {
return 0;
}
#include <iostream>
int main() {
int num1, num2;
cout << "The sum is: " << sum << std::endl;
return 0;
}
Libraries:
<string>: This library provides functionalities for working with strings (sequences of characters). It offers
features for string creation, manipulation (searching, replacing, concatenation), and comparison.
<cmath>: This library includes mathematical functions like trigonometric functions (sin, cos, tan), exponential
and logarithmic functions (exp, log), and common mathematical constants (PI, M_E).
<cstdlib>: This library offers general utility functions like memory allocation (malloc, free), random number
generation (rand, srand), and process control (exit).
<ctime>: This library deals with time and date functionalities. You can use it to get the current time, format
dates, and convert between different time representations.
<algorithm>: This library provides a rich set of algorithms for working with collections (like arrays, vectors)
such as sorting, searching, finding minimum/maximum elements, and transforming elements.