OOP Lab
OOP Lab
#include <iostream>
class Rectangle {
private:
float length;
float width;
public:
// Default constructor
Rectangle() {
length = 0.0;
width = 0.0;
// Parameterized constructor
Rectangle(float l, float w) {
length = l;
width = w;
// Destructor
~Rectangle() {
void getData() {
cout << "Enter length: ";
void showData() {
float area() {
float perimeter() {
};
int main() {
rect1.getData();
rect1.showData();
rect2.showData();
return 0;
}
Q#8
#include <iostream>
class Distance {
private:
int feet;
float inches;
public:
feet = ft;
inches = in;
void getDist() {
inches -= 12.0;
feet++;
void display() {
cout << "Feet: " << feet << " Inches: " << inches << endl;
};
int main() {
dist1.getDist();
dist2.getDist();
dist3.add(dist1, dist2);
dist1.display();
dist2.display();
cout << "Distance 3 (sum of Distance 1 and Distance 2):" << endl;
dist3.display();
return 0;
}
Week # 4
Q9
#include <iostream>
class Int {
private:
int value;
public:
Int() {
value = 0;
Int(int val) {
value = val;
void display() {
value += other.value;
}
};
int main() {
Int uninitializedInt;
Int initializedInt1(10);
Int initializedInt2(20);
initializedInt1.display();
initializedInt2.display();
uninitializedInt.add(initializedInt1);
uninitializedInt.add(initializedInt2);
uninitializedInt.display();
return 0;
}
Q10
#include <iostream>
class tollBooth {
private:
double totalCash;
public:
tollBooth() {
totalCars = 0;
totalCash = 0.0;
}
void payingCar() {
totalCars++;
totalCash += 0.50;
void nopayCar() {
totalCars++;
};
int main() {
tollBooth booth;
char key;
cout << "Press 'p' to count a paying car, 'n' to count a non-paying car, or 'Esc' to exit." << endl;
do {
key = _getch(); // Using _getch() to read a single character without waiting for Enter key
switch (key) {
case 'p':
booth.payingCar();
break;
case 'n':
booth.nopayCar();
break;
booth.display();
return 0;
}
Week # 5
Q11
#include <iostream>
class Time {
private:
int hours;
int minutes;
int seconds;
public:
Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) {} // Constructor to initialize with fixed
values
void display() const {
cout << hours << ":" << minutes << ":" << seconds;
Time result;
totalSeconds %= 3600;
return result;
};
int main() {
Time t3;
t3 = t3.add(t1, t2);
t1.display();
t3.display();
return 0;
}
Q12
#include <iostream>
class Employee {
private:
int employeeNumber;
float compensation;
public:
employeeNumber = empNum;
compensation = comp;
};
int main() {
int empNum;
float comp;
emp1.setData(empNum, comp);
emp2.setData(empNum, comp);
emp1.displayData();
emp2.displayData();
emp3.displayData();
return 0;
}
Q 13
#include <iostream>
class Date {
private:
int month;
int day;
int year;
public:
void getDate() {
cin >> month >> slash >> day >> slash >> year;
cout << "Date: " << month << "/" << day << "/" << year << endl;
};
int main() {
date1.showDate();
date2.showDate();
return 0;
}
Week # 6:
Q-14
#include <iostream>
class SerialNumberedObject {
private:
static int objectCount; // Static data member to record count of objects created
public:
SerialNumberedObject() {
}
void reportSerialNumber() const {
cout << "I am object number " << serialNumber << endl;
};
int main() {
obj1.reportSerialNumber();
obj2.reportSerialNumber();
obj3.reportSerialNumber();
return 0;
}
Q 15
#include <iostream>
#include <sstream>
class Fraction {
private:
int numerator;
int denominator;
public:
char slash;
};
int main() {
char choice;
Fraction result;
do {
frac1.getInput();
result = frac1.add(frac2);
result.display();
cout << "Do you want to enter another set of fractions? (y/n): ";
return 0;