0% found this document useful (0 votes)
3 views

Assignment 01 Object Oriented Programming

Uploaded by

hussoali12345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 01 Object Oriented Programming

Uploaded by

hussoali12345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Wondershare

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() {

return eng + math + science;

public:

void takeData() {

cout << "Enter admission number: ";

cin >> admno;

cout << "Enter student name: ";

cin>> sname;

cout << "Enter marks in English: ";


Wondershare
PDFelement

cin >> eng;

cout << "Enter marks in Math: ";

cin >> math;

cout << "Enter marks in Science: ";

cin >> science;

total = calculateTotal();

// Func on to display all data members on the screen

void showData() {

std::cout << "Admission Number: " << admno << std::endl;

std::cout << "Student Name: " << sname << std::endl;

std::cout << "Marks in English: " << eng << std::endl;

std::cout << "Marks in Math: " << math << std::endl;

std::cout << "Marks in Science: " << science << std::endl;

std::cout << "Total Marks: " << total << std::endl;

};

int main() {

Student student1;

student1.takeData(); // Input data for student

std::cout << "\nStudent Details:\n";

student1.showData(); // Display student details

return 0;

2. Define a class batsman with the following specifica ons:


#include <iostream>

#include <string>
Wondershare
PDFelement

using namespace std;

class Batsman {

private:

int bcode;

string bname;

int innings;

int notout;

float batavg;

void calcavg() {

if (innings - notout != 0)

batavg = float(runs) / (innings - notout);

else

batavg = 0;

public:

int runs;

void readdata() {

cout << "Enter Batsman code: ";

cin >> bcode;

cin.ignore(); // to clear the input buffer

cout << "Enter Batsman name: ";

getline(cin, bname);

cout << "Enter Innings: ";

cin >> innings;

cout << "Enter Not Out: ";

cin >> notout;


Wondershare
PDFelement

cout << "Enter Runs: ";

cin >> runs;

calcavg(); // Invoke func on to calculate ba ng average

void displaydata() {

cout << "Batsman Code: " << bcode << endl;

cout << "Batsman Name: " << bname << endl;

cout << "Innings: " << innings << endl;

cout << "Not Out: " << notout << endl;

cout << "Runs: " << runs << endl;

cout << "Ba ng Average: " << batavg << endl;

};

int main() {

Batsman player;

player.readdata();

cout << "\nBatsman Details:\n";

player.displaydata();

return 0;

3. Define a class TEST in C++ with following descrip on:


#include <iostream>

#include <string>

using namespace std;

class TEST {

private:
Wondershare
PDFelement

int TestCode;

string Descrip on;

int NoCandidate;

int CenterReqd;

int CALCNTR() {

return (NoCandidate / 100 + 1);

public:

void SCHEDULE() {

cout << "Enter Test Code: ";

cin >> TestCode;

cout << "Enter Descrip on: ";

cin.ignore(); // Clearing buffer

getline(cin, Descrip on);

cout << "Enter Number of Candidates: ";

cin >> NoCandidate;

CenterReqd = CALCNTR();

void DISPTEST() {

cout << "Test Code: " << TestCode << endl;

cout << "Descrip on: " << Descrip on << endl;

cout << "Number of Candidates: " << NoCandidate << endl;

cout << "Centers Required: " << CenterReqd << endl;

};
Wondershare
PDFelement

int main() {

TEST test;

test.SCHEDULE();

cout << "\nTest Details:\n";

test.DISPTEST();

return 0;

4. Define a class in C++ with following descrip on:


#include <iostream>

#include <string>

using namespace std;

class Flight {

private:

int flightNumber;

string des na on;

float distance;

float fuel;

void calFuel() {

if (distance <= 1000)

fuel = 500;

else if (distance > 1000 && distance <= 2000)

fuel = 1100;

else

fuel = 2200;

}
Wondershare
PDFelement

public:

void feedInfo() {

cout << "Enter Flight Number: ";

cin >> flightNumber;

cin.ignore(); // to clear the input buffer

cout << "Enter Des na on: ";

getline(cin, des na on);

cout << "Enter Distance: ";

cin >> distance;

calFuel(); // Calculate fuel based on distance

void showInfo() {

cout << "\nFlight Number: " << flightNumber << endl;

cout << "Des na on: " << des na on << endl;

cout << "Distance: " << distance << " km" << endl;

cout << "Fuel: " << fuel << " liters" << endl;

};

int main() {

Flight f;

f.feedInfo();

f.showInfo();

return 0;

5. Define a class BOOK with the following specifica ons :


Wondershare
PDFelement

#include <iostream>

#include <string>

using namespace std;

class BOOK {

private:

int bookNo;

char bookTitle[20];

float price;

float totalCost(int numCopies) {

return price * numCopies;

public:

void INPUT() {

cout << "Enter Book No: ";

cin >> bookNo;

cin.ignore(); // Clear input buffer

cout << "Enter Book Title: ";

cin.getline(bookTitle, 20);

cout << "Enter Price: ";

cin >> price;

void PURCHASE() {

int numCopies;

cout << "Enter the number of copies to be purchased: ";

cin >> numCopies;


Wondershare
PDFelement

float cost = totalCost(numCopies);

cout << "Total cost to be paid: $" << cost << endl;

};

int main() {

BOOK myBook;

myBook.INPUT();

myBook.PURCHASE();

return 0;

6. Define a class REPORT with the following specifica on:


#include <iostream>

#include <string>

class REPORT {

private:

int adno;

std::string name;

float marks[5];

float average;

// Private func on to compute average marks

void GETAVG() {

float sum = 0;

for (int i = 0; i < 5; ++i) {

sum += marks[i];

}
Wondershare
PDFelement

average = sum / 5;

public:

// Func on to read informa on and compute average

void READINFO() {

std::cout << "Enter admission number (4 digits): ";

std::cin >> adno;

std::cin.ignore(); // Clear input buffer

std::cout << "Enter name (up to 20 characters): ";

std::getline(std::cin, name);

std::cout << "Enter marks for 5 subjects: ";

for (int i = 0; i < 5; ++i) {

std::cin >> marks[i];

GETAVG(); // Compute average a er reading marks

// Func on to display informa on

void DISPLAYINFO() {

std::cout << "Admission Number: " << adno << std::endl;

std::cout << "Name: " << name << std::endl;

std::cout << "Marks: ";

for (int i = 0; i < 5; ++i) {

std::cout << marks[i] << " ";

std::cout << std::endl;

std::cout << "Average Marks: " << average << std::endl;

}
Wondershare
PDFelement

};

int main() {

REPORT student;

// Read informa on

student.READINFO();

// Display informa on

std::cout << "\nStudent Report:\n";

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:

void setLength(float len) {

length = len;

void setWidth(float wid) {


Wondershare
PDFelement

width = wid;

float perimeter() {

return 2 * (length + width);

float area() {

return length * width;

void show() {

std::cout << "Length: " << length << std::endl;

std::cout << "Width: " << width << std::endl;

int sameArea(Rectangle other) {

return (area() == other.area()) ? 1 : 0;

};

int main() {

Rectangle rect1, rect2;

rect1.setLength(5);

rect1.setWidth(2.5);

std::cout << "Rectangle 1:" << std::endl;

rect1.show();

std::cout << "Area: " << rect1.area() << std::endl;


Wondershare
PDFelement

std::cout << "Perimeter: " << rect1.perimeter() << std::endl;

rect2.setLength(5);

rect2.setWidth(18.9);

std::cout << "\nRectangle 2:" << std::endl;

rect2.show();

std::cout << "Area: " << rect2.area() << std::endl;

std::cout << "Perimeter: " << rect2.perimeter() << std::endl;

std::cout << "\nAre the two rectangles have the same area? ";

if (rect1.sameArea(rect2))

std::cout << "Yes" << std::endl;

else

std::cout << "No" << std::endl;

rect1.setLength(15);

rect1.setWidth(6.3);

std::cout << "\nRectangle 1 (updated):" << std::endl;

rect1.show();

std::cout << "Area: " << rect1.area() << std::endl;

std::cout << "Perimeter: " << rect1.perimeter() << std::endl;

std::cout << "\nAre the two rectangles have the same area now? ";

if (rect1.sameArea(rect2))

std::cout << "Yes" << std::endl;

else

std::cout << "No" << std::endl;

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>

using namespace std;

class Complex {

private:

float real;

float imaginary;

public:

// Constructor

Complex() {

real = 0.0;

imaginary = 0.0;

// Member func on to set the specified value in object

void set(float r, float i) {

real = r;

imaginary = i;

// Member func on to display complex number object

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;

temp.real = real + c.real;

temp.imaginary = imaginary + c.imaginary;

return temp;

};

int main() {

Complex c1, c2, c3;

// Set values in two objects

c1.set(2.5, 3.7);

c2.set(1.8, 4.2);

// Calculate sum and assign it in third object

c3 = c1.sum(c2);

// Display all complex numbers

cout << "Complex number 1: ";

c1.disp();

cout << "Complex number 2: ";

c2.disp();

cout << "Sum of complex numbers: ";

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:

void set(int , float in) {

feet = ;

inches = in;

void disp() {

std::cout << "Distance: " << feet << " feet and " << inches << " inches" << std::endl;

Distance add(Distance d) {

Distance result;

result.feet = feet + d.feet;

result.inches = inches + d.inches;

if (result.inches >= 12) {

result.feet += sta c_cast<int>(result.inches / 12);

result.inches = fmod(result.inches, 12);


Wondershare
PDFelement

return result;

};

int main() {

Distance d1, d2, d3;

d1.set(5, 6.5); // 5 feet 6.5 inches

d2.set(3, 8.25); // 3 feet 8.25 inches

d3 = d1.add(d2); // Sum of d1 and d2 stored in d3

std::cout << "Distance 1: ";

d1.disp();

std::cout << "Distance 2: ";

d2.disp();

std::cout << "Sum of Distance 1 and Distance 2: ";

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:

// Func on to set the specified value in the object

void setTime(int h, int m) {

hours = h;

minutes = m;

// Func on to display the me object

void showTime() {

cout << "Time: " << hours << " hours and " << minutes << " minutes" << endl;

// Func on to sum two me objects and return the result

Time sum(Time t) {

Time result;

result.minutes = minutes + t.minutes;

result.hours = hours + t.hours + result.minutes / 60;

result.minutes %= 60;

return result;

};

int main() {

Time t1, t2, t3;

// Set values in the first two objects

t1.setTime(3, 45);

t2.setTime(2, 30);
Wondershare
PDFelement

// Calculate sum and assign it to the third object

t3 = t1.sum(t2);

// Display all me objects

cout << "First Time Object:" << endl;

t1.showTime();

cout << "Second Time Object:" << endl;

t2.showTime();

cout << "Sum of First and Second Time Objects:" << endl;

t3.showTime();

return 0;

Constructor and De-constructor


1. Answer the ques ons (i) and (iii) a er going through the following class:
(i) To execute Func on 1 and Func on 3 of the class `Seminar`, you can do the following:

#include <iostream>

using namespace std;

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>

using namespace std;

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

Test test4("Math", 85);

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.

3. Consider the defini on of the following class:


class Sample { private: int x; double y; public : Sample(); //Constructor 1 Sample(int); //Constructor 2
Sample(int, int); //Constructor 3 Sample(int, double); //Constructor 4

Here are the defini ons for the constructors:

Sample::Sample() // Constructor 1

x = 0;

y = 0.0;

Sample::Sample(int xValue) // Constructor 2

x = xValue;

y = 0.0;

Sample::Sample(int xValue, int yValue) // Constructor 3

x = xValue;

y = sta c_cast<double>(yValue);

Sample::Sample(int xValue, double yValue) // Constructor 4


Wondershare
PDFelement

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>

using namespace std;

class cashRegister {

private:

int cashOnHand;

public:

cashRegister() {

cashOnHand = 500;

cashRegister(int cash) {

cashOnHand = cash;

int getCurrentBalance() {
Wondershare
PDFelement

return cashOnHand;

void acceptAmount(int amount) {

cashOnHand += amount;

};

class dispenserType {

private:

int numberOfItems;

int cost;

public:

dispenserType() {

numberOfItems = 50;

cost = 50;

dispenserType(int items, int itemCost) {

numberOfItems = items;

cost = itemCost;

int getNoOfItems() {

return numberOfItems;

int getCost() {
Wondershare
PDFelement

return cost;

void makeSale() {

if (numberOfItems > 0)

numberOfItems--;

};

void showSelec on() {

cout << "Products Available: " << endl;

cout << "1. Candy" << endl;

cout << "2. Chips" << endl;

cout << "3. Gum" << endl;

cout << "4. Cookies" << endl;

cout << "Select a product by entering its corresponding number." << endl;

void sellProduct(dispenserType &product, cashRegister &registerMachine) {

int selec on;

cout << "Enter your selec on: ";

cin >> selec on;

switch (selec on) {

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:

cout << "Invalid selec on." << endl;

return;

int amount;

cout << "Please deposit the money: ";

cin >> amount;

if (amount >= product.getCost()) {

product.makeSale();

registerMachine.acceptAmount(product.getCost());

cout << "Thank you for your purchase!" << endl;

} else {

cout << "Insuffi cient funds. Transac on canceled." << endl;

int main() {

dispenserType products[4]; // Assuming there are 4 products

cashRegister registerMachine;
Wondershare
PDFelement

showSelec on();

sellProduct(products[0], registerMachine);

return 0;

You might also like