SP23-BCS-061 Oop Ass1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Object Oriented Programming

Assignment 1

Aitzaz Ahmed
SP23-BCS-061

Question 1:
#include <iostream>
using namespace std;
class Bank
{
private:
string name,accountType;
int accountNumber;
float balance;

public:
Bank(string name, int accountNumber, string accountType, float balance)
{
this->name = name;
this->accountNumber = accountNumber;
this->accountType = accountType;
this->balance = balance;
}
void deposit(float amount)
{ balance += amount;}
void withdraw(float amount)
{
if (balance < amount)
{ cout << "Insufficient balance" << endl;}
else
{ balance -= amount;}
}
void display()
{
cout << "Name: " << name << endl;
cout << "Balance: " << balance << endl;
}
};

int main()
{ Bank b1("Aitzaz", 30000, "Current", 1000);

b1.deposit(500);
b1.display();
b1.withdraw(2000);
b1.display();
return 0;
}
Question 2:

#include <iostream>
using namespace std;
class Int
{
private:
int value;
public:
Int()
{
value = 0;
}
Int(int value)
{
this->value = value;
}
void display()
{
cout << value << endl; }
void add(Int i)
{ value += i.value; }
};

int main()
{
Int i1, i2(5), i3(10);
i1.add(i2);
i1.add(i3);
i1.display();

return 0;
}
Question 3:

#include <iostream>
using namespace std;

class Tollbooth
{
private:
unsigned int totalCars;
double totalCash;

public:
Tollbooth()
{
totalCars = 0;
totalCash = 0;
}
void payingCar()
{
totalCars++;
totalCash += 0.50;
}
void nopayCar()
{ totalCars++;}
void display() const
{
cout << "Total cars: " << totalCars << endl;
cout << "Total cash: " << totalCash << endl;
}
};

int main()
{
Tollbooth t1;
char ch;
while (true)
{
cout << "Press 'p' to count a paying car, 'n' to count a nonpaying
car, or 'e' to exit" << endl;
cin >> ch;
if (ch == 'p')
{
t1.payingCar();
}
else if (ch == 'n')
{
t1.nopayCar(); }
else if (ch == 'e')
{
t1.display();
break;
}
}

return 0;
}
Question 4:

#include <iostream>
using namespace std;

class Time
{
private:
int hours, minutes, seconds;
public:
Time()
{
hours = 0; minutes = 0; seconds = 0;
}
Time(int hours, int minutes, int seconds)
{
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;
}
void display()
{
cout << hours << ":" << minutes << ":" << seconds << endl;
}
Time add(const Time &t) const
{
Time sum;
sum.hours = hours + t.hours;
sum.minutes = minutes + t.minutes;
sum.seconds = seconds + t.seconds;
if (sum.seconds >= 60)
{
sum.seconds -= 60;
sum.minutes++;
}
if (sum.minutes >= 60)
{
sum.minutes -= 60; sum.hours++;
}
return sum;
}};
int main()
{
const Time t1(05, 25, 30), t2(35, 10, 45);
Time t3;
t3 = t1.add(t2);
t3.display();
return 0;
}
Question 5:

#include <iostream>
using namespace std;
class Student
{
private:
string studentName, rollNo, courseName;
int semesterNo, marks;
char grade;

public:
Student(string studentName, string rollNo,
int semesterNo, string courseName, int
marks)
{
this->studentName = studentName;
this->rollNo = rollNo;
this->semesterNo = semesterNo;
this->courseName = courseName;
this->marks = marks;
}
Student()
{
studentName = "";
rollNo = "";
semesterNo = 0;
courseName = "";
marks = 0;
}
void getStudentData()
{
cout << "Enter student name = ";
cin >> studentName;
cout << "Enter roll no. = ";
cin >> rollNo;
cout << "Enter semester no. = ";
cin >> semesterNo;
cout << "Enter course name = ";
cin >> courseName;
cout << "Your marks is = ";
cin >> marks;
}
void displayQuizGrade()
{
if (marks >= 85)
{ grade = 'A';}
else if (marks >= 80)
{ grade = 'A-';}
else if (marks >= 70)
{ grade = 'B-';}
else if (marks >= 60)
{ grade = 'C+'; }
else
{ grade = 'F';}
cout << "Your grade is = " << grade <<
endl;
}
};

int main()
{
Student s1("Aitzaz Ahmed", "SP23-BCS-0061-
Section (A)", 2, "OOP", 90);
Student s2("Jahanzaib", "SP23-BCS-070-
Section (A)", 2, "OOP", 40);
Student s3;

s3.getStudentData();

cout << "\nResult \n\n";

s1.displayQuizGrade();
s2.displayQuizGrade();
s3.displayQuizGrade();

return 0;
}
Question 6:

#include <iostream>
using namespace std;

class Travel
{
private:
int kilometers;
int hours;
public:
Travel()
{
kilometers = 0;
hours = 0;
}
void get();
void show();
void add(Travel t);
};

void Travel::get()
{
cout << "Enter kilometers = ";
cin >> kilometers;
cout << "Enter hours = ";
cin >> hours;
}

void Travel::show()
{
cout << "Kilometers: " << kilometers <<
endl;
cout << "Hours: " << hours << endl;
}

void Travel::add(Travel t)
{
kilometers += t.kilometers;
hours += t.hours;
}

int main()
{
Travel t1, t2;

t1.get();
t2.get();

t1.add(t2);
t1.show();
return 0;
}

Question 7:

#include <iostream>
using namespace std;
class Travel
{
private:
int kilometers, hours;

public:
Travel()
{
kilometers = 0;
hours = 0;
}
void get();
void show();
Travel add(Travel t);
};

void Travel::get()
{
cout << "Enter kilometers = ";
cin >> kilometers;
cout << "Enter hours = ";
cin >> hours;
}

void Travel::show()
{
cout << "Kilometers: " << kilometers << endl;
cout << "Hours: " << hours << endl;
}

Travel Travel::add(Travel t)
{
Travel sum;
sum.kilometers = kilometers + t.kilometers;
sum.hours = hours + t.hours;
return sum;
}

int main()
{
Travel t1, t2, t3;
t1.get();
t2.get();

t3 = t1.add(t2);
t3.show();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy