Name of Lecturer: Gibril Njai
Course Title: Object Oriented Programming (using C++)
                    Course Goal: To teach students the theoretical concepts as well as the practical skill
                    needed to solve problems using C++.
                    Class & Semester: BSc CS &BIT- year 2                     Course Status: Core
                    Course Code:      BSOP215                                Credit Hours: 3 hrs
Lecture Notes       Email: gnjai@ebkustsl.edu.sl
                    Date & Time: Monday,
Lecture Outline: 1 & 2
                            OVERVIEW OF PROGRAMMING LANGUAGES
A computer cannot understand our language that we use in our day-to-day conversations, and likewise, we
cannot understand the binary language that the computer uses to do its tasks. It is therefore necessary for us to
write instructions in some specially defined language like C++ which is like natural language and after
converting with the help of compiler the computer can understand it.
What is Programming?
    Programming is the process of writing instructions (code) for a computer to execute. These
       instructions are written in a programming language that the computer understands.
    Programming language is a set of rules, symbols, and special words used to construct programs.
Types of Programming Languages
Programming languages can be classified into:
   1. Low-Level Languages
    Machine Language (Binary code: 0s and 1s)
    Assembly Language (Uses mnemonics like MOV, ADD)
   2. High-Level Languages
    Easier for humans to read and write (C, C++, Java, Python)
    Translated into machine language using a compiler or interpreter
Why C++?
    C++ is a general-purpose programming language.
    It is powerful, fast, and efficient.
    Supports procedural programming (like C) and object-oriented programming (OOP) (like Java).
    Used in system programming, game development, embedded systems, and more.
WHAT IS C++?
    C++ is a cross-platform language that can be used to create high-performance applications.
    C++ was developed by Bjarne Stroustrup, as an extension to the C language.
    C++ gives programmers a high level of control over system resources and memory.
    The language was updated 3 major times in 2011, 2014, and 2017 to C++11, C++14, and C++17.
1.3 WHY USE C++
    C++ is one of the world's most popular programming languages.
    C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.
    C++ is an object-oriented programming language which gives a clear structure to programs and allows
       code to be reused, lowering development costs.
    C++ is portable and can be used to develop applications that can be adapted to multiple platforms.
    C++ is fun and easy to learn!
    As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice vers
To start using C++, you need two things:
    A text editor, like Notepad, to write C++ code
    A compiler, like GCC, to translate the C++ code into a language that the computer will understand
Software complexity
                   Lots of lines in code – hard to understand
                   Not many meaningful comments
                   Too many variables – not named well
                   Complex functions – too nested, too lengthy, too many if conditions
                   Inter-dependencies – changes that have to be done together in multiple files
                   When you fix a bug, you make a few more.
                                 HISTORY AND FEATURES OF C++
Brief History of C++
      Developed by Bjarne Stroustrup at Bell Labs in the early 1980s.
      Evolved from the C language by adding object-oriented programming features.
      Standardized as ISO C++ with different versions (C++98, C++11, C++14, C++17, C++20).
Key Features of C++
   1. Fast and Efficient – Compiled language, runs faster than interpreted languages like Python.
   2. Object-Oriented – Supports classes and objects.
   3. Rich Library Support – Standard Template Library (STL) for data structures and algorithms.
   4. Platform Independent – Can run on different operating systems.
   5. Memory Management – Allows direct access to memory (pointers, dynamic memory allocation).
                     SETTING UP THE C++ DEVELOPMENT ENVIRONMENT
Installing a C++ Compiler
To write and execute C++ programs, you need a compiler like:
      GCC (GNU Compiler Collection) – Used on Linux/macOS, available in MinGW for Windows.
      Microsoft Visual C++ Compiler – Part of Visual Studio (Windows).
      Clang – Used on macOS and Linux.
