You are on page 1of 7

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Object Oriented
Programming
Assignment # 01
Last date of Submission: 11 Nov, 2020

Submitted To: Ms. Sehresh Khan

Student Name: Muhammad Zain Zafar

Reg Number: 1912267

Object Oriented Programming BS/CS-2 SZABIST-ISB


COMPUTER SCIENCE DEPARTMENT

Question1. (2.5
Marks)
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.

Include a program to test this class. This program should allow the user to push one key
to count a paying car, and another to count a nonpaying car. Pushing the Esc key should
cause the program to print out the total cars and total cash and then exit.

Solution:

#include <iostream>
using namespace std;

class tollboth
{ private:
int null,pcar,wpcar;
float tax;
public:
tollboth()
{
tax=0;
pcar=0;
wpcar=0;
null=0;
}
void paycar(int a)
{
/*pcar=a;*/
pcar=pcar+a;
for(null;null<=pcar;null++)
{
tax=tax+0.50;
}
}
void withoutpay(int b)
{
wpcar=wpcar+b;
}
void showdata()
{
cout<<"Total no of payed cars are : "<<pcar<<endl;
cout<<"Total tax is : "<<tax<<endl;
cout<<"Total no of not payed cars are : "<<wpcar<<endl;

}
};
int main()
{
tollboth tb;
char press,input;
int a,b;
do{

cout<<"Press 1 for pay tax cars"<<endl;


cout<<"Press 2 for not pay tax cars"<<endl;
cout<<"Press 3 for total tax "<<endl;
cin>>press;

switch(press)
{
case '1':
{
cout<<"Enter Number of the pay tax cars"<<endl;
cin>>a;
tb.paycar(a);
break;
}
case '2':
{
cout<<"Enter Number of not pay tax cars"<<endl;
cin>>b;
tb.withoutpay(b);
break;
}
}
cout<<"Press y to continue and n for exit"<<endl;

cin>>input;
}
while(input=='y');
tb.showdata();
system("pause");
return 0;
}

OUTPUT:
Question2.
(2.5
Marks)

Create an Account class that a bank might use to represent customers’ bank accounts.
Include a data member of type int to represent the account balance. Provide a constructor
that receives an initial balance and uses it to initialize the data member.
The constructor should validate the initial balance to ensure that it’s greater than or equal
to 0. If not, set the balance to 0 and display an error message indicating that the initial
balance was invalid.
Provide three member functions. Member function credit should add an amount to the
current balance. Member function debit should withdraw money from the Account and
ensure that the debit amount does not exceed the Account’s balance. If it does, the
balance should be left unchanged and the function should print a message indicating
"Debit amount exceeded account balance." Member function getBalance should return
the current balance. Create a program that creates two Account objects and tests the
member functions of class Account.

Solution:

#include<iostream>
#include <string>
using namespace std;

class Account
{
public:
Account( int);
void credit(int );
void debit(int );
int getAccountBalance();
private:
int accountBalance;
};
Account::Account( int balance)
{
if(balance>=0)
accountBalance=balance;
else

accountBalance=0;
cout<<"Initial balance was invalid."<<endl;
}
}
void Account::credit( int balance)
{
accountBalance = accountBalance + balance;
}
void Account::debit( int balance)
{
if(accountBalance>=balance)
{
accountBalance = accountBalance - balance;
}
else
{
cout<<"Debit amount exceeded account balance."<<endl;
}
}
int Account::getAccountBalance()
{
return accountBalance;
}
int main()
{
Account Account1(100);
Account Account2(-66);
cout << "Account 1 initial balance is: "<<
Account1.getAccountBalance()<< "\nAccount 2 initial balance is: "<<
Account2.getAccountBalance() << endl;

Account1.credit(500);
Account2.credit(44);
cout << "\nAccount 1 balance is: "<< Account1.getAccountBalance()<<
"\nAccount 2 balance is: "<< Account2.getAccountBalance() << endl;
Account1.debit(60);
Account2.debit(54);
cout << "\nAccount 1 balance is: "<< Account1.getAccountBalance()<<
"\nAccount 2 balance is: "<< Account2.getAccountBalance() << endl;
};

You might also like