Week #3 Q7
#include <iostream>
using namespace std;
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() {
cout << "Destructor called" << endl;
void getData() {
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
void showData() {
cout << "Length: " << length << endl;
cout << "Width: " << width << endl;
float area() {
return length * width;
float perimeter() {
return 2 * (length + width);
};
int main() {
Rectangle rect1; // Default constructor called
Rectangle rect2(5.0, 3.0); // Parameterized constructor called
cout << "Rectangle 1:" << endl;
rect1.getData();
cout << "Rectangle 1 data:" << endl;
rect1.showData();
cout << "Area of Rectangle 1: " << rect1.area() << endl;
cout << "Perimeter of Rectangle 1: " << rect1.perimeter() << endl;
cout << endl;
cout << "Rectangle 2 data:" << endl;
rect2.showData();
cout << "Area of Rectangle 2: " << rect2.area() << endl;
cout << "Perimeter of Rectangle 2: " << rect2.perimeter() << endl;
return 0;
}
Q#8
#include <iostream>
using namespace std;
class Distance {
private:
int feet;
float inches;
public:
// Setter function to set feet and inches
void setDist(int ft, float in) {
feet = ft;
inches = in;
// Getter function to get feet and inches from user
void getDist() {
cout << "Enter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
// Add function to add two Distance objects
void add(const Distance& d1, const Distance& d2) {
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
if (inches >= 12.0) {
inches -= 12.0;
feet++;
// Display function to display the Distance object
void display() {
cout << "Feet: " << feet << " Inches: " << inches << endl;
};
int main() {
Distance dist1, dist2, dist3;
cout << "Enter values for Distance 1:" << endl;
dist1.getDist();
cout << "Enter values for Distance 2:" << endl;
dist2.getDist();
dist3.add(dist1, dist2);
cout << "Distance 1:" << endl;
dist1.display();
cout << "Distance 2:" << endl;
dist2.display();
cout << "Distance 3 (sum of Distance 1 and Distance 2):" << endl;
dist3.display();
return 0;
}
Week # 4
Q9
#include <iostream>
using namespace std;
class Int {
private:
int value;
public:
// Constructor to initialize Int to 0
Int() {
value = 0;
// Constructor to initialize Int to a given value
Int(int val) {
value = val;
// Display function to show the value of Int
void display() {
cout << value;
// Add function to add two Int values
void add(const Int& other) {
value += other.value;
}
};
int main() {
Int uninitializedInt;
Int initializedInt1(10);
Int initializedInt2(20);
cout << "Initialized Int 1: ";
initializedInt1.display();
cout << endl;
cout << "Initialized Int 2: ";
initializedInt2.display();
cout << endl;
uninitializedInt.add(initializedInt1);
uninitializedInt.add(initializedInt2);
cout << "Sum of Initialized Int 1 and Initialized Int 2: ";
uninitializedInt.display();
cout << endl;
return 0;
}
Q10
#include <iostream>
#include <conio.h> // for _getch()
using namespace std;
class tollBooth {
private:
unsigned int totalCars;
double totalCash;
public:
tollBooth() {
totalCars = 0;
totalCash = 0.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 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;
} while (key != 27); // 27 is the ASCII value for Esc key
cout << "Exiting..." << endl;
booth.display();
return 0;
}
Week # 5
Q11
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
int seconds;
public:
Time() : hours(0), minutes(0), seconds(0) {} // Constructor to initialize to 0
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 add(const Time& t1, const Time& t2) const {
Time result;
int totalSeconds = t1.hours * 3600 + t1.minutes * 60 + t1.seconds +
t2.hours * 3600 + t2.minutes * 60 + t2.seconds;
result.hours = totalSeconds / 3600;
totalSeconds %= 3600;
result.minutes = totalSeconds / 60;
result.seconds = totalSeconds % 60;
return result;
};
int main() {
const Time t1(1, 30, 45);
const Time t2(2, 15, 20);
Time t3;
t3 = t3.add(t1, t2);
cout << "Time 1: ";
t1.display();
cout << endl;
cout << "Time 2: ";
t2.display();
cout << endl;
cout << "Time 3 (sum of Time 1 and Time 2): ";
t3.display();
cout << endl;
return 0;
}
Q12
#include <iostream>
using namespace std;
class Employee {
private:
int employeeNumber;
float compensation;
public:
void setData(int empNum, float comp) {
employeeNumber = empNum;
compensation = comp;
void displayData() const {
cout << "Employee Number: " << employeeNumber << endl;
cout << "Compensation: $" << compensation << endl;
};
int main() {
Employee emp1, emp2, emp3;
int empNum;
float comp;
// Input for Employee 1
cout << "Enter employee number for Employee 1: ";
cin >> empNum;
cout << "Enter compensation for Employee 1: ";
cin >> comp;
emp1.setData(empNum, comp);
// Input for Employee 2
cout << "Enter employee number for Employee 2: ";
cin >> empNum;
cout << "Enter compensation for Employee 2: ";
cin >> comp;
emp2.setData(empNum, comp);
// Input for Employee 3
cout << "Enter employee number for Employee 3: ";
cin >> empNum;
cout << "Enter compensation for Employee 3: ";
cin >> comp;
emp3.setData(empNum, comp);
// Display Employee data
cout << "Employee 1:" << endl;
emp1.displayData();
cout << endl;
cout << "Employee 2:" << endl;
emp2.displayData();
cout << endl;
cout << "Employee 3:" << endl;
emp3.displayData();
cout << endl;
return 0;
}
Q 13
#include <iostream>
using namespace std;
class Date {
private:
int month;
int day;
int year;
public:
void getDate() {
cout << "Enter date (in MM/DD/YY format): ";
char slash;
cin >> month >> slash >> day >> slash >> year;
void showDate() const {
cout << "Date: " << month << "/" << day << "/" << year << endl;
};
int main() {
Date date1; // Object with default initialization
Date date2; // Object to accept user input
date2.getDate(); // Call member function to get date from user
cout << "Date 1 (initialized):" << endl;
date1.showDate();
cout << "Date 2 (user input):" << endl;
date2.showDate();
return 0;
}
Week # 6:
Q-14
#include <iostream>
using namespace std;
class SerialNumberedObject {
private:
static int objectCount; // Static data member to record count of objects created
int serialNumber; // Data member to hold serial number
public:
SerialNumberedObject() {
objectCount++; // Increment object count
serialNumber = objectCount; // Assign serial number based on count
}
void reportSerialNumber() const {
cout << "I am object number " << serialNumber << endl;
};
int SerialNumberedObject::objectCount = 0; // Initialize static data member outside the class
int main() {
SerialNumberedObject obj1, obj2, obj3;
obj1.reportSerialNumber();
obj2.reportSerialNumber();
obj3.reportSerialNumber();
return 0;
}
Q 15
#include <iostream>
#include <sstream>
using namespace std;
class Fraction {
private:
int numerator;
int denominator;
public:
Fraction(int num = 0, int denom = 1) : numerator(num), denominator(denom) {}
void getInput() {
char slash;
cout << "Enter fraction in the form of 'numerator/denominator': ";
cin >> numerator >> slash >> denominator;
void display() const {
cout << numerator << "/" << denominator;
Fraction add(const Fraction& other) const {
int resultNum = numerator * other.denominator + other.numerator * denominator;
int resultDenom = denominator * other.denominator;
return Fraction(resultNum, resultDenom);
};
int main() {
char choice;
Fraction result;
do {
Fraction frac1, frac2;
cout << "Enter first fraction:" << endl;
frac1.getInput();
cout << "Enter second fraction:" << endl;
frac2.getInput();
result = frac1.add(frac2);
cout << "Sum of fractions: ";
result.display();
cout << endl;
cout << "Do you want to enter another set of fractions? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
return 0;