0% found this document useful (0 votes)
26 views5 pages

Student and Rectangle Class Implementation

The document describes two programming assignments. The first assignment involves creating a StudentRecord class with private data members for student ID, units, and gender. It includes two constructors, one with parameters and one without, and a printRecord method. The second assignment involves creating a Rectangle class with private data members for width, height, area, and perimeter. It includes default and parameterized constructors, setter and getter methods, and methods to compute area and perimeter.

Uploaded by

craggyahmed123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Student and Rectangle Class Implementation

The document describes two programming assignments. The first assignment involves creating a StudentRecord class with private data members for student ID, units, and gender. It includes two constructors, one with parameters and one without, and a printRecord method. The second assignment involves creating a Rectangle class with private data members for width, height, area, and perimeter. It includes default and parameterized constructors, setter and getter methods, and methods to compute area and perimeter.

Uploaded by

craggyahmed123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 1

A StudentRecord class maintains student records for the registration for the registrar. The attributes
include studentId as text, (all units registered in a semester) as integer and gender (female or male) as
character. For example, a student record has "J17S/MSA/1265/2007" as studentId ,8 as unit and 'M' as
gender.

I. Write a class StudentRecord with the attributes above as private data member elements.
Include in the class a definition of two constructors, one taking three parameters corresponding
to the three attributes and another taking no parameter. The first constructor should us
parameters to assign them to the corresponding data attributes and in the second constructor
assign the data attributes default values of your choice. Also include a method printRecord() the
displays student information as follows:

StudentId : J17S/MSA/1265/2007

Units :8

Gender: M

Solution is:

#include <iostream>

#include <string>

using namespace std;

