Object Oriented Programming –
CS/CE 224/272
Nadia Nasir
Habib University, Fall 2024
Content
• How to build C++ program
• Preprocessor directives
• Header files
• Using namespace std
• User input
• Loops
• If/else
• switch
• Logical and arithmetic operators
• Datatypes
• int, size_t, double, float, bool, and char
• Cast and type conversion
• Definition and declaration
• Setw
• math functions
Preprocessor directives and Header files
• C++ treats input and output as a stream of characters.
• stream : sequence of characters (printable or nonprintable)
• The functions to allow standard I/O are in iostream header file.
• #include <iostream>
• instructs the preprocessor to include a section of standard C++
code, known as header iostream, that allows to perform
standard input and output operations, such as writing the output
of this program (Hello World) to the screen.
• namespace:
• a collection of names and their definitions. Allows different
namespaces to use the same names without confusion.
main() function
The function named main is a special function in all C++ programs.
It is the function called when the program is run.
The execution of all C++ programs begins with the main function,
regardless of where the function is actually located within the code.
What is a Variable?
A variable is a memory address where data can be stored and
changed.
Declaring a variable means specifying both its name and its data type.
C++ Data Types
simple structured
integral enum floating array struct union class
char short int long bool
address
float double long double
pointer reference
Datatypes
• Signed integers types:
• short, int, long, and long long
• Unsigned integer types
• unsigned short, unsigned int, unsigned long, unsigned long long
• Decimal datatypes
• float, double
• Boolean data type
• bool
• Character data type
• char
• Nothing data type
• void
Datatypes
Type Typical Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 126
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int Range 0 to 65,535
signed short int Range -32768 to 32767
long int 4bytes -2,147,483,648 to 2,147,483,647
signed long int 4bytes same as long int
unsigned long int 4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 16bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character
Cin-n-Cout
• Cin reads input from the stream
→→→→→→→→→→→
int main(int argc, char* argv[])
{
int n = 0;
std::cin >> n;
std::cout << "You typed:" << n << '\n';
}
• Cout is used to print to output stream
←←←←←←←←←←←←←←←←←←←
std::endl vs \n
• To print new line, you can use `std::endl`
• OR you can use ‘\n’
• Use whichever approach you prefer. But using \n is better
Loopiness!
• Any loop can be used in any situation! It is just your preference.
• Some are preferred at times over other. You will know which ones to
use as you practise!
• For loop. Conceptually similar to python. It runs a fixed amount of
time
For Loop
For loop
• Bare minimum for loop!
• You only need 2 semicolons!
• Another variation
• There are many variations of
for loop. You will learn them
as you get more experienced
with it
While loop
• The structure of while loop is very similar to python
• The output will be the same as the for loop we earlier saw
Do-while loop
• Consider that a homework and a reading exercise.
• Convert the earlier code into a do-while loop!
Decisions decisions!
• Familiar if/else to make decisions
Decisions decisions!
• You can also use if/else if/else
• What you learned in pfun about
chained and nested conditionals
holds here too
• An if statement can be nested inside
another if or else if or else
• Like in python, you can’t have an
else without if
Homework
• What this code will do?
• Take input from a user compute the square of a number. User
should be prompted appropriately, and the output should be printed
out. The program should exit when user just presses enter
Concepts covered so far
• iostream , namespace std
• For, while, and do while loop
• cin, cout, endl, setw, setprecision
• Datatypes , size and its range
• Signed, and unsigned int
• Decimal types
• Bool, char and void
• If/else if/else
Acknowledgement
Most of the slides are adapted from the lectures of Musabbir
Abdul Majeed