Pwet Ni Charlim
Pwet Ni Charlim
1958-68 ALGOL John Backus (US), Peter Naur, Van Wijngaarden (Europe) US & Europe
What is C++?
C++ is an extension of the C language, inheriting its rich operator set and portability across systems.
Compatible with C programs, maintaining compatibility was a primary design goal.
Advantages of C++ over C:
o Strong Typing: Improved type safety.
o Object-Oriented Programming: Introduces classes and objects.
o General Purpose Language: C++ can replace C in general-purpose programming.
Features of C++
Portability: Programs can run on various machines and systems.
Compatibility: C++ compilers can handle existing C code.
Object-Oriented Programming (OOP): Support for classes and objects.
Rich Operator Set: Inherits from C, which includes a variety of operators.
Main Function: Every C++ program must have a main() function.
Flowchart Overview
A flowchart is a graphical representation of a process, showing the steps in sequence using
specialized symbols.
It provides a visual guide, helping people follow a process logically from beginning to end.
Applications: Used to model data, instructions, processes, information, and workflows.
Importance of Flowcharts
Flowcharts help illustrate complex processes in an easy-to-understand visual form.
Useful as a business tool for effective and efficient communication of process steps.
Flowcharting Symbols
Here are key symbols used in flowcharts:
⬦ Terminal Used for START (beginning) and STOP (ending) of the flowchart.
☐ Input/Output Defines when to request input from the user or output a value.
➔ Flowlines Shows the sequence or flow of the process steps with directional arrows.
● On-Page Connector Connects parts of the flowchart on the same page, avoiding tangled lines.
⧫ Off-Page Connector Connects parts of the flowchart across different pages, for clarity.
Key Takeaways
Flowcharts help simplify processes by using visual symbols.
Flowlines show the logical sequence, while connectors help avoid confusion in complex diagrams.
Decision and Process symbols are crucial for mapping out logic and operations.
int 4 bytes Stores whole numbers without decimals (e.g., 123, -123).
float 4 bytes Stores fractional numbers with up to 7 decimal digits (e.g., 19.99).
double 8 bytes Stores fractional numbers with up to 15 decimal digits (e.g., 19.9999).
char 1 byte Stores a single character, letter, or ASCII value (e.g., 'a', 'B').
Declaring Variables
To declare a variable, specify the data type followed by the variable name:
int myNum = 15;
Syntax: type variableName = value;
Example:
#include <iostream>
using namespace std;
main() {
int myNum = 15;
cout << myNum; // OUTPUT: 15
}
#include <iostream>
using namespace std;
main() {
int myNum;
myNum = 15;
Overwriting values:
#include <iostream>
using namespace std;
main() {
int myNum = 15;
myNum = 10;
cout << myNum; // OUTPUT: 10
}
Displaying Variables
The cout object with the << operator is used to display variables.
Example:
#include <iostream>
using namespace std;
main() {
int myAge = 35;
cout << "I am " << myAge << " years old."; // OUTPUT: I am 35 years old.
}
Adding Variables
To add variables, use the + operator:
#include <iostream>
using namespace std;
main() {
int x = 5;
int y = 6;
int sum = x + y;
cout << sum; // OUTPUT: 11
}
main() {
int x = 5, y = 6, z = 50;
cout << x + y + z; // OUTPUT: 61
}
C++ Identifiers
Identifiers are unique names for variables. Use descriptive names to improve readability and maintainability.
Example:
#include <iostream>
using namespace std;
main() {
int minutesPerHour = 60; // Good name
int m = 60; // Not descriptive
cout << minutesPerHour << "\n"; // OUTPUT: 60
cout << m; // OUTPUT: 60
}
C++ Constants
To prevent modification of a variable, use the const keyword:
#include <iostream>
using namespace std;
main() {
const int myNum = 15;
// myNum = 10; // Error: Assignment of read-only variable
}
Example of constants:
#include <iostream>
using namespace std;
main() {
const int minutesPerHour = 60;
const float PI = 3.14;
cout << minutesPerHour << "\n"; // OUTPUT: 60
cout << PI; // OUTPUT: 3.14
}
1. Assignment Operators
Assignment operators assign values to variables.
Operator Description Example
2. Arithmetic Operators
Used for common mathematical operations.
Operator Name Example
+ Addition C = A + B;
- Subtraction C = A - B;
* Multiplication C = A * B;
Operator Name Example
/ Division C = A / B;
% Modulus C = A % B;
++ Increment ++A;
-- Decrement --A;
== Equal to (A == B)
!= Not equal to (A != B)
4. Logical Operators
Used to combine multiple conditions.
Operator Description Example
|| Logical OR `(A
! Logical NOT !A