OOP Using C++ Lecture 1 - Notes

Object Oriented Programming

Introduction to OOP Using C++

  • Dr. Mahmoud Alnamoly, Faculty of Computer and Informatics, Zagazig University, 2025.

Why OOP?

  • Structured Programming:

    • Uses functions like fun1 and fun2.

    • Statements are executed sequentially (e.g., Statment1, Statment2).

  • Procedural Programming:

    • Similar to structured programming.

  • Object Oriented Programming:

    • Uses classes (e.g., class1, class2).

    • Includes private data items and public function members.

    • Statements like Statment9, Statment10, etc., are part of class methods.

  • Drawbacks of non-OOP:

    • Unreadable code.

    • Repeatable code.

    • Error-prone.

    • Lacks data privacy.

Class Syntax

  • Class Declaration:

    • class foo { ... };

    • class is a keyword.

    • foo is the name of the class.

    • Braces {} enclose the class body.

    • Semicolon ; terminates the class declaration.

  • Private Members:

    • private: keyword and colon.

    • int data; declares a private data member.

    • Private members are for data privacy and are not directly accessible from outside the class.

  • Public Members:

    • public: keyword and colon.

    • void memfunc (int d) { data = d; } declares a public member function.

    • Public members are accessible from outside the class.

Class vs. Object

  • Class: A user-defined data type that behaves like built-in types.

    • A template or blueprint for creating objects.

    • Consists of data members (attributes) and function members (behavior).

    • Example: A blueprint for a house.

    • Doesn't occupy memory (except for static classes).

    • Cannot be directly manipulated.

  • Object: An instance of a class.

    • A collection of objects of similar type gives life to a class.

    • Example: Houses built from a blueprint.

    • Occupies memory.

    • Each object has its own copy of member functions.

    • Can be manipulated.

Example of Class and Object

  • Class definition:

class Employee {
private:
    int empid;
    string empname;
    float salary;
public:
    void getdata() {
        empid = 100;
        empname = "ABC";
        salary = 10000.0;
    }
    void Displayinfo() {
        cout << "Employee Id : " << empid << endl;
        cout << "Employee Name: " << empname << endl;
        cout << "Employee Salary: " << salary << endl;
    }
};
  • Object Creation and Usage:

    • Using stack:

int main() {
    Employee e;
    e.getdata();
    e.Displayinfo();
}
*   Using heap:
int main() {
    Employee *e = new Employee();
    e->getdata();
    e->Displayinfo();
}
  • Accessing members: object.member or object->member.

  • Memory allocation:

    • Stack allocation: Object is stored in the stack.

    • Heap allocation: Object is stored in the heap.

OOP Principles

  • Encapsulation: Binding data (variables) and methods into a single entity (class).

    • Helps keep related data and functions together.

    • Uses private variables (not visible) and public getter/setter methods (visible to all).
      *Example:

class Rectangle {
public:
    int length;
    int breadth;

    int getArea() {
        return length * breadth;
    }
};

int main() {
    Rectangle rect(8, 6);
    cout << "Area = " << rect.getArea();
    return 0;
}
  • Abstraction: Hides complex details and provides only essential information.
    *Example:

class Adder {
private:
    int total;
public:
    void addNum(int number) {
        total += number;
    }
    int getTotal() {
        return total;
    }
};

int main() {
    Adder a;
    a.addNum(10);
    a.addNum(20);
    a.addNum(30);
    cout << "Total " << a.getTotal() << endl;
    return 0;
}
  • Inheritance: Entities can inherit attributes from other entities.

    • Parent class (most general) and child class (general).

    • Example: Person (parent) -> Student, Employee (children).

    • Specific child classes inherit from general child classes

    • Example: ITStudent, MathStudent that inherit from the Student Class
      Diagrams showcasing inheritance between Person, Student, and Employee.

  • Polymorphism: Entities can have more than one form.

    • An operation may exhibit different behavior depending on the data used.

Polymorphism example

*Shape Class with a Draw () method that takes different forms when inherited by Line, Triangle, Circle and Rectangle
*Makesound() function for different animals

class Mammal {
public:
    virtual void makeSound() = 0;
    string toString() { return "Mammal"; }
};

class Cat: public Mammal {
public:
    virtual void makeSound() { cout << "rawr" << endl; }
    string toString() { return "Cat"; }
};

class Siamese: public Cat {
public:
    virtual void makeSound() { cout << "meow" << endl; }
    string toString() { return "Siamese"; }
    virtual void scratchCouch() { cout << "scraaaatch" << endl; }
};
*   `virtual` keyword enables runtime polymorphism.

Benefits of OOP

  • Code reusability through inheritance.

  • Data hiding through encapsulation.

  • Operator and function overloading through polymorphism.

Phases of OOP Development

  • Phase 1: Design Phase

    • Use UML (Unified Modeling Language) class diagrams.

    • Specifying the structure of the software system without implementation.

    • Transition from "what" the system must do to "how" the system will do it.

    • Determining classes, fields, methods, and interactions.
      *Example:
      *Rectangle class specifying width, length, setWidth(),setLength(), getWidth(), getLength(),getArea()
      *User class specifying id, firstname, lastname, gender, city, addeddate with user(), search() and show() methods.
      *Phone, Email and Address classes specifying phone, email and place, with corresponding methods.

  • Phase 2: Implementation Phase

    • Writing the code.

C++ Implementation

  • Separation of interface and implementation:

    • Interface: Declarations of functions, classes, members, etc. (in .h files).

    • Implementation: Definitions of how the above are implemented (in .cpp files).

  • Code files:

    • .h file (header file): Contains only interface (declarations).

    • .cpp file (source file): Contains definitions.
      *Example:
      *CLASS1.H and CLASS2.H containing declarations.
      *CLASS1.CPP and CLASS2.CPP containing implementations.
      *MAIN.CPP containing usage of the classes.#include files.

  • Scope Resolution Operator (::):

    • Used to define methods outside the class in .cpp files.

returnType ClassName::methodName(parameters) {
    statements;
}
  • Including header files:

#include "CLASS1.H"
  • Makes .cpp files aware of declarations.

  • At compilation, all definitions are linked together into an executable.

Example: Employee Class

  • Employee.h (header file):

#ifndef _classname_h
#define _classname_h
#include <iostream>
using namespace std;

class Employee {
private:
    int empid;
    string empname;
    float salary;
public:
    void getdata();
    void Displayinfo();
};

#endif
  • Employee.cpp (source file):

#include "Empolyee.h";

void Employee::getdata() {
    empid = 100;
    empname = "ABC";
    salary = 10000.0;
}

void Employee::Displayinfo() {
    cout << "Employee Id : " << empid << endl;
    cout << "Employee Name : " << empname << endl;
    cout << "Employee Salary: " << salary << endl;
}