Recommended IDEs (Integrated Development Environments)
                      IDE                      Platform                     Features
             Code:Blocks                 Windows, Linux             Lightweight, easy to use
             Dev-C++                     Windows                    Simple interface
                                                                    Feature-rich, best for
             Visual Studio               Windows
                                                                    large projects
             CLion                       Cross-platform             Advanced features (paid)
                                      C++ PROGRAM STRUCTURE
Basic Components of a C++ Program
Every C++ program has:
   1.   Header Files: #include <iostream> – Includes necessary functions.
   2.   Main Function: int main() {} – Execution starts from here.
   3.   Statements: std::cout << "Hello"; – Instructions to perform tasks.
   4.   Return Statement: return 0; – Indicates successful execution.
Breakdown of a Simple C++ Program
cpp – program extension
#include <iostream> // Includes the standard input/output library
         int main() {
         std::cout << "Welcome to C++ Programming!"; // Displays output
                    return 0; // Indicates successful execution
                   }
Explanation:
       #include <iostream> → Includes input/output functions.
       int main() → Defines the main function.
       {} → Denotes the start and end of the function.
       std::cout << "Welcome to C++ Programming!"; → Prints a message.
       return 0; → Terminates the program successfully.
                                            Key Concepts in C++
1. Input and Output in C++
C++ provides two basic I/O functions:
       std::cout → Prints output to the console.
       std::cin → Takes input from the user.
Example: Using cin and cout
                   #include <iostream>
                    int main() {
                       int age;
                       std::cout << "Enter your age: ";
                       std::cin >> age;
                       std::cout << "You are " << age << " years old.";
                       return 0;
                    }
Explanation:
      std::cin >> age; → Takes user input and stores it in the age variable.
      std::cout << "You are " << age << " years old."; → Displays the inputted age.
                  STEPS IN WRITING AND COMPILING A SIMPLE C++ PROGRAM
Step 1: Open an IDE or terminal
Step 2: Write a simple program
    Open Codeblocks
    go to File > New > Empty File.
    Write the following C++ code and save the file as myfirstprogram.cpp (File > Save File as):
       #include <iostream>
       using namespace std;
       int main()
       {
           cout << "Hello World!";
           return 0;
       }
Step 3: Compile the program
   Then, save the project using the extension .cpp go to Build > Build and Run to run (execute) the
   program using GCC. The result will look something to this:
                                              C++ COMPILER
A C++ compiler is itself a computer program which’s only job is to convert the C++ program from our form
to a form the computer can read and execute. The original C++ program is called the “source code”, and the
resulting compiled code produced by the compiler is usually called an “object file”.
Before compilation the preprocessor performs preliminary operations on C++ source files. Preprocessed form
of the source code is sent to compiler. After compilation stage object files are combined with predefined
libraries by a linker, sometimes called a binder, to produce the final complete file that can be executed by the
computer. A library is a collection of pre-compiled “object code” that provides operations that are done
repeatedly by many computer programs.
                                                                                                     Compil
                                   ation and linking of a C++ Program
Above Figure illustrates the process of translating a C++ source file into an executable file. You can perform
entire process of invoking the preprocessor, compiler, and linker with a single action by using Integrated
Development environment. These environments consist of a text editor, compiler, debugger, and other
utilities integrated into a package with a single set of menus. Preprocessing, compiling, linking, and even
executing a program is done by selecting a single item from a menu.
                                                  LECTURE II
                                     BASIC SYNTAX AND DATA TYPES
VARIABLES
     A variable is a named storage location in memory that holds a value, which may change during
         program execution.
     Variable enables our program to perform calculation on data. Example, we want to develop a program that add
         two numbers.
                                      Declaring and Initializing Variables
Syntax:
     data_type variable_name = value;
Example:
     int age = 25;      // Integer variable
     double pi = 3.14; // Floating-point variable
     char grade = 'A'; // Character variable
