Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hands on OOP submission by Caesar Yoel Christian Pasaribu #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Caesar Yoel Christian Pasaribu_President University/main1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//OOP_Inheritance, Polymorphism, and SOLID - Caesar Yoel Christian Pasaribu_President University

#include <iostream>
#include <vector>

// Abstract class for animals
class Animal {
protected:
std::string name;
int age;
public:
Animal(std::string name, int age) : name(name), age(age) {}
virtual void makeNoise() = 0;
};

// Dog class inherited from Animal
class Dog : public Animal {
public:
Dog(std::string name, int age) : Animal(name, age) {}
virtual void makeNoise() { std::cout << "Woof woof" << std::endl; }
void fetch() { std::cout << name << " is fetching." << std::endl; }
};

// Cat class inherited from Animal
class Cat : public Animal {
public:
Cat(std::string name, int age) : Animal(name, age) {}
virtual void makeNoise() { std::cout << "Meow" << std::endl; }
void climb() { std::cout << name << " is climbing." << std::endl; }
};

// Lion class inherited from Animal
class Lion : public Animal {
private:
std::string maneColor;
public:
Lion(std::string name, int age, std::string maneColor) : Animal(name, age), maneColor(maneColor) {}
virtual void makeNoise() { std::cout << "Roar" << std::endl; }
void hunt() { std::cout << name << " is hunting." << std::endl; }
};

// Using the SOLID principles to create an AnimalManager class that is open for extension and closed for modification
class AnimalManager {
private:
std::vector<Animal*> animals;
public:
void addAnimal(Animal* animal) { animals.push_back(animal); }
void makeNoiseAll() {
for(auto animal : animals) {
animal->makeNoise();
}
}
};

int main() {
AnimalManager manager;
manager.addAnimal(new Dog("Fido", 2));
manager.addAnimal(new Cat("Whiskers", 3));
manager.addAnimal(new Lion("Simba", 4, "golden"));
manager.makeNoiseAll();
}
57 changes: 57 additions & 0 deletions Caesar Yoel Christian Pasaribu_President University/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//OOP_Interfaces, Abstract Classes, and Design Patterns - Caesar Yoel Christian Pasaribu_President University

#include <iostream>
#include <vector>

// Interface for shape
class Shape {
public:
virtual void draw() = 0;
};

// Abstract class for 2D shapes
class TwoDShape : public Shape {
protected:
double width;
double height;
public:
TwoDShape(double width, double height) : width(width), height(height) {}
virtual double getArea() = 0;
};

// Rectangle class implementing TwoDShape
class Rectangle : public TwoDShape {
public:
Rectangle(double width, double height) : TwoDShape(width, height) {}
virtual void draw() { std::cout << "Drawing rectangle...\n"; }
virtual double getArea() { return width * height; }
};

// Circle class implementing TwoDShape
class Circle : public TwoDShape {
private:
double radius;
public:
Circle(double radius) : TwoDShape(radius, 0), radius(radius) {}
virtual void draw() { std::cout << "Drawing circle...\n"; }
virtual double getArea() { return 3.14159 * radius * radius; }
};

// Using the factory design pattern to create Shape objects
class ShapeFactory {
public:
static Shape* createShape(std::string type, double width, double height) {
if(type == "Rectangle") return new Rectangle(width, height);
if(type == "Circle") return new Circle(width);
return nullptr;
}
};

int main() {
Shape* rectangle = ShapeFactory::createShape("Rectangle", 4, 5);
rectangle->draw();
std::cout << "Area of rectangle: " << dynamic_cast<TwoDShape*>(rectangle)->getArea() << std::endl;
Shape* circle = ShapeFactory::createShape("Circle", 3, 4);
circle->draw();
std::cout << "Area of circle: " << dynamic_cast<TwoDShape*>(circle)->getArea() << std::endl;
}
95 changes: 95 additions & 0 deletions Caesar Yoel Christian Pasaribu_President University/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//OOP_Inheritance, Interfaces, Polymorphism, Design Patterns, and SOLID - Caesar Yoel Christian Pasaribu_President University

