Lesson 2 +30 XP

Basic Concepts of Object Oriented Programming

Basic Concepts of Object Oriented Programming

Every object oriented language is built on the same small set of ideas. IPU asks about them every year, often as "Explain the basic concepts of OOP with examples" for 10 marks. This lesson covers all eight, each with a definition, a real-world picture and a short C++ program.

ConceptOne-line meaning
Objecta run-time entity with state (data) and behaviour (functions)
Classa user-defined type: the blueprint from which objects are made
Data abstractionshowing only the essential features, hiding the details
Encapsulationwrapping data and functions into one unit (the class)
Inheritancea new class acquires the properties of an existing class
Polymorphismone name, many forms
Dynamic bindingthe code to run for a call is chosen at run time
Message passingobjects communicate by calling each other's functions

1. Objects

An object is a basic run-time entity in an object oriented system. It may represent a person, a bank account, a table of data or anything the program has to handle. Every object has:

  • State: the values of its data at a moment (a student's roll number and marks);
  • Behaviour: what it can do, given by its functions (display marks, compute grade);
  • Identity: what makes it distinct from every other object, even one with the same state (its own memory address).

Objects take up space in memory and have an address, like a structure variable in C. When a program runs, objects interact by sending messages to one another.

An object: identity, state and behaviourAn object: identity, state and behaviour

2. Classes

A class is a user-defined data type that describes the data and functions an object will have. Once a class is defined, any number of objects of that class can be created. A class is to its objects what a cookie cutter is to cookies, or int is to the variables a, b, c.

C++
#include <iostream>
using namespace std;

class Student { // the class: a blueprint
    int roll;   // data (state)
    float marks;

public:
    void setData(int r, float m) {
        roll = r;
        marks = m;
    } // functions (behaviour)
    void show() {
        cout << "Roll " << roll << ": " << marks << endl;
    }
};

int main() {
    Student s1, s2; // two objects of the class
    s1.setData(1, 88.5);
    s2.setData(2, 74);
    s1.show();
    s2.show();
    return 0;
}

Output:

OUTPUT
Roll 1: 88.5
Roll 2: 74
ClassObject
A blueprint or templateAn instance of the class
A logical entity; no memory for data when declaredA physical entity; memory is allocated when it is created
Declared onceMany objects can be created from one class
Example: StudentExample: s1, s2

3. Data Abstraction

Abstraction means representing the essential features without including the background details. When you drive a car you use the steering wheel, accelerator and brake. You do not need to know how the fuel injection works.

Classes use abstraction: they are defined as a list of abstract attributes (size, weight, cost) and the functions that operate on them. Because classes use data abstraction, they are called Abstract Data Types (ADTs). The user of a class needs to know what a function does, not how it does it.

C++
#include <iostream>
using namespace std;

class Car {
    int speed;
    void injectFuel() {
        speed = speed + 10;
    } // hidden detail
public:
    Car() {
        speed = 0;
    }
    void accelerate() {
        injectFuel();
    } // the essential feature
    int getSpeed() {
        return speed;
    }
};

int main() {
    Car c;
    c.accelerate();
    c.accelerate();
    cout << "Speed: " << c.getSpeed() << endl;
    return 0;
}

Output:

OUTPUT
Speed: 20

4. Encapsulation

Encapsulation is the wrapping up of data and functions into a single unit called a class. It is the most striking feature of a class. The data is not accessible to the outside world; only the functions wrapped in the class can access it. These functions provide the interface between the object's data and the program.

Keeping the data away from direct access by the program is called data hiding or information hiding. In C++ it is done with the private keyword.

AbstractionEncapsulation
Solves the problem at the design levelSolves the problem at the implementation level
Hides complexity (how it works)Hides data (what is stored)
Focus on what the object doesFocus on how the data is protected
Achieved with classes and interfaces (abstract classes)Achieved with access specifiers (private, public)

5. Inheritance

Inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports hierarchical classification: a sparrow is a bird, and a bird is an animal. The existing class is the base (parent) class; the new class is the derived (child) class.

Inheritance gives reusability: we can add features to an existing class without modifying it, by deriving a new class from it.

C++
#include <iostream>
using namespace std;

class Animal { // base class
public:
    void eat() {
        cout << "Eating" << endl;
    }
};

class Bird : public Animal { // derived class
public:
    void fly() {
        cout << "Flying" << endl;
    }
};

int main() {
    Bird b;
    b.eat(); // inherited from Animal
    b.fly(); // its own
    return 0;
}

Output:

OUTPUT
Eating
Flying

6. Polymorphism

Polymorphism (Greek: poly = many, morph = form) is the ability to take more than one form. The same operation may behave differently on different types of data. For example, + adds two numbers but joins two strings.

KindResolvedHow in C++
Compile-time (static, early binding)when the program is compiledfunction overloading, operator overloading, templates
Run-time (dynamic, late binding)while the program runsvirtual functions with base-class pointers
C++
#include <iostream>
using namespace std;

// One name, many forms: function overloading
int area(int side) {
    return side * side;
}
int area(int l, int b) {
    return l * b;
}
double area(double r) {
    return 3.14 * r * r;
}

int main() {
    cout << area(4) << endl;
    cout << area(3, 5) << endl;
    cout << area(2.0) << endl;
    return 0;
}

Output:

OUTPUT
16
15
12.56

7. Dynamic Binding

Binding links a function call to the code that will run. In static (early) binding this is fixed at compile time. In dynamic (late) binding the code is chosen at run time, depending on the actual object a pointer points to. C++ uses virtual functions for dynamic binding.

C++
#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() {
        cout << "Drawing a shape" << endl;
    }
    virtual ~Shape() {}
};
class Circle : public Shape {
public:
    void draw() {
        cout << "Drawing a circle" << endl;
    }
};
class Square : public Shape {
public:
    void draw() {
        cout << "Drawing a square" << endl;
    }
};

int main() {
    Shape* list[3];
    list[0] = new Shape;
    list[1] = new Circle;
    list[2] = new Square;
    for (int i = 0; i < 3; i++) {
        list[i]->draw(); // which draw()? decided at run time
    }
    for (int i = 0; i < 3; i++) {
        delete list[i];
    }
    return 0;
}

Output:

OUTPUT
Drawing a shape
Drawing a circle
Drawing a square

8. Message Passing

Objects communicate by sending and receiving messages. A message to an object is a request to run one of its functions. Message passing involves three things: the name of the object, the name of the function (the message) and the information to send (arguments).

underbrace{texttt{employee}}_{object}.underbrace{texttt{salary}}_{message}(underbrace{texttt{name}}_{information})

An object lives from creation to destruction, and can be contacted as long as it is alive.

💡 Tip

Key points to remember

  • Object = identity + state + behaviour. Class = blueprint (user-defined type).
  • Abstraction hides complexity; encapsulation hides data (data hiding with private).
  • Inheritance gives reusability; polymorphism = one name, many forms.
  • Compile-time polymorphism: overloading. Run-time polymorphism: virtual functions (dynamic binding).
  • Message passing = object name + function name + arguments.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Explain the basic concepts of object oriented programming with suitable examples. (10 Marks)
  • Write: all eight concepts, a definition and a one-line example for each; the class vs object table; one small program.
  • Q2. Differentiate between data abstraction and encapsulation. (5 Marks)
  • Write: the comparison table, the car example and the private balance example.
  • Q3. What is polymorphism? Distinguish between compile-time and run-time polymorphism. (5 Marks)
  • Write: the definition, the two-row table, an overloading example and the virtual draw() example.
  • Q4. What is dynamic binding? How is it achieved in C++? (2.5 Marks)
  • Q5. Differentiate between a class and an object. (2.5 Marks)