Assignment
Assignment
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>
class StudentRecord {
private:
string studentId;
int units;
char gender;
public:
studentId = id;
units = u;
gender = g;
}
void printRecord() {
};
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() {
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.
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.
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() {
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 getArea and getPerimeter and display the area and perimeter of each rectangle .
Solution is ;
int main() {
rect1.setWidth(100);
rect1.setHeight(25);
rect1.computeArea();
rect1.computePerimeter();
rect2.computeArea();
rect2.computePerimeter();
return 0;