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

Assignment Basics

The document contains instructions for 8 programming tasks involving object-oriented concepts like classes, objects, methods, and encapsulation. The tasks involve creating classes to model real-world entities like integers, fractions, dates, times, distances, and heaters with appropriate private data and public methods. Main functions are provided to test the class functionality by creating objects, calling methods to set/get data and perform operations like addition. Students are asked to write the method code outside the classes to implement the required behavior.

Uploaded by

usmanahmadawan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Assignment Basics

The document contains instructions for 8 programming tasks involving object-oriented concepts like classes, objects, methods, and encapsulation. The tasks involve creating classes to model real-world entities like integers, fractions, dates, times, distances, and heaters with appropriate private data and public methods. Main functions are provided to test the class functionality by creating objects, calling methods to set/get data and perform operations like addition. Students are asked to write the method code outside the classes to implement the required behavior.

Uploaded by

usmanahmadawan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment 01: Object Oriented Programming BSIT-II Semester.

Task 1:
Create a class Int which has one private data member intvar.
Write a non-parametrized constructor and initialize intvar to 0.
Write a parametrized constructor and assign the parameter value to
intvar.
Write a display() function and print the value of intvar on the consol.
Write an add(Int x, Int y) function to add two class object values in third
data member and display the addition of both object values.
Class definition loos like this:
class Int{
private:
int intvar;
public:
Int();
Int(int x);
void display();
void add(Int x, Int y);
};
The main() function should looks like this:
int main(){
Int a(5),b(45);
Int c;
c.add(a,b);
c.display();
}
Sample Output:

Hint: See Example 13.13 from the book. Pg. # 408.

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 1|Page


Assignment 01: Object Oriented Programming BSIT-II Semester.

Task 2:
Create a class fraction which has two private data members numerator,
denominator as int and one data member c as char.
Write a dispFract() function as private to print the fractional value.
Write a getFract() function as public to input the numerator and
denominator in fractional form.
Write an addFract(fraction x, fraction y) function to add two class object
values and display the addition of both object values in fractional form.
Class definition loos like this:
class fraction{
private:
int numerator, denominator;
char c;
void dispFract();
public:
void getFract();
void addFract(fraction x, fraction y);
};
The main() function should looks like this:
int main()
{
fraction f1,f2,f3;
f1.getFct();
f2.getFct();
f3.addFct(f1, f2);
}
Sample Output:

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 2|Page


Assignment 01: Object Oriented Programming BSIT-II Semester.

Task 3:
Imagine a bridge toll booth. Passing vehicles are required to pay a 50
cent toll. Most of the time they do, but occasionally a car passes
without paying. The toll booth records the number of vehicles that have
passed through as well as the total amount of money taken in.
Model this tollbooth with a class called tollBooth. The two data items
are a type unsigned int to hold the total number of cars, and a type
double to hold the total amount of money collected. A constructor
initializes both of these to 0. A member function called payingCar()
increments the car total and adds 0.50 to the cash total. Another
function, called nopayCar(), increments the car total but adds nothing
to the cash total. Finally, a member function called display() displays
the two totals. Make appropriate member functions const.
Class definition and main() function look good in this question,  please
complete the code of the following functions outside the class:
#include <iostream>
#include <conio.h>

using namespace std;

char getWhatTheyWant();

class tollBooth{
private:
unsigned int numCars;
double amount;
public:
//Complete the code of the following function outside
the class.
tollBooth();
void payingCar();
void noPayCar();
void display();
};
int main(){
tollBooth booth;
char whatTheyWant;
whatTheyWant = getWhatTheyWant();

while(whatTheyWant!='s'){
switch(whatTheyWant){
case('p'):
Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 3|Page
Assignment 01: Object Oriented Programming BSIT-II Semester.

cout << "Paid" << endl << endl;


booth.payingCar();
whatTheyWant = getWhatTheyWant();
break;
case('n'):
cout << "Not Paid" << endl << endl;
booth.noPayCar();
whatTheyWant = getWhatTheyWant();
break;
case('s'):
cout << "Result" << endl << endl;
break;
default:
cout << "Invalid Input" << endl;
}
}
booth.display();
}
char getWhatTheyWant(){
char a;
cout << "Enter 'p' To Pay And Pass" << endl;
cout << "Enter 'n' To Pass Without Paying" << endl;
cout << "Press 's' To Show Result And Exit" << endl;
cout << "Make Your Choice" << endl;
a = getch();
return a;
}
Sample Output:

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 4|Page


Assignment 01: Object Oriented Programming BSIT-II Semester.

Task 4:
Create a Date class with private date members “month, day and year”.
Manage the following function given below in the class. Program should
add two dates and display it after addition of two dates. Class & main()
function looks good in question.  . Your task is to write proper code in
functions of class.
#include<iostream>
#include<conio.h>
using namespace std;