class StudentRecord {

private:

string studentId;

int units;

char gender;

public:

StudentRecord(string id="", int u=0, char g=' ') {

studentId = id;

units = u;

gender = g;

}
void printRecord() {

cout << "StudentId : " << studentId << endl;

cout << "Units : " << units << endl;

cout << "Gender : " << gender << endl;

};

II. Write a main function that will create an object named Jmart with ID as
“J27S/MSA/2453/2008” ,unit as 16 and gender as ‘F’ and use this objects to call the method
printRecord().

Solution is;
int main() {

StudentRecord record1("J17S/MSA/1265/2007", 8, 'M');

[Link]();

// Output:

// StudentId : J17S/MSA/1265/2007

// Units : 8

// Gender : M

StudentRecord record2;

[Link]();

// Output:

// StudentId :

// Units : 0

// Gender :

return 0;

Assignment 2

a) Create a CLASS CALLED RECTANGLE with integer instance variables named width, height, area
and perimeter. Override the default constructor and use it to set these variables to zero. Also
include another constructor that accepts parameters for the width and height instance variables
and initializes the instance variables area and perimeter to zero.

Include the following accessor and mutator methods;

i. SetWidth which accepts a parameter that can be used to assign the width instance variable.
ii. setHeight which accepts a parameter that can be used to assign the height instance variable.
iii. getArea that returns the value of the instance variable
iv. getPerimeter that returns the value of the perimeter instance variable.

Also include the following custom methods:

i. computeArea which computes a rectangles area but does not return a value, and
ii. computePerimeter which computes a rectangles perimeter and also does not return a value.

The area of the rectangle is width multiplied by height, and the perimeter is twice the width plus height
(2 *(W+L)).

Solution is :

class Rectangle {

private:

int width;

int height;

int area;

int perimeter;

public:

// Default constructor

Rectangle() {

width = 0;

height = 0;

area = 0;

perimeter = 0;

// Parameterized constructor

Rectangle(int w, int h) {
width = w;

height = h;

area = 0;

perimeter = 0;

// Accessor methods

void setWidth(int w) {

width = w;

void setHeight(int h) {

height = h;

int getArea() {

return area;

int getPerimeter() {

return perimeter;

// Custom methods

void computeArea() {

area = width * height;

void computePerimeter() {
perimeter = 2 * (width + height);

};

b) Write the main function that declares two rectangle objects as follows:
 The first named rect1 is created using the overridden default constructor.
 The second named rect2 has a height of 5 and width of 10 assigned by the
parameterized constructor.

Use setWidth and setHeight to assign rect1 a width of 100 and height of 25 respectively .

Call computeArea and computePerimeter for each rectangle object.

Call getArea and getPerimeter and display the area and perimeter of each rectangle .

Solution is ;

int main() {

Rectangle rect1; // default constructor

Rectangle rect2(10, 5); // parameterized constructor

[Link](100);

[Link](25);

[Link]();

[Link]();

[Link]();

[Link]();

cout << "Rectangle 1 area: " << [Link]() << endl;

cout << "Rectangle 1 perimeter: " << [Link]() << endl;

cout << "Rectangle 2 area: " << [Link]() << endl;

cout << "Rectangle 2 perimeter: " << [Link]() << endl;

return 0;

Common questions

Powered by AI

Polymorphism could be used to extend the functionality of the StudentRecord or Rectangle classes by allowing these classes to have subclasses that override base class methods with specialized behavior. In the StudentRecord class, polymorphism might involve different sorts of student records (such as undergraduate vs. graduate students) with specific validation rules or presentation formats. For the Rectangle class, it could involve creating subclasses like Square or ColorRectangle, where the computeArea and computePerimeter methods might need to accommodate additional properties such as color or constraints regarding the equality of width and height .

Accessor (getArea and getPerimeter) and mutator (setWidth and setHeight) methods play crucial roles in preserving data integrity and enhancing class design. They provide controlled access to private attributes, ensuring external classes cannot directly alter width and height, thus maintaining the encapsulation integrity. They also enable validation and the decoupling of data manipulation from data access, offering flexibility for changes without affecting users' interaction with these class attributes .

Using constructors with parameters, as seen in the Rectangle class, allows for direct and flexible initialization of object attributes. It facilitates setting initial values at the time of object creation, which enhances code efficiency by reducing the need for multiple method calls post-instantiation. For example, the parameterized constructor sets width and height directly, providing immediate usability for methods that calculate area and perimeter .

Adding new attributes to the StudentRecord class could lead to challenges like increased complexity, potential for data inconsistency, and the need for additional validation logic. These might be mitigated through careful design by ensuring new attributes are encapsulated with appropriate get and set methods, maintaining attribute initialization within constructors, and updating print and validation methods accordingly. Designing for extension by using optional parameters and overloading constructors could also help manage complexity and ensure backward compatibility .

The use of default values in the StudentRecord constructor provides multiple advantages. It allows for the creation of objects without requiring specific initialization inputs, thereby increasing flexibility when dealing with cases where complete data is not immediately available. This approach also simplifies object creation in testing scenarios, where assumptions are made to verify functionality. It effectively balances between facilitating ease of use and allowing specific assignments when needed .

Encapsulation in the StudentRecord class enhances functionality by restricting direct access to its internal state and requiring all interactions to occur through well-defined methods. This is achieved by declaring the attributes studentId, units, and gender as private, thus preventing unauthorized manipulation of these values, ensuring that the values are only set and retrieved through controlled mechanisms, such as the constructor and printRecord method .

Object-oriented principles such as encapsulation, abstraction, and modularity optimize the main function's use of the Rectangle class by streamlining object initialization, manipulation, and result display. Encapsulation ensures each Rectangle instance manages its own data, while abstraction through methods like setWidth and setHeight abstracts complex operations into simple calls. Modularity allows rect1 and rect2 to be individually modified and processed, reducing code interdependency and increasing clarity and reuse potential in different contexts .

Abstraction in the main function using the StudentRecord object is demonstrated by the way specific complex interactions, such as setting attributes and printing results, are encapsulated within the class and exposed through simple, meaningful methods. This abstraction keeps the main function focused on high-level logic rather than low-level implementation details, as it simply creates the record1 and record2 objects and calls printRecord() to display their details .

To improve maintainability, the Rectangle class could benefit from refactoring its computation logic by separating concerns. Implement individual methods for calculating specific features like recalculating only when values change, which could decouple height and width setting from area and perimeter updates. Moreover, introducing constants for fixed values, like the formula multiplication factor in getPerimeter, would clarify intentions and simplify future changes. Additionally, using setter methods to automatically trigger area and perimeter computations on attribute updates might reduce code redundancy and increase reliability .

The computeArea and computePerimeter methods are central to the Rectangle class's functionality, consistently processing width and height attributes into meaningful metrics. They encapsulate calculation logic, enhancing maintainability by isolating computation functionality from users' input and data retrieval operations. This design ensures that any logic updates, such as recalibrating formulas or adding new computational features, remain localized within these methods, thus minimizing potential ripple effects on the rest of the codebase .

You might also like