OOP
Object Oriented Programming
OOP stands for Object-Oriented Programming, which is a programming paradigm that focuses on organizing code around objects, which are instances of classes. OOP promotes the use of encapsulation, inheritance, and polymorphism to structure and design software.
It emphasizes the creation of reusable and modular code by breaking down complex systems into smaller, self-contained objects that interact with each other. OOP languages, such as Java, C++, and Python, provide features and constructs to implement OOP principles effectively.
OOD
Object Oriented Design
On the other hand, OOD stands for Object-Oriented Design, which is a broader concept that encompasses the process of designing software systems using object-oriented principles. OOD involves the high-level design decisions and architectural considerations taken during the initial phases of software development.
It includes identifying objects, their relationships, responsibilities, and interactions to create a well-structured and maintainable system. OOD focuses on conceptualizing the system's structure, defining classes, and designing the overall architecture before moving into the implementation phase.
Let's understand it by a simple example.
Suppose we want to create a program to simulate a zoo. We'll start with OOD, which involves designing the system before writing any code. In this stage, we would identify the key objects and their relationships. For a zoo, we might have objects like "Zoo," "Animal," "Lion," and "Tiger." The Zoo object would contain a collection of Animal objects, which can be either Lion or Tiger. We would define the properties and behaviors of each object, such as the Animal's name, age, and sound it makes, and the Zoo's capacity and methods to add or remove animals.
#include <iostream>
#include <vector>
class Animal {
protected:
std::string name;
int age;
public:
Animal(const std::string& name, int age) : name(name), age(age) {}
virtual void make_sound() {
// Placeholder for the specific sound each animal makes
}
};
class Lion : public Animal {
public:
Lion(const std::string& name, int age) : Animal(name, age) {}
void make_sound() override {
std::cout << "Roar!" << std::endl;
}
};
class Tiger : public Animal {
public:
Tiger(const std::string& name, int age) : Animal(name, age) {}
void make_sound() override {
std::cout << "Growl!" << std::endl;
}
};
class Zoo {
private:
int capacity;
std::vector<Animal*> animals;
public:
Zoo(int capacity) : capacity(capacity) {}
void add_animal(Animal* animal) {
if (animals.size() < capacity) {
animals.push_back(animal);
} else {
std::cout << "Zoo is full!" << std::endl;
}
}
void remove_animal(Animal* animal) {
auto it = std::find(animals.begin(), animals.end(), animal);
if (it != animals.end()) {
animals.erase(it);
}
}
void display_animals() {
for (Animal* animal : animals) {
std::cout << animal->name << " (" << typeid(*animal).name() << "), Age: " << animal->age << std::endl;
}
}
};
int main() {
Zoo zoo(3);
Lion lion("Simba", 5);
Tiger tiger("Sher Khan", 7);
zoo.add_animal(&lion);
zoo.add_animal(&tiger);
zoo.display_animals();
lion.make_sound();
tiger.make_sound();
return 0;
}
The Animal class is a base class with the name and age properties and a virtual make_sound function. The Lion and Tiger classes inherit from Animal and override the make_sound function with their specific sound implementations.
The Zoo class has an add_animal function that accepts a pointer to an Animal object and manages a vector of animal pointers. The rest of the code demonstrates how to create instances of animals, add them to the zoo, and call the appropriate methods.
Top comments (0)