0% found this document useful (0 votes)
19 views

Assignment

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)
19 views

Assignment

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
You are on page 1/ 5

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');

record1.printRecord();

// Output:

// StudentId : J17S/MSA/1265/2007

// Units : 8

// Gender : M

StudentRecord record2;

record2.printRecord();

// 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

rect1.setWidth(100);

rect1.setHeight(25);

rect1.computeArea();

rect1.computePerimeter();

rect2.computeArea();

rect2.computePerimeter();

cout << "Rectangle 1 area: " << rect1.getArea() << endl;

cout << "Rectangle 1 perimeter: " << rect1.getPerimeter() << endl;

cout << "Rectangle 2 area: " << rect2.getArea() << endl;

cout << "Rectangle 2 perimeter: " << rect2.getPerimeter() << endl;

return 0;

You might also like