OOP Theory Mid P2 Sol
OOP Theory Mid P2 Sol
Instructions:
Part-II should be solved on answer sheet. Write name & roll no on the question paper.
Paper is closed book & closed notes.
There are 3 questions in this part, attempt all.
Part- II (Subjective)
The university wants you to develop a C++ program for allocating roll numbers to new students and printing the
student’s fee. The problem description is given in following.
“Once a student is successful in getting admission, the name, father name, phone number, address of student is
noted by the admission officer. Admission office also allocate the next available six digit roll number to the
student and the batch number. The new roll numbers are created based on the count of students already
registered in the current year. Moreover, the roll number is suffixed by two digits year indicator. For example, if
50 new students are already registered in Spring 2022, then the next roll number to allocate is 220051 (two digit
year suffix+0+ (count of already registered students+1)).
The student is next required to choose which courses he/she is interested to study. Each course has a name,
course code and credit hours information. A student can register multiple courses. The student is next required
to pay the fee. A Fee slip is generated that contains the students information, the current semester information
and the courses information for which the fee needs to be deposited. Currently university charges Rs. 12000 per
credit hour for all courses and for all semesters. Note that each Fee slips has an automatically generated unique
reference number. Reference numbers are incremental i.e. 1st fee slip will have reference# 1, next will have
reference#2 and so on. ”
I. Identify the required entities with their attributes & behaviors that needs to be implemented to solve the
problem [5]
II. Draw the UML class diagram of each class indicating all its members [10]
III. Implement UML diagram in terms of C++ classes [15]
Page 1 of 10
Solution:
I.
Entities Attributes Behavior
Student name, father name, phone number, address, getName, getFatherName, getPhoneNumber,
rollNumber, courses registered, semester, getAddress, getRollNumber,
year, stdCnt getCoursesRegistered, generateRollNo
Course Course name, course code and credit hours getCourseName, getCourseCode,
getCreditHours
II.
Course
Student
- courseName: string
- name : string
- code: int
- fname: string
- credits: short
- phonceNo: int
- address: string + getCourseName(): string
-rollNo: string +getCode(): int
-semester: short +getCredits(): short
-year: short
+ setCourseName(string): void
-generateRollNo(): string +setCode(int): void
+setCredits(short): void
+ stdCnt: int
+Course(string,int,short):
+Student(string,string,int,string,
int,short):
+ getName() : string
+ getFname(): string
+ getPhonceNo(): int
+ getAddress(): string
+ getRollNo(): string
+getSemester(): short
+getYear(): short
+ ~Student():
+… set methods..
Page 2 of 10
FeeSlip
- referenceNo: int
- stdInfo: Student*
- coursesInfo: Course*
- totalFee: int
-generateRefNo(): int
-calculateFee(): int
+ slipCnt: int
+FeeSlip(Student*, int):
+ getRefNo(): int
+ getStdInfo(): Student*
+ getCoursesInfo(): Course*
+ getTotalFee(): int
+ setRefNo(int): void
+ setStdInfo(Student*): void
+ setCoursesInfo(Course*):
+ setTotalFee(int) : void
Page 3 of 10
III.
#ifndef COURSE_H
#define COURSE_H
#include <string.h>
using namespace std;
class Course{
private:
string courseName;
int code;
short credits;
public:
Course(string name,int code ,short crdhrs){
this->courseName=name;
this->code=code;
this->credits=crdhrs;
}
Course(){
this->courseName="";
this->code=-1;
this->credits=0;
}
string getCourseName(){ return courseName;}
int getCode(){ return code;}
short getCredits(){ return credits;}
};
#endif
Page 4 of 10
#ifndef STUDENT_H
#define STUDENT_H
#include <string.h>
#include "Course.h"
using namespace std;
class Student
{
private:
int phoneNo;
short semester,year;
string generateRollNo(){
string rl=to_string(year)+"00"+to_string(stdCnt);
return rl;
}
public:
}
string getName(){return name;}
string getFname(){ return fname;}
int getphoneNo(){return phoneNo;}
string getAddress(){ return address;}
string getRollNo(){ return rollNo;}
~Student(){
//delete [] pCoursesRegistered;
}
};
int Student::stdCnt=0;
#endif
Page 5 of 10
#ifndef FEESLIP_H
#define FEESLIP_H
#include "Student.h"
#include "Course.h"
class FeeSlip{
private:
int referenceNo;
Student* pStdInfo;
Course* pCoursesInfo;
int totalFee;
short nCourses;
int generateRefNo(){
return ++slipCnt;
}
int calculateFee(){
short totalCredits=0;
return totalCredits*12000;
}
public:
static int slipCnt;
FeeSlip(Student* pStdInfo, Course* pCoursesInfo, short nCourses){
this->referenceNo=generateRefNo();
this->pStdInfo=pStdInfo;
this->pCoursesInfo=pCoursesInfo;
this->nCourses=nCourses;
this->totalFee=calculateFee();
}
void print(){
cout<<"\n=============================================================================="<<endl;
cout<<"|\t\t\t Slip#:"<<this->referenceNo<<"
|"<<endl;
cout<<"| |"<<endl;
cout<<"|\t\t\t Student's info: |"<<endl;
cout<<"| |"<<endl;
cout<<"|\t\t\t Name: "<<pStdInfo->getName()<<"\tFname: "<<pStdInfo->getFname()<<"\tSemester:
"<<pStdInfo->getSemester()<<"|"<<endl;
cout<<"|----------------------------------------------------------------------------|"<<endl;
cout<<"|\t\t\t Courses |"<<endl;
cout<<"|----------------------------------------------------------------------------|"<<endl;
cout<<"|\t\t "<<i+1<<".\t|"<<pCoursesInfo[i].getCourseName()<<"
("<<pCoursesInfo[i].getCode()<<")"<<"\t\t\t| "<<pCoursesInfo[i].getCredits()*12000<<"|"<<endl;
}
cout<<"|----------------------------------------------------------------------------|"<<endl;
cout<<"| Total Fee ....|"<<getTotalFee()<<endl;
}
Page 6 of 10
};
Question 3 CLO 4 Marks: 25
Assume you have the following class with self explanatory member variables.
class Room {
public:
double length;
double breadth;
double height;
char *name;
};
For the Room class re-write the C++ code such that it includes
A parameterized constructor receiving room name and three other arguments as the values for the other
member variables of the class.
The constructors and destructor must have the appropriate initialization and cleanup code considering
the above class declaration.
Getter/setter functions for all the member variables.
Inside the main( ) create an array of Room containing three elements. All the array elements must be
instantiated using overloaded constructor.
int main()
{
Room R_array[3]={ Room(“Room1”,3.0,5.3,6.4,),
Room(“Room2”,2,4,6),
Room(“Room3”,7.3,4.5,6.1)
};
}
Write the C++ code for a class named Product that includes a data member called serialNumber. The
serialNumber holds the “serial number” of the product (i.e. object of Product class). Serial numbers are
assigned such that the first object have a serialNumber 1, the second have 2, and so on. To do this, you’ll
need another data member that records a count of how many objects of the Product class have been created
so far. This member should apply to the class as a whole; not to individual objects. Then, as each object is
created, its constructor can examine this count member variable to determine the appropriate serial number for
the new object. Add a member function that print the serial number of object. Inside the main() creates 3
objects and print their serial numbers. At the end, print the value of count.
Page 8 of 10
//============================================================================
// Name : OOPM_Q4.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
class Product {
private:
static int counter;
int serialNumber;
public:
Product() {
serialNumber = ++counter;
};
void printSerialNumber() {
cout << "I am object " << serialNumber << endl;
};
static int getCounter(){ // or simple member function called with ref of objs
return counter;
}
};
int Product::counter = 0;
int main() {
Product obj1;
Product obj2;
Product obj3;
obj1.printSerialNumber();
obj2.printSerialNumber();
obj3.printSerialNumber();
cout<<"Count ="<<Product::getCounter()<<endl;
}
Page 9 of 10
Page 10 of 10