Rules for Naming Variables
    1. Can contain letters, digits, and underscores.
    2.    Must start with a letter or underscore (_).
    3. Cannot be a C++ keyword (e.g., int, return).
    4. Case-sensitive (age and Age are different).
Steps:
     STEP 1 : Allocate memory for storing three numbers
     STEP 2 : Store first number in computer memory
     STEP 3 : Store second number in computer memory
     STEP 4 : Add these two numbers together and store the result of the addition in a third memory
         location
     STEP 5 : Print the result
    STEP 1: Now first we'll allocate memory for storing numbers, location of the computer memory
       which is used to store data and is given a symbolic name for reference is known as variable. We need
       three variables, two for storing input and third for storing result. Before a variable is used in a
       program, we must declare it. This activity enables the compiler to make available the appropriate type
       of location in the memory. Following statements declare three variables of type integer to store whole
       numbers.
                      int p;
                      int q;
                      int r;
       You can declare more than one variable of same type in a single statement like :
                                int x, y, z;
    STEP 2 : Following statement stores value in first variable
                    p = 25;
    STEP 3 : Following statement stores value in second variable
                    q = 10;
    STEP 4 : Now, add these two numbers together and store the result of the addition in third variable
                   r = p + q;
    STEP 5 : Print the result
                  cout << "The sum is ";
                  cout << sum;
You can combine above two statements in one statement
               cout << "The sum is " << sum;
*** the complete program
               #include <iostream>
               using namespace std;
                    int main()
                {
                    int x;
                    int y;
                    int z;
                    x = 25;
                 y = 10;
                 z = x + y;
                 cout << "The sum is ";
                 cout << z;
                 return 0;
                 }
   Output :
         The sum is 35
Identifiers
        The identifier is a sequence of characters taken from C++ character set. In previous program x, y and z are
         identifiers of variables. The rule for the formation of an identifier are:
              i. An identifier can consist of alphabets, digits and/or underscores.
              ii. It must not start with a digit
              iii. C++ is case sensitive that is upper case and lower case letters are considered different from
                    each other.
              iv. It should not be a reserved word
Keywords
     There are some reserved words in C++ which have predefined meaning to compiler called keywords.
         These words may not be used as identifiers.
Tokens
     A token is a group of characters that logically belong together. The programmer can write a program by using
         tokens. C++ uses the following types of tokens. Keywords, Identifiers, Literals, Punctuators, Operators.
              1. Keywords - are some reserved words in C++ which have predefined meaning to compiler
                    called keywords. It is discussed in previous section
              2. Identifiers Symbolic names - A symbolic name is generally known as an identifier. The
                    identifier is a sequence of characters taken from C++ character set. The rule for the formation
              of an identifier are:------
                      An identifier can consist of alphabets, digits and/or underscores.
                      It must not start with a digit
                      C++ is case sensitive that is upper case and lower case letters are considered different
                       from each other.
                      It should not be a reserved word
           3. Literals - (often referred to as constants) are data items that never change their value during
              the execution of the program. The following types of literals are available in C++. Declared
              using const.
                      Integer-Constants
                      Character-constants
                      Floating-constants
                      Strings-constants
           Example: const double PI = 3.14159; // Constant variable
                                                DATA TYPES IN C++
C++ has four primary data types:
1. Integer Types (int, long, short)
Used to store whole numbers (positive or negative).
                              Type          Size (Bytes)                Range
                        short int           2              -32,768 to 32,767
                        int                 4              -2,147,483,648 to 2,147,483,647
                        long int            4 or 8         Varies by system
                        long long int 8                    Very large range
Example:
      int num = 100;
      long bigNumber = 1000000;
2. Floating-Point Types (float, double)
Used for numbers with decimals.
                                     Type       Size (Bytes)        Precision
                                   float        4              6-7 decimal places
                                 doubl      8                 15-16 decimal places
                                 e
Example:
    float price = 12.99;
    double preciseValue = 3.1415926535;
