Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
OOP program questions with answers
1. Assignment #1
Q#1: Create a class that imitates part of the functionality of the basic data type int. Call the
class Int (note different capitalization). The only data in this class is an int variable. Include
member functions to initialize an Int to 0, to initialize it to an int value, to display it (it looks just
like an int), and to add two Int values. Write a program that exercises this class by creating one
uninitialized and two initialized Int values, adding the two initialized values and placing the
response in the uninitialized value, and then displaying this result.
Program:
#include <iostream>
usingnamespace std;
classInteger
{
private:
int num,num1;
public:
Integer()
{}
Integer(intn)
{
num=n;
}
int add(IntegerInt1,IntegerInt2)
{
num1=Int1.num+Int2.num;
returnnum1;
}
};
intmain()
{
int number;
IntegerInt1(10),Int2(13),Int3;
number=Int3.add(Int1,Int2);
cout<<"1st
number:"<<10<<"n2nd number:"<<13<<endl;
cout<<"n3rd numberissum of 1st and2nd numbers:"<<number<<"n";
}
Output:
1st
number:10
2nd
number:13
3rd
numberissumof 1st and 2nd numbers: 23
2. Q#2: Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent
toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of
the number of cars that have gone by, and of the total amount of money collected. 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.
Program:
#include <iostream>
usingnamespace std;
classtollboth
{
private:
int null,pcar,ncar;
floattax;
public:
tollboth()
{
tax=0;
pcar=0;
ncar=0;
null=0;
}
voidpaycar(inta)
{
pcar=pcar+a;
for(null;null<=pcar;null++)
{
tax=tax+0.50;
}
}
voidnopaycar(intb)
{
ncar=ncar+b;
}(
voiddisplay()
{
cout<<"Total no of payedcars are : "<<pcar<<endl;
cout<<"Total tax is : "<<tax<<endl;
cout<<"Total no of not payedcars are : "<<ncar<<endl;
}
};
3. intmain()
{
tollbothtb;
char press,input;
int a,b;
do{
cout<<"Press1 forcar pay tax"<<endl;
cout<<"Press2 forcar not pay tax"<<endl;
cout<<"Press3 fortotal tax and Exit"<<endl;
cin>>press;
switch(press)
{
case '1':
{
cout<<"EnterNo of the cars pay tax"<<endl;
cin>>a;
tb.paycar(a);
break;
}
case '2':
{
cout<<"EnterNo of cars notpay tax"<<endl;
cin>>b;
tb.nopaycar(b);
break;
}
}
cout<<"Pressy to continue andn forterminate"<<endl;
cin>>input;
}
while(input=='y');
tb.display();
system("pause");
return0;
}
Output:
Press 1 for car pay tax
Press2 forcar not paytax
Press3 fortotal tax and exit
1
Enter Noof the cars pay tax
2
Pressy to continue andnfor terminate
y
Press 1 for car pay tax
Press2 forcar not paytax
4. Press3 fortotal tax and exit
2
Enter Noof cars not pay tax
2
Pressy to continue andnfor terminate
y
Press 1 for car pay tax
Press2 forcar not paytax
Press3 fortotal tax and exit
3
Pressy to continue andnfor terminate
Y
Total noof payedcars are : 2
Total tax is : 1.5
Total noof notpayedcars are : 2
Q#3: Create a class called time that has separate int member data for hours, minutes, and
seconds. One constructor should initialize this data to 0, and another should initialize it to fixed
values. Another member function should display it, in 11:59:59 format. The final member
function should add two objects of type time passed as arguments. A main() program should
create two initialized time objects (should they be const?) and one that isn’t initialized. Then it
should add the two initialized values together, leaving the result in the third time variable.
Finally it should display the value of this third variable. Make appropriate member functions
const.
Program:
#include <iostream>
#include <conio.h>
usingnamespace std;
classtime{
private:
inthours,minutes,seconds;
public:
time(){
hours= minutes=seconds= 0;
}
time(inth,intm,int s){
hours= h;
minutes=m;
seconds= s;
}
voidshowTime() const{
cout << hours<< ':' << minutes<<':' << seconds;
}
voidaddTime(timex,time y){
seconds= x.seconds+y.seconds;
5. if(seconds>59){
seconds-=60;
minutes++;
}
minutes+= x.minutes+y.minutes;
if(minutes>59){
minutes-=60;
hours++;
}
hours+=x.hours+y.hours;
}
};
intmain(){
const time a(2,23,45), b(4,25,15);
time c;
c.addTime(a,b);
c.showTime();
}
Output:
06:49:00
Q#4: Create an employee class. The member data should comprise an int for storing the
employee number and a float for storing the employee’s compensation. Member functions
should allow the user to enter this data and display it. Write a main() that allows the user to
enter data for three employees and display it.
Program:
#include <iostream>
#include <conio.h>
usingnamespace std;
classemployee{
private:
intemp_num;
floatemp_comp;
public:
voidentData(){
cout << "EnterEmployee'sNumber: ";
cin >> emp_num;
cout << "EnterEmployee'sSalary :" ;
cin >> emp_comp;
}
voiddisplay(){
cout << "Employee'sNumber:" << emp_num<< endl;
cout << "Enployee'sSalary: "<< emp_comp<< endl;
6. }
};
intmain(){
employee emp1,emp2,emp3;
cout << "Enter Data For Employee 1"<< endl;
emp1.entData();
cout << "Enter Data For Employee 2"<< endl;
emp2.entData();
cout << "Enter Data For Employee 3"<< endl;
emp3.entData();
cout << "Total Data EnteredIs : " << endl;
emp1.display();
emp2.display();
emp3.display();
}
Output:
Enter Data For Employee 1
Enter Employee’sNumber:5
Enter Employee’sSalary:20000
Enter Data For Employee 2
Enter Employee’sNumber:6
Enter Employee’sSalary:65000
Enter Data For Employee 3
Enter Employee’sNumber:7
Enter Employee’sSalary:15000
Total Data EnteredIs:
Employee’sNumber 5
Employee’sSalary 20000
Employee’sNumber 6
Employee’sSalary 65000
Employee’sNumber 7
Employee’sSalary 15000
Q#5: Define a class that will hold the set of integers from 0 to 31. An element can be set with
the set member function and cleared with the clear member function. It is not an error to set
an element that's already set or clear an element that's already clear. The function test is used
to tell whether an element is set.
Program:
#include <iostream>
#include<windows.h>
#include<conio.h>
usingnamespace std;
classInteger{
8. Integera;
do {
system("cls");
cout<<"nnntMAIN MENU";
cout<<"nnt01.Set Array1 to 31 values";
cout<<"nnt02.Test Array";
cout<<"nnt03.ClearArray";
cout<<"nnt04.EXIT";
cout<<"nntSelectYourOption(1-4) ";
cin>>num;
switch(num)
{
case 1:
a.setArray();
break;
case 2:
a.test();
break;
case 3:
a.clear();
break;
}
}while(num!=4);
return0;
}
Q#6: Write a "checkbook" class. You put a list of numbers into this class and get a total out.
Member functions:
void check::additem(int amount); // Add a new entry to the checkbook
int check::total(void); // Return the total of all items
Program:
#include <iostream>
using namespace std;
Class check
{
Private:
int sum=0,n;
Public:
9. void add item();
int add item(int amount);
total(void);
};
void check::additem(){
cout<<"Enter number of items in the data set:";
cin>>n;
int dset[n];
int i;
for(i=0;i<n;i++) {
cout<<"dset["<<i<<"]:";
cin>>dset[i];
}
}
void check::total(){
for(i=0;i<n;i++){
sum=sum+dset[i];
cout<<"Total:"<<sum<<endl;
}
}
int main(){
check c;
void a;
10. int t;
a=c.additem();
t=c.total();
cout << "numbers:" << a << endl;
cout << "sum:" << t << endl;
return 0;
}
Method 2:
#include <iostream>
#include <conio.h>
using namespace std;
void sumArray(){
int sum=0,n;
cout<<"Enter number of items in the data set:";
cin>>n;
int dset[n];
int i;
for(i=0;i<n;i++) { //input array elements
cout<<"dset["<<i<<"]:";
cin>>dset[i];
}