2. C-- Introduction
2. C-- Introduction
• Header Files: Include statements for accessing standard library functions and
classes.
• Function Definitions: Including the main() function where the execution starts.
means that we
can use names
blank line for for objects and
readability variables from
the standard
library.
This is called a
function. Any code
inside its curly
brackets {} will be
executed.
• They are lines read and processed by the preprocessor before proper
compilation begins.
• Preprocessor directives must be specified in their own line and, because they
are not statements, do not have to end with a semicolon (;).
Headers
Header files in C++ contain declarations of functions and classes. Some
commonly used headers are:
<iostream>: For standard input and output stream objects like cin and cout.
<vector>: For using the vector container.
<string>: To use the string class.
<algorithm>: For common algorithms like sort, search, etc.
<cmath>: For mathematical functions.
C++ Statements
• In a programming language, these programming instructions are called
statements.
• The following statement "instructs" the compiler to print the text "Hello World"
to the screen:
Output
cout << "Hello World!";
Output
// This is a comment
cout << "Hello World!";
Single-line Comments
cout << "Hello World!"; // This is a comment
Multi-line Comments
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
Standard Output (cout)
cout is used in conjunction with the insertion operator, which is written as
<< (two "less than" signs).
int age;
Note:
float precision ~6 to 7 decimal places
double precision ~15 to 16 decimal places
3.141592 3.141592653589793
• A float can store fewer decimal places (~6-7), leading to small
errors.
• A double is more precise (~15-16 decimal places) and is used in
most calculations requiring accuracy.
Boolean Type
• A boolean data type is declared with the bool keyword, take the values
true or false.
• When the value is returned, true = 1 and false = 0.
• Boolean values are mostly used for conditional testing.
Try this!
Create a variable called myNum of type int and assign it the value 15.
int myNum;
myNum = 15;
cout << myNum;
Note that if you assign a new value to an existing variable, it will overwrite the previous
value:
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
C++ Constants
When you do not want others (or yourself) to change existing variable values, use
the const keyword.
You should always declare the variable as constant when you have values that are
unlikely to change:
// Print variables
cout << "Student ID: " << studentID << "\n";
cout << "Student Age: " << studentAge << "\n";
cout << "Student Fee: " << studentFee << "\n";
cout << "Student Grade: " << studentGrade << "\n";
Practice Exercise 2: (not graded)
Problem:
Calculate the area of a rectangle given its length and width.
Task:
1. Create an Algorithm
2. Create an IPO (Input-Process-Output) Chart
3. Draw a Flowchart
4. Write the C++ Code
Practice Exercise 2: (not graded)
Expected Output:
OPERATORS
Arithmetic Operators
• Arithmetic operators are fundamental elements of any programming
language, including C++.
• *, /, and % are at the same level of precedence and are evaluated next.
• + and - have the same level of precedence and are evaluated last.
Final result:
The value of result will be 9.
Assignment Operator
• Assignment operators are used to assign values to variables in programming
languages, including C++.
• They provide a concise and efficient way to update the value of a variable
based on a computation or another variable's value.
Example
STRING HANDLING
String Concatenation
• String concatenation is the process of combining two or more strings into a
single string.
String Copying
• String copying involves copying the contents of one string to another. In C++,
you can simply assign one string to another using the assignment operator
(=).
Practice Exercise 2: (not graded)
Simple Average Calculator
Instructions:
Prompt the user to enter three numbers.
Use arithmetic operators to calculate the sum and average.
Sum = number1 + number2 + number3
Average = Sum ÷ 3.