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

C++ 1

Uploaded by

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

C++ 1

Uploaded by

ankamahinstagram
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

INTRODUCTIN TO C++

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:

■ int age = 25;

■ double pi = 3.14159;

■ char initial = 'A';


Rules For Declaring Variables
The name you choose for your variable must follow these rules:

● It can start with a letter (uppercase or lowercase) or an underscore (_).


● It can contain letters, numbers, and underscores after the initial character.
● It cannot be a reserved keyword in C++ (like int, float, if, else, etc.).
● It's case-sensitive ( age and Age are considered different variables).
Data Types:

○ Define the type of data a variable can hold (integers, floating-point numbers, characters, etc.).

Fundamental Data Types:

● int (integer): Stores whole numbers (positive, negative, or zero).


Examples: age (30), score (-100), count (15).

● float (floating-point): Stores decimal numbers (less precise than double).


Examples: pi (3.14159), average (2.75).
Fundamental Data Types:

● double (floating-point): Stores decimal numbers with higher precision than float.
Examples: scientific_data (1.234567890123), distance (3.1415926535).

● char (character): Stores a single character (letter, number, or symbol). Represented by


single quotes ('). Examples: initial ('A'), symbol ('&').

● bool (boolean): Stores logical values (true or false). Represented by true or false.
Operators

Arithmetic Operators

○ Perform operations on data. We'll use arithmetic operators (+, -, *, /)


○ Examples:

■ 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

○ Perform operations on data. We'll use Logical operators ( &&, ||, !)


○ Examples:

■ True || False

■ Gari && Beans


● Syntax

○ These terms are crucial for understanding any programming language.

○ 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).

○ Example (incorrect syntax): int age = "twentyfive";

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

stores it in the name variable)


#include <iostream> // This line includes the iostream library for input/output
Using namespace std;

int main() {
string name;

cout << "Enter your name: ";


cin >> name;

cout << "Hello, " << name << "!" << std::endl; // endl adds a newline

return 0; // Indicates successful program termination


}
Control Flow

● Conditional Statements:
○ Control the program's flow based on certain conditions. We'll use if, else if, and else statements.

○ Example:
■ if (age >= 18) {

cout << "You are an adult.";

else {

cout << "You are not an adult.";

}
● Loops:
○ Repeat a block of code a certain number of times. We'll cover for loops and while loops.

○ Example (for loop):


for (int i = 0; i < 5; i++)

cout << i << " ";

(prints numbers 0 to 4)
Functions:

○ Reusable blocks of code that perform a specific task. We'll define functions

with parameters and a return value.

○ Example: int add(int a, int b) { return a + b; } (function to add

two numbers)
#include <iostream>

using namespace std;

int main() {

int age;

cout << "Enter your age: ";

cin >> age;

if (age >= 18) {

cout << "You are an adult." << endl;

} else {

cout << "You are not an adult." << endl;

return 0;

}
#include <iostream>

int add(int a, int b) {


return a + b;
}

int main() {
int num1, num2;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

int sum = add(num1, num2); // Call the add function

cout << "The sum is: " << sum << std::endl;

return 0;
}
Libraries:

Pre-written collections of code that provide functionalities we can use in


our programs. They save time and effort by offering common operations.

Example: The iostream library (included with #include <iostream>)


provides input/output functionalities like cin and cout.

Using Libraries: We include the necessary header file (e.g., <iostream>)


at the beginning of our code to access the library's functions.
<fstream>: This library deals with file operations. You can use it to open, read from, write to, and close files.

<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.

You might also like