0% found this document useful (0 votes)
51 views11 pages

University of Managementt & Technology

This document appears to be an exam for a course on Object Oriented Programming Lab. It contains 5 questions related to OOP concepts like classes, objects, constructors, operators, inheritance etc. Students are asked to write code to define classes and functions to represent rational numbers, days of the week, personal details, arrays and inheritance between classes. The exam is out of a total of 55 marks and students are given 150 minutes to complete it.

Uploaded by

Asjad
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)
51 views11 pages

University of Managementt & Technology

This document appears to be an exam for a course on Object Oriented Programming Lab. It contains 5 questions related to OOP concepts like classes, objects, constructors, operators, inheritance etc. Students are asked to write code to define classes and functions to represent rational numbers, days of the week, personal details, arrays and inheritance between classes. The exam is out of a total of 55 marks and students are given 150 minutes to complete it.

Uploaded by

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

University of Managementt & Technology

Student Name: _______________________ Student ID: ____________________


Course Title: Object Oriented Programming Lab Lab Section: V6
Course Code: CC1022L Semester: Spring 2021
Instructors: Sharaz Haider x
Date: 26/06/2021 Total Marks: 55
Exam: Final Term Time Allowed: 150 mins

Question 1: Create a class RationalNumber to represent numbers in p/q form


e.g. 2/7. (15)

a) Write the definition of the class RationalNumber with data members


and member function prototypes after reading the complete problem.
b) Write the default constructor that sets both p and q to be 1.
c) Write the parameterized/overloaded constructor that prevents a 0 or negative
denominator and simplifies the fraction to reduced form if it is not in reduced form
already by calling its utility/helper function simplify(). For example 4/14 should be
reduced to 2/7.
d) Write the definition of the utility/helper function simplify().
e) Overload == operator to compare two RationalNumber objects.
f) Define print function for p/q
#include <iostream>
using namespace std;

class Fraction {
private:
int num;
int den;

public:
Fraction(int top, int bottom) {
num = top;
den = bottom;
}
void display(){

}
Fraction (int top) {
num = top;
den = 1;
}

bool operator ==(Fraction &otherFrac) {


int firstnum = num*otherFrac.den;
int secondnum = otherFrac.num*den;

return firstnum==secondnum;
}
};

int main() {
Fraction f1(1,4);
Fraction f2(1,2);

return 0;
}

Question 2 (15)
Design and implement a class dayType that implements the day of the week in a
program. The class dayType should store the day, such as “Sun” for Sunday. The
program should be able to perform the following operations on an object of type
dayType:
a. Set the day. void setDay(string d);
b. Print the day. void printDay();
c. Return the day. string getDay();
d. Return the next day. string getNextDay();
e. Return the previous day. string getPreviousDay();
f. Calculate and return the day by adding certain days to the current day. For
example, if the current day is Monday and we add 4 days, the day to be
returned is Friday. Similarly, if today is Tuesday and we add 13 days,
the day to be returned is Monday.
string getDayByAddingNumber(int number);
g. Add the appropriate constructors.
#include <iostream>
using namespace std;

class daytype{

private:
int currentday;
string daysnamne[7]={"Sun", "Mon","Tues","Wed","Thur", "Fri", "Sat"};
public:
daytype(int currentDayIn){
currentday = currentDayIn;
currentday--;
currentday%=7;
}

void setDay(int currentDayIn)


{
currentday = currentDayIn;
currentday%=7;
}
void printCurrentDay(){
cout<<"Current day is : "<<daysnamne[currentday]<<endl;
}

string getCurrentDay(){
return daysnamne[currentday];
}
string getNextDay(){
int day = currentday;
day++;
if(day==7){
day = 0;
}
return daysnamne[day];
}

string getPreviosDay(){
int day = currentday;
day--;
if(day<0){
day = 6;
}
return daysnamne[day];
}

};

int main()
{
daytype day(1);
cout<<"Previous day is : "<<day.getPreviosDay()<<endl;
day.printCurrentDay();
cout<<"Next day is : "<<day.getNextDay()<<endl;
cout<<"\n\nSetting day to 3\n";
day.setDay(3);
cout<<"Current day is : "<<day.getCurrentDay()<<endl;

cout<<"Next day is : "<<day.getNextDay()<<endl;

return 0;
}
Write the definitions of the functions to implement the operations for the class
dayType. Also, write a program to test various operations on this class.

