unit 3 opps
1️⃣ Relationships in OOP
In Object-Oriented Programming, objects and classes are connected through relationships.
Main types:
-
Inheritance (IS-A)
-
Association (Uses/Has-A)
-
Aggregation (Has-A, weak form)
-
Composition (strong Aggregation – optional mention)
2️⃣ Inheritance
🔹 Definition
Inheritance is a mechanism by which one class acquires the properties and behavior of another class.
👉 It promotes code reusability.
🔹 Purpose of Inheritance
✅ Code Reuse
✅ Extensibility
✅ Logical hierarchy
✅ Polymorphism support
✅ Reduces redundancy
🔹 Syntax in C++
class Parent {
// base class
};
class Child : public Parent {
// derived class
};
🔹 “IS-A” Relationship
Inheritance represents an IS-A relationship.
Example:
-
Dog IS-A Animal
-
Car IS-A Vehicle
So, Dog inherits from Animal.
🔹 Example in C++
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating\n";
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking\n";
}
};
int main() {
Dog d;
d.eat(); // inherited
d.bark(); // own function
}
Here:
-
DogIS-AAnimal
🔹 Types of Inheritance in C++
1️⃣ Single Inheritance
One base → one derived
class A {};
class B : public A {};
2️⃣ Multiple Inheritance
One derived → multiple base
class A {};
class B {};
class C : public A, public B {};
3️⃣ Multilevel Inheritance
A → B → C
class A {};
class B : public A {};
class C : public B {};
4️⃣ Hierarchical Inheritance
One base → multiple derived
class A {};
class B : public A {};
class C : public A {};
5️⃣ Hybrid Inheritance
Combination of multiple types.
3️⃣ Association
🔹 Definition
Association represents a relationship where one class uses another class.
👉 It represents a USES-A or HAS-A relationship.
Example:
-
Student uses Library
-
Teacher uses Marker
🔹 Example in C++
class Engine {
public:
void start() {
cout << "Engine started\n";
}
};
class Car {
private:
Engine e; // Car HAS-A Engine (Association)
public:
void startCar() {
e.start();
}
};
Here:
Car uses Engine.
4️⃣ Aggregation
🔹 Definition
Aggregation is a special type of association representing a weak HAS-A relationship.
👉 The contained object can exist independently.
Example:
-
Department has Teachers
-
Teacher can exist without Department
🔹 Example
class Teacher {
public:
string name;
};
class Department {
public:
Teacher *t; // Aggregation
};
If Department is destroyed, Teacher can still exist.
🔹 Difference: Association vs Aggregation
| Feature | Association | Aggregation |
|---|---|---|
| Relationship | Uses | Has-A |
| Ownership | No ownership | Weak ownership |
| Life Cycle | Independent | Independent |
5️⃣ Abstract Classes
🔹 Definition
A class that contains at least one pure virtual function is called an abstract class.
👉 Cannot create object of abstract class.
🔹 Syntax
class Shape {
public:
virtual void area() = 0; // pure virtual
};
🔹 Example
class Shape {
public:
virtual void area() = 0;
};
class Circle : public Shape {
public:
void area() {
cout << "Area of Circle\n";
}
};
int main() {
Circle c;
c.area();
}
You cannot do:
Shape s; // ERROR
🔹 Purpose of Abstract Class
✅ To achieve abstraction
✅ To define common interface
✅ To support runtime polymorphism
6️⃣ Interfaces in C++
C++ does not have a separate interface keyword like Java.
But we create interface using:
👉 Abstract class with all pure virtual functions.
🔹 Example of Interface
class Printable {
public:
virtual void print() = 0;
};
class Document : public Printable {
public:
void print() {
cout << "Printing document\n";
}
};
Here:
Printable works like an interface.
🔹 Difference: Abstract Class vs Interface (C++ Context)
| Abstract Class | Interface |
|---|---|
| May contain normal functions | Only pure virtual |
| May contain variables | Typically no data |
| Partial abstraction | Full abstraction |
Comments
Post a Comment