3. Character Type (char)
Used for single characters. Enclosed in single quotes ('').
Example:
    char grade = 'A';
4. Boolean Type (bool)
Used for storing true or false values.
Example:
    bool isStudent = true;
                                         INPUT AND OUTPUT (cin, cout)
   1. Using cout for Output
cout is used to display output on the screen.
    << (insertion operator) is used to send data to cout.
Example:
    #include <iostream>
    using namespace std;
    int main() {
    cout << "Hello, C++!";
    cout << "Welcome to programming.";
    return 0;
    }
       Output: Hello, C++!Welcome to programming.
   2. Using cin for Input
cin is used to take input from the user.
        >> (extraction operator) is used to store user input into a variable.
Example:
#include <iostream>
using namespace std;
int main() {
  int age;
  cout << "Enter your age: ";
  cin >> age;
  cout << "You are " << age << " years old.";
  return 0;
                  }
Input:
20
Output: You are 20 years old.
                                              OPERATORS IN C++
Operators are symbols that perform operations on variables and values. C++ provides six types of operators.
Arithmetical operators, Relational operators, Logical operators, Unary operators, Assignment operators,
Conditional operators, Comma operator
1. Arithmetic Operators
Arithmetical operators +, -, *, /, and % are used to performs an arithmetic (numeric) operation. You can use
the operators +, -, *, and / with both integral and floating-point data types. Modulus or remainder % operator
is used only with the integral data type. Operators that have two operands are called binary operators.
                                   Operato            Meaning            Example
                                       r
                                  +            Addition                  a+b
                                  -            Subtraction               a-b
                                  *            Multiplication            a*b
                                  /            Division                  a/b
                                  %            Modulus (Remainder) a % b
Example:
               int a = 10, b = 3;
               cout << "Sum: " << (a + b);
               cout << "Remainder: " << (a % b);
2. Relational (Comparison) Operators
Used to compare or test the relation between two values. Returns true (1) or false (0). All relational operators
are binary operators and therefore require two operands. A relational expression returns zero when the
relation is false and a non-zero when it is true. The following table shows the relational operators
                                Operator              Meaning           Example
                                ==           Equal to                   a == b
                                !=           Not equal to               a != b
                                >            Greater than               a>b
                                <            Less than                  a<b
                                >=           Greater than or equal to a >= b
                                <=           Less than or equal to      a <= b
Example:
               int a = 5, b = 10;
               cout << (a < b); // Output: 1 (true)
