CENG201 - Assignment 1: Implementing a Custom Pen and Shape Drawing System
In this assignment, you will practice classes, header files, inheritance, and composition. You will
1. Implement a `Pen` class with specific functionality and constraints.
2. Extend `Pen` to create an `IncrementalPen` class that advances ink letters.
3. Use `Pen` within a `Shape` system to create drawable shapes.
Important Points before beginning:
- Write your name to all your source codes.
- Use comments and proper indentation.
- All attributes should be private.
- Use header files for declarations and cpp files for implementations.
- You can use include guards (#ifndef #define statements) to avoid multiple definitions.
Requirements
Part 1: `Pen` Class
1. Each class should have its own header (`.h`) and implementation (`.cpp`) files.
- `Pen.h` and `Pen.cpp`
- `IncrementalPen.h` and `IncrementalPen.cpp`
2. Pen Class Specifications:
Attributes:
- char ink: The ink character used for drawing. Must be a lowercase letter (`a` to `z`). If an invalid character is given, default it to `'a'`.
Methods:
- Pen(char inkChar = 'a')`: Constructor that sets `ink` to a lowercase letter. If the provided `inkChar` is invalid, default to `'a'`.
- void write(int n): Prints the `ink` character `n` times.
- void space(int n): Prints `n` spaces.
- void setBold(bool bold): Converts `ink` to uppercase if `bold` is `true`, lowercase if `false`.
E.g., if the ink is `c`, when it is bold it should print `C`.
Part 2: `IncrementalPen` Class
1. Extend the `Pen` class to create `IncrementalPen`.
2. Specifications:
Attributes: Inherits `ink` from `Pen`.
Methods:
- IncrementalPen(char inkChar = 'a'): Constructor that initializes `ink` with validation.
- Override write(int n): After each character print, increment `ink` to the next letter. If `ink` reaches `'z'`, it wraps around to `'a'`. For instance, if ink is e and your call write(4). It should print efgh
Part 3: Shape System
1. Create Classes:
- `Shape` (abstract base class): `Shape.h` and `Shape.cpp`
- `Rectangle` and `Circle` (subclasses of `Shape`): `Rectangle.h`, `Rectangle.cpp`, `Circle.h`,
`Circle.cpp`
- `Square` (subclass of `Rectangle`): `Square.h` and `Square.cpp`
2. Specifications:
- Shape:
- Contains a `Pen` object.
- draw(): Abstract method to be overridden by concrete shapes.
- Rectangle:
- Attributes: int width, int height.
- Constructor initializes `width`, `height`, and `Pen`.
- draw(): Uses `Pen` to draw a rectangle of `width` x `height`. It should be an empty (non-filled) rectangle.
If the ink in Pen is b, width is 4 and height is 3 it should draw the following shape.
bbbb
b b
bbbb
- Circle:
- Attribute: int radius.
- Constructor initializes `radius` and `Pen`.
- draw(): Draws a rough ASCII circle shape. It should be a non-filled circle.
How to draw the circle: you should just draw the positions where distance to center = radius Distance to center will be calculated as shown in Figure 1. Check a square region of size
2*radius+1 around the center
Figure 1: calculating distance to center
- Square:
- Inherits from `Rectangle`.
- Constructor Square(int size) initializes `width` and `height` to the same size value.
Implementation Instructions
File Structure
Your project structure should look like this:
Project/
Example Test Cases
Place the following code in `main.cpp` to test your classes:
#include
#include "Pen.h"
#include "IncrementalPen.h"
#include "Rectangle.h"
#include "Circle.h"
#include "Square.h"
int main() {
// Test Pen and IncrementalPen
Pen p('c');
p.write(5); // Expected: "ccccc"
std::cout << std> p.setBold(true);
p.write(3); // Expected: "CCC"
std::cout << std>
IncrementalPen ip('x');
ip.write(5); // Expected: "xyzab" (wraps around)
std::cout << std>
// Test Rectangle, Circle, Square with Pen
Rectangle rect(p, 5, 3);
rect.draw(); // Expected: Rectangle shape
Circle circ(p, 3);
circ.draw(); // Expected: Circle shape
Square sq(ip, 4);
sq.draw(); // Expected: Square shape, with incremental pen
return 0;
}
Compilation Instructions
If using the command line, navigate to the project directory and use the following commands to compile: (on windows, otherwise just do not write .exe)
g++ Pen.cpp IncrementalPen.cpp Shape.cpp Rectangle.cpp Circle.cpp Square.cpp main.cpp -o main.exe
To run:
main.exe
Submission Checklist
- Separate interface and implementation to header and cpp files for all your classes.
- Dont forget having your name and id on top of all your source files.
- Do not have any incorrect class or method names because in such a case your program would fail in test cases.
- Use proper comments and indentation in your codes. We will check it.
- Put all of your source code into directory called assignment1, compress it to a zip file and submit. Do not have the any other files in this directory.
Are you struggling to keep up with the demands of your academic journey? Don't worry, we've got your back!
Exam Question Bank is your trusted partner in achieving academic excellence for all kind of technical and non-technical subjects. Our comprehensive range of academic services is designed to cater to students at every level. Whether you're a high school student, a college undergraduate, or pursuing advanced studies, we have the expertise and resources to support you.
To connect with expert and ask your query click here Exam Question Bank