University of Managementt & Technology
University of Managementt & Technology
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;
}
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;
}
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;
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);
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.
#include <iostream>
class person
private:
string name ;
public:
person(string n)
name = n;
void display()
};
private:
int RollNo;
public:
RollNo = rollno;
void display()
person::display();
};
int main()
{
Student s1("Asjad", 101);
Student s2 = s1;
s1.display();
s2.display();
return 0;