#include <iostream>
#include <vector>
#include <cmath>
#include <math.h>

// Interface
class Shape {
public:
virtual double area() = 0;
};

// Abstract class
class Polygon : public Shape {
protected:
std::vector<double> sides;

public:
Polygon(const std::vector<double>& s) : sides(s) {}

double perimeter() {
double p = 0;
for (auto side : sides) {
p += side;
}
return p;
}
};

// Inheritance
class Rectangle : public Polygon {
public:
Rectangle(double w, double h) : Polygon({w, h, w, h}) {}

double area() override {
return sides[0] * sides[1];
}
};

class Triangle : public Polygon {
public:
Triangle(double a, double b, double c) : Polygon({a, b, c}) {}

double area() override {
double p = perimeter() / 2;
double a = sides[0], b = sides[1], c = sides[2];
return sqrt(p * (p - a) * (p - b) * (p - c));
}
};

class Circle : public Shape {
private:
double radius;

public:
Circle(double r) : radius(r) {}

double area() override {
return 3.14 * radius * radius;
}
};

// Design pattern
class ShapeFactory {
public:
static Shape* createShape(std::string type, std::vector<double> args) {
if (type == "rectangle") {
return new Rectangle(args[0], args[1]);
} else if (type == "triangle") {
return new Triangle(args[0], args[1], args[2]);
} else if (type == "circle") {
return new Circle(args[0]);
} return 0;
}
};

int main() {
// SOLID
std::vector<Shape*> shapes;
shapes.push_back(ShapeFactory::createShape("rectangle", {3, 4}));
shapes.push_back(ShapeFactory::createShape("triangle", {3, 4, 5}));
shapes.push_back(ShapeFactory::createShape("circle", {5}));

for (auto shape : shapes) {
std::cout << shape->area() << std::endl;
}

// Clean up
for (auto shape : shapes) {
delete shape;
}

return 0;
}
74 changes: 74 additions & 0 deletions Caesar Yoel Christian Pasaribu_President University/main4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//OOP_Inheritance, Interfaces, Polymorphism, Design Patterns, and SOLID - Caesar Yoel Christian Pasaribu_President University

#include <iostream>
#include <vector>

// Interface for shape objects
class Shape {
public:
virtual double area() = 0;
};

// Abstract class for 2D shapes
class TwoDShape: public Shape {
public:
TwoDShape(double w, double h) : width_(w), height_(h) {}
double width() const { return width_; }
double height() const { return height_; }
void resize(double w, double h) { width_ = w; height_ = h; }
private:
double width_;
double height_;
};

// Class for rectangles
class Rectangle: public TwoDShape {
public:
Rectangle(double w, double h) : TwoDShape(w, h) {}
double area() { return width() * height(); }
};

// Class for triangles
class Triangle: public TwoDShape {
public:
Triangle(double b, double h) : TwoDShape(b, h) {}
double area() { return width() * height() / 2; }
};

// Design pattern: Composite pattern
class ShapeGroup: public Shape {
public:
void addShape(Shape* shape) { shapes_.push_back(shape); }
double area() {
double area = 0;
for (auto shape : shapes_) {
area += shape->area();
}
return area;
}
private:
std::vector<Shape*> shapes_;
};

// SOLID principle: Single responsibility
class ShapeCalculator {
public:
double calculateArea(Shape* shape) { return shape->area(); }
};

int main() {
ShapeCalculator calculator;

Rectangle rect(4, 5);
std::cout << "Rectangle area: " << calculator.calculateArea(&rect) << std::endl;

Triangle tri(4, 5);
std::cout << "Triangle area: " << calculator.calculateArea(&tri) << std::endl;

ShapeGroup group;
group.addShape(&rect);
group.addShape(&tri);
std::cout << "Group area: " << calculator.calculateArea(&group) << std::endl;

return 0;
}