class date{
private:
int month, day, year;
char c;
public:
void setDate(); //input date
void getDate(); //display date
void addDate(date x, date y){
c='/';
//-----Check validation of date & Correction-----
//...Write proper code for both dates validation
cout<<"Date 1 after correction: ";x.getDate();
cout<<"Date 2 after correction: ";y.getDate();
cout<<"Addition of above two dates: ";
//----------------Addition------------------------
//...Write proper code for addition of two dates
//-----------------Correction---------------------
//...Write code for new date validation after addition
}
};

int main(){

date d1,d2,d3;
d1.setDate();
d2.setDate();

cout << "Date 1 Is:";


d1.getDate();
cout << "Date 2 Is:";
d2.getDate();
cout << "Date After Addition:"<<endl;
d3.addDate(d1,d2);
d3.getDate();
}

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 5|Page


Assignment 01: Object Oriented Programming BSIT-II Semester.

Sample Output:

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 6|Page


Assignment 01: Object Oriented Programming BSIT-II Semester.

Task 5:
Create a Time class with private data members “hours, minutes and
seconds”. Manage the following function given below in the class. The
program should add two Time types of objects and display them after
addition. Class & main() function looks good in question.  . Manage
member functions with proper logic outside of the class.
class Time
{
private:
int hours;
int minutes;
int seconds;

public:
void getTime(void); //Input Time in function
void putTime(void); //Display Time
void addTime(Time T1,Time T2);
};
int main()
{
Time T1,T2,T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1,T2);
T3.putTime();

return 0;
}
Sample Output:

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 7|Page


Assignment 01: Object Oriented Programming BSIT-II Semester.

Task 6:
Create a class “Distance” with private data members “feet and inches”.
Manage the following function given below in the class. The program
should add two Distance types of objects and display them after
addition. Class & main() function looks good in question.  . Your task
is to write proper code of functions outside of the class. Manage your
functions with proper logic. (After addition, inches should not be
greater than 12 i.e 12 inches = 1 feet).
class Distance
{
private:
int feet;
int inches;
public:
void set_distance();
void get_distance();
void add(Distance d1, Distance d2);
};

int main()
{
Distance d1, d2, d3;
int feet,inches;
d1.set_distance();
d2.set_distance();
d3.add(d1, d2);
d3.get_distance();
return 0;
}
Sample Output:

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 8|Page


Assignment 01: Object Oriented Programming BSIT-II Semester.

Task 7:
Create a class, Heater that contains a single integer field, temperature.
Define a constructor that takes no parameters. The temperature field
should be set to the value 15 in the constructor. Define the mutators
warmer and cooler, whose effect is to increase or decrease the value of
the temperature by 5 respectively. Define an accessor method to return
the value of temperature.
Note: Accessor and mutator functions (a.k.a. set and get functions)
provide a direct way to change or just access private variables. They must
be written with the utmost care because they have to provide the
protection of the data that gives meaning to a class in the first place.
Sample Output:

Task 8:
Modify the above Task 07 and add three new integer fields called
increment, max, and min in your Heater class. Change the constructor to
parameterize which should be used to set the min and max values and
the increment's value should be set to 5.
Change warmer and cooler definitions so that they use the value of
increment rather than an explicit value of 5.
Verify that everything functions as it did before moving on. Now change
the warmer method to prevent the temperature from being set to a
value greater than max. In a similar manner, alter the cooler method of
the Heater so that it forbids temperature settings lower than min. Now
add a method, setIncrement() that takes a single integer parameter and
uses it to set the value of the increment.
Once again, test that the class works as you would expect it to, by
creating some Heater objects. Do things still work as expected if a
negative value is passed to the setIncrement() method? Add a check to
Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 9|Page
Assignment 01: Object Oriented Programming BSIT-II Semester.

this method to prevent a negative value from being assigned to


increment.
In the main() function following instructions are executed for sample
output:
int main()
{
Heater h(5,25);
cout<<"Initial Temprature of Heater:
"<<h.getTempratur()<<endl; //5
h.warmer();
cout<<"Temprature of Heater After Warmer:
"<<h.getTempratur()<<endl; //20
h.cooler();
cout<<"Temprature of Heater After Cooler:
"<<h.getTempratur()<<endl; //15
h.setIncrement(10);
h.warmer();
cout<<"Temprature of Heater After increment to
warmer: "<<h.getTempratur()<<endl; //25
h.setIncrement(5);
h.cooler();
cout<<"Temprature of Heater After increment to
cooler: "<<h.getTempratur()<<endl; //20
h.setIncrement(10);
h.cooler();
cout<<"Temprature of Heater After increment to
cooler: "<<h.getTempratur()<<endl; //10
h.setIncrement(10);
h.cooler();
cout<<"Temprature of Heater After increment to
cooler: "<<h.getTempratur()<<endl; //10
h.setIncrement(30);
h.warmer();
cout<<"Temprature of Heater After increment to
warmer: "<<h.getTempratur()<<endl; //
return 0;
}

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 10 | P a g e


Assignment 01: Object Oriented Programming BSIT-II Semester.

Sample Output:

Prof. Usman Ahmad. Assistant Professor. Department of IT & CS. 11 | P a g e

You might also like