oop2.0
oop2.0
Object in OOP
**Definition**: An object is an instance of a class that represents a real-world entity. It contains data (attributes)
and methods (functions) that define its behavior.
**Example**:
An object of a Car class could have attributes like color and a method like `drive()`.
```cpp
Car myCar;
myCar.color = "Red";
myCar.drive();
2. Class in OOP
Definition: A class is a blueprint or template for creating objects. It defines the
attributes (data members) and behaviors (methods) that the objects will have.
Example:
A Student class could define attributes like name and age and a method like display().
cppCopy code
class Student {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
3. Encapsulation
Definition: Encapsulation is the concept of bundling data (attributes) and methods
(functions) that operate on the data into a single unit (class). It also restricts direct
access to some of the object's components.
Example:
Using private members and public methods in a BankAccount class ensures controlled
access.
cppCopy code
class BankAccount {
private:
int balance;
public:
void deposit(int amount) { balance += amount; }
};
4. Inheritance
Definition: Inheritance allows a class (child) to inherit attributes and methods from
another class (parent). It promotes code reuse and represents an "is-a" relationship.
Example:
A Dog class inherits common features from an Animal class.
cppCopy code
class Animal {
public:
void eat() { cout << "Eating..." << endl; }
};
5. Polymorphism
Definition: Polymorphism allows objects of different classes to be treated as objects of
a common base class. It enables methods to behave differently based on the object that
invokes them.
Example:
A Dog and a Cat class can override the sound() method from a base Animal class.
cppCopy code
class Animal {
public:
virtual void sound() { cout << "Animal sound" << endl; }
};
6. Abstraction
Definition: Abstraction hides unnecessary details and shows only essential features. It
simplifies complex systems by modeling classes appropriate to the problem.
Example:
A Calculator class exposes only the add() method without revealing how addition is
performed.
cppCopy code
class Calculator {
public:
int add(int a, int b) { return a + b; }
};
7. Constructor
Definition: A constructor is a special method that is automatically called when an object
is created. It is used to initialize the object's data members.
Example:
A Car class initializes its brand attribute when an object is created.
cppCopy code
class Car {
public:
string brand;
Car(string b) { brand = b; }
};
8. Destructor
Definition: A destructor is a special method that is automatically called when an object
is destroyed. It is used to release resources.
Example:
A Student class destructor displays a message when an object is destroyed.
cppCopy code
class Student {
public:
~Student() { cout << "Object destroyed" << endl; }
};
9. Friend Function
Definition: A friend function is a function that is not a member of a class but can access
its private and protected members.
Example:
A friend function printLength() accesses the private member length of a Box class.
cppCopy code
class Box {
private:
int length;
public:
friend void printLength(Box b);
};
13. Templates
Definition: Templates allow writing generic functions and classes that can work with
any data type.
Example:
A generic add() function can work with integers, floats, or other data types.
cppCopy code
template <typename T>
T add(T a, T b) { return a + b; }
17. Association
Definition: Association is a relationship between two classes where one class uses or
interacts with another class.
Example:
A Driver class interacts with a Car class.
cppCopy code
class Driver {
public:
void drive(Car car) { cout << "Driving " << car.model << endl; }
};
18. Aggregation
Definition: Aggregation is a "has-a" relationship where one class contains objects of
another class, but the contained objects can exist independently.
Example:
A University class contains Department objects that can exist independently.
cppCopy code
class Department {
public:
string name;
};
class University {
public:
vector<Department> departments;
};
19. Composition
Definition: Composition is a strong "has-a" relationship where the contained objects
cannot exist independently of the container.
Example:
A Car class contains an Engine object that cannot exist without the Car.
cppCopy code
class Engine {
public:
string type;
};
class Car {
public:
Engine engine; // Engine cannot exist without Car
};
```cpp
class Base {
public:
virtual ~Base() { cout << "Base destructor" << endl; }
};
int Counter::count = 0;
ifstream inFile("example.txt");