PRESENTATION
TOPIC:
CLASS ITS RELATIONSHIPS, ABSTRACTION, AGGREGATION
AND COMPOSITION
What is class?
• A class is a blueprint used to create a object, combining data
and function in one unit.
• a class is a user defined data type that contains data member
function.
Syntax of class:
class ClassName {
private:
// data members (variables)
public:
// member functions (functions)
protected:
// optional (beech ka access level)
};
Access specifiers:
• Public :
(accessible from anywhere in the program)
• private :
( Cannot be accessed directly from outside)
• Protected :
( Accessible only within the class and its
derived (child) classes)
Object:
• An object is an instance of a class
• It represent the real-world entity and contains data and
function.
Syntax:
Student s1; // object of class Student
Data member:
• Data member are variables declared inside a class.
• They are used to store data of an object.
• Example: age, name, salary
Member function:
• member function are functions defined inside a class.
• They are used to perform operations on data member.
• example: setage(), display().
Global function:
• A global function is a function defined outside any class.
• 👉 It is independent of classes.
• Example:
void display() {
cout << "Hello from global function!" << endl;
}
Key point:
• Defined outside class.
• No object needed.
• Cannot directly access class private data.
What is abstraction?
• Abstraction is the process of hiding complex details and
showing only the important features.
• 👉 Focus: What it does, not how it works
• Example:
1. 🚗 Car
2. 📱 Mobile
Abstraction in programming:
• It is a key concept of OOP
• Helps to reduce complexity
• User only sees interface, not implementation
Types of abstraction:
1. Data Abstraction
• Hide data details
• Example: balance is private in Bank Account
2. Procedural Abstraction
• Hide logic in functions
• Example: sort() function
Code example:
#include <iostream>
using namespace std;
// abstract class
class shape {
public:
virtual double area() = 0; // pure virtual function
};
// derived class
class circle : public shape {
public:
double area() override {
return 3.14 * 5 * 5;
}
};
int main() {
circle c;
cout << [Link]();
return 0;
}
Composition:
• Composition is a strong relationship between classes.
• Has-A relationship.
Key point:
• Strong relationship
• Dependent object.
• Lifetime same
• If one object is destroy, second also destroy
Example:
• A house contains room.
• If the house destroyed, room also no longer exist.
Aggregation:
• Aggregation is a weak relationship between classes
• It also represents a “Has-A” relationship
• One class uses another class, but does not fully own it
Key point:
• Weak relationship
• Objects are independent
• Different lifetimes
• One object can exist without the other
Real-Life Example 🚗 Car and
Engine:
• A car has an engine
• If the car is destroyed, the
engine can still exist