Question 3 (10)
Defined a class personType to store the name of a person which includes
firstName, middlename, lastName. Define the default and parameterized
constructors and the following member functions:
a. Set the first name only. void setFirstname(string firstN);

b. Set the last name only. void setLastname(string lastN);

c. Store and set the middle name. void setMiddlename(string


middleN);

d. Check whether a given first name is the same as the first name
of this person. bool isFirstNameSame(string name);
e. Check whether a given last name is the same as the last name
of this person. bool isLastNameSame(string name);
Write the definitions of the member functions to implement the operations
for this class. Also, write a program to test various operations on this class.
#include <iostream>
#include <string>
using namespace std;
class persontype
{
private:
string firstName, middlename, lastName;
public:
persontype()
{
firstName = "null";
middlename = "null";
lastName = "null";
}
persontype(string fn = "Rana", string mn = "Asjad",
string ln = "Ramzan" )
{
firstName = fn;
middlename = mn;
lastName = ln;
}
void setFirstname(string firstN)
{
firstName = firstN;
}
void setLastname(string lastN)
{
lastName = lastN;
}
void setMiddlename(string middleN)
{
middlename = middleN;
}
bool isFirstNameSame(string name)
{
bool A = true;
bool B = false;
cout<<"\nPlease Enter first name to search:
\n"<<endl;
getline(cin, name);
if (firstName == name)
{
cout<< A<<endl;
}
else
cout<< B<<endl;
}
bool isLastNameSame(string name)
{
bool A = true;
bool B = false;
cout<<"\nPlease Enter last name to search:
\n"<<endl;
getline(cin, name);
if (lastName == name)
{
cout << A <<endl;
}
else
cout<<B<<endl;
}
};
int main()
{
string i, j, fn, ln, mn;
fn = "Rana";
mn = "Asjad";
ln = "Ramzan";
persontype p1(fn, mn, ln);
p1.setFirstname(fn);
p1.setMiddlename(mn);
p1.setLastname(ln);
p1.isFirstNameSame( i);
p1.isLastNameSame(j);
}
Question 4 (5)
Write a program with a class that contains an array of integers. Initialize the integer
array in the constructor of the class. Then create two friend functions to the class to
find the largest and smallest integers in the array. (One function should be global)
Create a destructor that sets all of the elements in the array to 0.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

class integer{
private:
int Arr[10]={1,2,6,3,5,7,8,4,4,17 };
public:
integer(){ }
~integer();
friend void M(integer&);
};

void M(integer& A)
{
int Max1,Min1;
int flag=0;
Max1=A.Arr[0];
Min1=A.Arr[0];
for(int i=0; i <10; i++){
if(Max1<A.Arr[i])
Max1=A.Arr[i];
if(Min1>A.Arr[i])
Min1=A.Arr[i];
}
sort(A.Arr, A.Arr+10);
cout<<"\nThe largest integer in the Array \n"<<Max1<<endl;
cout<<"\nThe smallest integer in the Array\n "<<Min1<<endl;
cout<<"\nThe repeated elements are: \n"<<endl;
for(int i=0; i<10; i++){
for(int j=i+1; j<10; j++){
if(A.Arr[i]==A.Arr[j]){
cout<<A.Arr[j]<<" ";
}
}
}

}
integer::~integer() {
cout<<"\nSetting all the elements to 0\n";
for(int i=0; i<10; i++) {
Arr[i]=0;
cout<<" "<<Arr[i];
}
}

int main(){
integer I;
M(I);

}
Question 5 (10)

Declare classes Person and Student where Student is derived from Parent class. Parent
has Name and Student has RollNo as its private member.

a) Create a Student class object and initialize it with the already existing object of
Student class. Analyze the behavior of default copy constructors of Person and
Student classes.

b) Explicitly write copy constructor for Person class. Analyze the behavior.

c) Now, Explicitly write copy constructor for the Student class.

#include <iostream>

using namespace std;

class person

private:

string name ;
public:

person(string n)

name = n;

void display()

cout << "Name: " <<name << endl;

};

class Student : public person

private:

int RollNo;

public:

Student(string n, int rollno) : person(n)

RollNo = rollno;

void display()

person::display();

cout << "Roll No.: " << RollNo << endl;

};

int main()

{
Student s1("Asjad", 101);

Student s2 = s1;

cout << "\nStudent 1: " << endl;

s1.display();

cout << "\nStudent 2: " << endl;

s2.display();

return 0;

You might also like