This repository contains a learning-based implementation of an object-oriented shape simulator using C++. The project was designed to develop a strong understanding of class design, inheritance, polymorphism, modular code structure, compilation automation, and 2D visualization using SFML.
Resources: [learncpp.com Chapters 14 & 15]
Key Learnings:
- Classes allow bundling data + behavior.
- Understood access specifiers (
public
,private
,protected
). - Constructors, destructors, and initialization lists.
- Created member methods like
getLength()
,getBreadth()
,getArea()
.
Takeaway: A well-structured class can simplify program logic and extend easily through inheritance.
Resources: [learncpp.com Chapters 2.11, 2.12, 7.8, 7.10]
Key Learnings:
- Separated interface (.h) and implementation (.cpp).
- Header guards to prevent redefinitions.
- Used
#include
to link files together.
Takeaway: Separation of concerns improves readability and debugging in large projects.
Resources: [Makefile Tutorials]
Key Learnings:
- Automated compilation using
Makefile
. - Only modified files are recompiled.
- Commands like
make
,make clean
simplify workflow.
Takeaway: Automating builds is essential in real-world projects to save time and prevent errors.
- Created class with
length
,breadth
. - Added methods:
getLength()
,getBreadth()
,getArea()
,isSquare()
.
- Inherited from
Rectangle
. - Ensured both sides are equal.
- Overridden relevant methods.
- Introduced abstract base class
Shape
. - Derived:
Rectangle
,Square
,Circle
. - Used polymorphism to call derived methods using
Shape*
.
- Created
Ellipse
withxRadius
,yRadius
. - Made
Circle
inherit fromEllipse
with equal radii.
Each shape class implements int getShape()
:
Shape | ID |
---|---|
Rectangle | 0 |
Square | 1 |
Circle | 2 |
Ellipse | 3 |
Resources: UML introductory videos, tools like draw.io, StarUML, PlantUML.
Key Learnings:
- UML (Unified Modeling Language) helps in visualizing and planning class relationships before coding.
- Diagrams created:
- Class diagrams showing inheritance between
Shape
,Rectangle
,Square
,Ellipse
,Circle
. - Attributes and public method signatures.
- Class diagrams showing inheritance between
- Benefits:
- Easier debugging.
- Cleaner and modular design.
- Reduces interdependencies and simplifies extensions.
- Represented following relationships:
Shape
is an abstract base class.Rectangle
,Square
,Ellipse
, andCircle
inherit fromShape
.Circle
inherits fromEllipse
.- Optional: A
Coordinate
class included to handle positions.
- Each class includes:
- Attributes: dimensions (length, breadth, radius), position.
- Methods:
getShape()
,getArea()
,translate()
, etc.
-
Class:
Canvas
- Stores shapes using:
std::vector<Shape*> shapes;
- Method:
void addShape(Shape* shape);
to add a shape to canvas. - Method:
void displayCanvas();
renders all shapes in a graphical window.
- Stores shapes using:
-
Display Logic with SFML:
- Each shape implements a
draw(sf::RenderWindow&)
method. Canvas::displayCanvas()
loops over all shapes and calls theirdraw()
method.- Promotes separation of concerns: rendering handled by each shape individually.
- Each shape implements a
Tools Used:
- SFML Library for 2D graphics.
- Installed using:
sudo apt install libsfml-dev