Assignment 01 Object Oriented Programming
Assignment 01 Object Oriented Programming
PDFelement
Assignment 1
oop
1. Define a class student with the following specifica on
#include <iostream>
#include <string>
class Student {
private:
int admno;
string sname;
float eng;
float math;
float science;
float total;
float calculateTotal() {
public:
void takeData() {
cin>> sname;
total = calculateTotal();
void showData() {
};
int main() {
Student student1;
return 0;
#include <string>
Wondershare
PDFelement
class Batsman {
private:
int bcode;
string bname;
int innings;
int notout;
float batavg;
void calcavg() {
if (innings - notout != 0)
else
batavg = 0;
public:
int runs;
void readdata() {
getline(cin, bname);
void displaydata() {
};
int main() {
Batsman player;
player.readdata();
player.displaydata();
return 0;
#include <string>
class TEST {
private:
Wondershare
PDFelement
int TestCode;
int NoCandidate;
int CenterReqd;
int CALCNTR() {
public:
void SCHEDULE() {
CenterReqd = CALCNTR();
void DISPTEST() {
};
Wondershare
PDFelement
int main() {
TEST test;
test.SCHEDULE();
test.DISPTEST();
return 0;
#include <string>
class Flight {
private:
int flightNumber;
float distance;
float fuel;
void calFuel() {
fuel = 500;
fuel = 1100;
else
fuel = 2200;
}
Wondershare
PDFelement
public:
void feedInfo() {
void showInfo() {
cout << "Distance: " << distance << " km" << endl;
cout << "Fuel: " << fuel << " liters" << endl;
};
int main() {
Flight f;
f.feedInfo();
f.showInfo();
return 0;
#include <iostream>
#include <string>
class BOOK {
private:
int bookNo;
char bookTitle[20];
float price;
public:
void INPUT() {
cin.getline(bookTitle, 20);
void PURCHASE() {
int numCopies;
cout << "Total cost to be paid: $" << cost << endl;
};
int main() {
BOOK myBook;
myBook.INPUT();
myBook.PURCHASE();
return 0;
#include <string>
class REPORT {
private:
int adno;
std::string name;
float marks[5];
float average;
void GETAVG() {
float sum = 0;
sum += marks[i];
}
Wondershare
PDFelement
average = sum / 5;
public:
void READINFO() {
std::getline(std::cin, name);
void DISPLAYINFO() {
}
Wondershare
PDFelement
};
int main() {
REPORT student;
// Read informa on
student.READINFO();
// Display informa on
student.DISPLAYINFO();
return 0;
7. Write the defini on for a class called Rectangle that has floa ng point data member’s
length and width. The class has the following member func ons:
#include <iostream>
class Rectangle {
private:
float length;
float width;
public:
length = len;
width = wid;
float perimeter() {
float area() {
void show() {
};
int main() {
rect1.setLength(5);
rect1.setWidth(2.5);
rect1.show();
rect2.setLength(5);
rect2.setWidth(18.9);
rect2.show();
std::cout << "\nAre the two rectangles have the same area? ";
if (rect1.sameArea(rect2))
else
rect1.setLength(15);
rect1.setWidth(6.3);
rect1.show();
std::cout << "\nAre the two rectangles have the same area now? ";
if (rect1.sameArea(rect2))
else
return 0;
Wondershare
PDFelement
8. Write the defini on for a class called complex that has floa ng point data members for
storing real and imaginary parts.
#include <iostream>
class Complex {
private:
float real;
float imaginary;
public:
// Constructor
Complex() {
real = 0.0;
imaginary = 0.0;
real = r;
imaginary = i;
void disp() {
cout << real << " + " << imaginary << "i" << endl;
}
Wondershare
PDFelement
// Member func on to sum two complex numbers & return complex number
Complex sum(Complex c) {
Complex temp;
return temp;
};
int main() {
c1.set(2.5, 3.7);
c2.set(1.8, 4.2);
c3 = c1.sum(c2);
c1.disp();
c2.disp();
c3.disp();
Wondershare
PDFelement
return 0;
9. Write the defini on for a class called Distance that has data member feet as integer and
inches as float
#include <iostream>
class Distance {
private:
int feet;
float inches;
public:
feet = ;
inches = in;
void disp() {
std::cout << "Distance: " << feet << " feet and " << inches << " inches" << std::endl;
Distance add(Distance d) {
Distance result;
return result;
};
int main() {
d1.disp();
d2.disp();
d3.disp();
return 0;
10. Write the defini on for a class called me that has hours and minutes as integer
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
Wondershare
PDFelement
public:
hours = h;
minutes = m;
void showTime() {
cout << "Time: " << hours << " hours and " << minutes << " minutes" << endl;
Time sum(Time t) {
Time result;
result.minutes %= 60;
return result;
};
int main() {
t1.setTime(3, 45);
t2.setTime(2, 30);
Wondershare
PDFelement
t3 = t1.sum(t2);
t1.showTime();
t2.showTime();
cout << "Sum of First and Second Time Objects:" << endl;
t3.showTime();
return 0;
#include <iostream>
int main() {
// Execute Func on 1
Seminar seminar1;
// Execute Func on 3
Seminar seminar2(45);
return 0;
}
Wondershare
PDFelement
```
(iii) Func on 1 and Func on 3 together illustrate the concept of constructor overloading. Constructor
overloading allows mul ple constructors within a class, each with a different signature (different
parameters). This enables the crea on of objects in different ways, providing flexibility and
customiza on.
2. Answer the ques ons (i) and (ii) a er going through the following class:
(i) To execute Func on 1, Func on 2, Func on 3, and Func on 4 of the class `Test`, you can do the
following:
#include <iostream>
#include <cstring>
int main() {
// Execute Func on 1
Test test1;
// Execute Func on 2
Test test2("Physics");
// Execute Func on 3
Test test3(90);
// Execute Func on 4
return 0;
}
Wondershare
PDFelement
(ii) Func on 1, Func on 2, Func on 3, and Func on 4 together demonstrate the concept of constructor
overloading in Object-Oriented Programming. Constructor overloading allows the crea on of objects
using different constructors, each accep ng different parameters. This provides flexibility in object
ini aliza on and allows for customiza on based on the parameters passed during object crea on.
Sample::Sample() // Constructor 1
x = 0;
y = 0.0;
x = xValue;
y = 0.0;
x = xValue;
y = sta c_cast<double>(yValue);
x = xValue;
y = yValue;
Explana on:
- Constructor 1 ini alizes both private member variables `x` and `y` to 0.
- Constructor 2 ini alizes `x` according to the value of the parameter `xValue`, and `y` is ini alized to 0.
- Constructor 3 ini alizes `x` according to the value of the parameter `xValue`, and `y` is ini alized by
conver ng the `int` parameter `yValue` to a `double`.
- Constructor 4 ini alizes both `x` and `y` according to the values of the parameters `xValue` and
`yValue`, respec vely.
4. A common place to buy candy is from a machine. The machine sells candies, chips, gum,
and cookies. You have been asked to write a program for this candy machine.
#include <iostream>
class cashRegister {
private:
int cashOnHand;
public:
cashRegister() {
cashOnHand = 500;
cashRegister(int cash) {
cashOnHand = cash;
int getCurrentBalance() {
Wondershare
PDFelement
return cashOnHand;
cashOnHand += amount;
};
class dispenserType {
private:
int numberOfItems;
int cost;
public:
dispenserType() {
numberOfItems = 50;
cost = 50;
numberOfItems = items;
cost = itemCost;
int getNoOfItems() {
return numberOfItems;
int getCost() {
Wondershare
PDFelement
return cost;
void makeSale() {
if (numberOfItems > 0)
numberOfItems--;
};
cout << "Select a product by entering its corresponding number." << endl;
case 1:
cout << "You've selected Candy. Cost: $" << product.getCost() << endl;
break;
case 2:
cout << "You've selected Chips. Cost: $" << product.getCost() << endl;
Wondershare
PDFelement
break;
case 3:
cout << "You've selected Gum. Cost: $" << product.getCost() << endl;
break;
case 4:
cout << "You've selected Cookies. Cost: $" << product.getCost() << endl;
break;
default:
return;
int amount;
product.makeSale();
registerMachine.acceptAmount(product.getCost());
} else {
int main() {
cashRegister registerMachine;
Wondershare
PDFelement
showSelec on();
sellProduct(products[0], registerMachine);
return 0;