3. Logical Operators
Used to combine to combine one or more relational expression/conditions.
                             Operator        Meaning                   Example
                        &&                 AND              (a > 0) && (b > 0)
                        ||                 OR               `
                        !                  NOT              !(a > 0)
Example:
    bool result = (5 > 2) && (10 > 5); // true
4. Assignment Operators
Used to assign a variable to a values. This operator takes the expression on its right-hand-side and places it
into the variable on its left-hand-side.
    Example, the operator takes the expression on the right, 5, and stores it in the variable on the left, m.
                                      p = q = r = 32;
          This code stores the value 32 in each of the three variables p, q, and r
                            Operator               Example              Equivalent to
                       =                      a=5                 a=5
                       +=                     a += 3              a=a+3
                       -=                     a -= 2              a=a-2
                       *=                     a *= 4              a=a*4
                       /=                     a /= 2              a=a/2
                       %=                     a %= 3              a=a%3
5. Increment and Decrement Operators - C++ provides two special operators viz '++' and '--' for
incrementing and decrementing the value of a variable by 1. The increment/decrement operator can be used
with any type of variable but it cannot be used with any constant. Increment and decrement operators each
have two forms, pre and post.
     The syntax of the increment operator is:
                             Pre-increment: ++variable
                              Post-increment: variable++
     The syntax of the decrement operator is:
                             Pre-decrement: ––variable
                             Post-decrement: variable––
    In Prefix form first variable is first incremented/decremented, then evaluated In Postfix form first variable
    is first evaluated, then incremented/decremented
                int p, q;
                int i = 10, j = 10;
                p = ++i; //add one to i, store the result back in p
                q= j++; //store the value of j to q then add one to j
                cout << p; //11
               cout << q; //10
6.Conditional operator - The conditional operator ?: is called ternary operator as it requires three operands.
The format of the conditional operator is: Conditional_ expression ? expression1 : expression2; If the value of
conditional expression is true then the expression1 is evaluated, otherwise expression2 is evaluate
              int a = 5, b = 6;
              big = (a > b) ? a : b;
        The condition evaluates to false, therefore biggets the value from b and it becomes 6.
7.The comma operator - gives left to right evaluation of expressions. When the set of expressions has to be
evaluated for a value, only the rightmost expression is considered.
                 int a = 1, b = 2, c = 3, i; // comma acts as separator, not as an operator
                 i = (a, b); // stores b into i
   Would first assign the value of a to i, and then assign value of b to variable i. So, at the end, variable i
   would contain the value 2
8.The sizeof operator - the sizeof operator determines the amount of memory in byte required for an object
at compile time rather than at run time. For example
                   sizeof (char) returns 1
                   sizeof (float) returns 4
                                             OPERATOR PRECEDENCE
The order in which the Arithmetic operators (+,-,*,/,%) are used in a. given expression is called the order of
precedence. The following table shows the order of precedence.
                                             Order               Operators
                                     First                ()
                                     Second               *, /, %
                                     Third                +, -
CONSTANTS
A variable which does not change its value during execution of a program is known as a constant variable. Any attempt
to change the value of a constant will result in an error message. A constant in C++ can be of any of the basic data
types, const qualifier can be used to declare constant as shown below:
    const float PI = 3.1415;
The above declaration means that PI is a constant of float types having a value 3.1415. Examples of valid constant
declarations are:
    const int RATE = 50;
    const float PI = 3.1415;
    const char CH = 'A';
                                     TYPE CONVERSION AND TYPE CASTING
The process in which one pre-defined type of expression is converted into another type is called conversion. There are
two types of conversion in C++.
              i. Implicit conversion
              ii. Explicit conversion
Implicit Type Conversion (Automatic)
     Data type can be mixed in the expression. For example,
                    double a;
                    int b = 5;
                    float c = 8.5;
                    a = b * c;
When two operands of different type are encountered in the same expression, the lower type      variable is converted to
the higher type variable. The following table shows the order of data types
       Example: int to float.
Example:
int a = 10;
double b = a; // Implicit conversion (int to double)
cout << b; // Output: 10.0
Explicit Type Conversion (Type Casting)
      Done manually using casting syntax.
Example:
       double pi = 3.14159;
       int approx = (int) pi; // Explicit conversion (double to int)
       cout << approx; // Output: 3
Summary:
    Programming involves writing instructions for a computer.
    C++ is a powerful and versatile language, combining procedural and OOP features.
    To start coding in C++, install a compiler and an IDE.
    Basic structure of a C++ program includes header files, the main function, and statements.
    I/O operations in C++ use std::cin for input and std::cout for output.
    Variables store data and must have a valid name.
    C++ has different data types (int, float, char, bool).
    cin is used for input, cout is used for output.
    Arithmetic, relational, and logical operators perform computations.
    Type conversion can be implicit (automatic) or explicit (casting)
Exercise:
   1. Modify the "Hello, World" program to print two lines of text.
   2. Write a program that asks the user for their favorite number and prints it.
   3. Write a C++ program that prints your name and age.
   4. Write a program that asks for two numbers and prints their sum.
   5. Write a program to check if a number is even or odd.
   6. Modify the "age" program to use double instead of int
References:
1. Programming: Principles and Practice Using C++ by Bjarne Stroustrup
2. Object-Oriented Programming In C++ 4th Edition by Robert Lafore