0% found this document useful (0 votes)
8 views2 pages

Object-Oriented Programming Assignment

The assignment requires students to create a Shape class in C++ with specific member functions and data members, including constructors, destructors, and methods to calculate area and perimeter. Students must derive Circle, Rectangle, and Square classes from Shape, implementing additional functionalities such as copy constructors and operator overloading. The assignment must be submitted as a zip file on CMS/LMS by April 25, 2025, and plagiarism will result in severe consequences.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Object-Oriented Programming Assignment

The assignment requires students to create a Shape class in C++ with specific member functions and data members, including constructors, destructors, and methods to calculate area and perimeter. Students must derive Circle, Rectangle, and Square classes from Shape, implementing additional functionalities such as copy constructors and operator overloading. The assignment must be submitted as a zip file on CMS/LMS by April 25, 2025, and plagiarism will result in severe consequences.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment 2 (Graded)

COSC-1102-Object Oriented Programming


Course Instructor: Dr. Nasir Mehmood
Submission Deadline: April 25, 2025, 11:59:59 PM
Submission Method: Submit on CMS/LMS

Important Instructions
ˆ The assignments must be submitted online using CMS/LMS. Submission
by email or any other method will not be entertained.
ˆ Copying assignment from others or submitting assignment that is com-
pleted by some other students can result in zero-marks in the assignment
or failing the course depending on the severity of the plagiarism. (The
case may be forwarded to disciplinary committee for further action)
ˆ Do not wait for the last day to start. Start as early as possible.

ˆ Write complete code in single file ”[Link]” for example


([Link]) and create a zip file ”[Link]”.
Make sure there is no folder or any other file in the zip file. Upload
”[Link]” on CMS

Description
Create a class Shape that will represent a geometrical shape. Add two data
members, name as char pointer (string) and an integer, numParams,for number
of parameters. For example, A circle has only one parameter, the radius, a
rectangle has two parameters width and height, a triangle has two parameters,
base length and height. a square has one parameter, the side of a length. Add
following member functions of the base class:

i) A default constructor that will initialize the pointer to NULL and number
of parameters to zero.

ii) A parameterized constructor that receives a string (as a character pointer)


and an integer for the number of parameters. The constructor first dynam-
ically allocates memory for the name variable and then copies values of the
parameters to the member variables.

iii) A destructor to deallocate any memory. Make sure the pointer is not
null before deallocating the memory. (Figure out yourself if the destructor

1
should be virtual or not. And write your justification as comments in the
cpp file.)
iv) A member function FindArea that should calculate and return the area of
the shape. (Figure out yourself if the function sould be non-virtual, virtual
or pure virtual. Write your justification as comments in the cpp file)

v) A member function FindPerimeter that should calculate and return the


perimeter of the shape. (Figure out yourself if the function sould non-
virtual, virtual or pure virtual. Write your justification as comments in the
cpp file)

Derive three classes from the Shape class namely, Circle, Rectangle and
Square. Add one double type member variable in the circle class for the radius
of the circle. Add two double type member variables width and height in the
Rectangle class. Add one double type member variable sideLength in the square
class. It is up to you to decide appropriate constructors and a destructor in each
of the three classes. In the destructor just add a display line e.g. ”Destructor
of Circle”. Override the FindeArea and FindPerimeter methds in each of the
derived classes that calculate and return the area and perimeter of the given
shape.
Furthermore, add following functionality.
i) Add copy constructors in the base class as well as in each of the derived
classes.
ii) Overload the assignment operator in the base as well as derived classes.

iii) Overload insertion operator to display parameters of each of the derived


classes. For example, cout<<Circle; should display parameters of the Cir-
cle.
Write main function to test your code completely.

Common questions

Powered by AI

Derived classes should have data members specific to their shape properties: Circle with a radius, Rectangle with width and height, and Square with sideLength. Each should override FindArea and FindPerimeter to perform calculations specific to the shape. Constructors should initialize all necessary values, and destructors should handle cleanup, even if just displaying a message. By designing with future extensibility in mind, such as flexibility to add new shapes, the structure remains robust and adaptable .

Submitting in a single file ensures consistency and simplifies the evaluation process by reducing potential issues with file navigation and environment setup. Evaluators can quickly assess the code without having to deal with complex directories or additional files, streamlining the process while minimizing errors due to incorrect or mixed submissions .

Constructors and destructors are crucial for managing dynamic memory allocation in the Shape class. The default constructor initializes the name pointer to NULL and numParams to zero, while the parameterized constructor allocates memory dynamically for the name variable. The destructor must deallocate memory, checking that the pointer is not null, to avoid memory leaks. It's necessary to decide if a virtual destructor is needed based on whether the class is intended for use through base class pointers .

Starting the assignment early aligns with best practices in project management that emphasize risk management, buffer time for unforeseen issues, and iterative improvement. Early start allows thorough testing, incremental progress, and prevents rush-induced errors. This parallels agile methodologies in software development, promoting flexible, iterative processes for better quality outputs .

Implementing a destructor in the Circle class that merely displays a message may not align with best practices unless it also ensures resource management. While displaying messages aids learning and debugging, best practices entail resource deallocation to prevent memory leaks. The current implementation could be more illustrative but should maintain focus on practical resource management .

Requiring students to justify implementation choices in comments fosters critical thinking, deeper understanding, and self-reflection on programming concepts such as dynamic memory management and polymorphism. It encourages students to articulate their logic and reasoning, promoting a solid grasp of object-oriented principles. This practice enhances their ability to evaluate and defend design decisions, a crucial skill in software development .

Copy constructors and overloaded assignment operators enable proper copying of class instances, especially those with dynamic memory. They ensure deep copying rather than shallow copying, avoiding issues like double deletes. In the context of inheritance, ensuring these are implemented correctly prevents slicing and maintains correct behavior for derived class objects when treated as instances of the base class .

The second assignment must be submitted online using CMS/LMS by April 25, 2025, at 11:59:59 PM. Submission via email or any other method is not accepted. Plagiarism, such as copying assignments from others, can result in zero marks or even failing the course, with severe cases being forwarded to the disciplinary committee .

Overloading the insertion operator (<<) allows for easy output of object properties, enhancing usability and debugging. For derived classes like Circle, using cout<<Circle should display the circle's parameters, such as its radius value. This representation makes data output more intuitive and seamlessly integrates with standard output streams .

Using virtual functions for FindArea and FindPerimeter allows derived classes like Circle, Rectangle, and Square to override these methods providing specific implementations for each shape. This ensures that when a shape object is dealt with via a base class pointer or reference, the appropriate derived class method is called. Pure virtual functions could be justified if it is expected that no meaningful calculations can be performed on the base class instances themselves .

You might also like