0% found this document useful (0 votes)
3 views5 pages

Module 1 Program 2

The document provides a C++ program that defines a Box class with data members for length, breadth, and height, along with member functions to calculate volume and compare volumes between two Box objects. The main function creates two Box objects, prompts the user for their dimensions, displays their volumes, and compares them. The program demonstrates encapsulation by protecting data and manipulating it through member functions.

Uploaded by

Bhagya S G
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Module 1 Program 2

The document provides a C++ program that defines a Box class with data members for length, breadth, and height, along with member functions to calculate volume and compare volumes between two Box objects. The main function creates two Box objects, prompts the user for their dimensions, displays their volumes, and compares them. The program demonstrates encapsulation by protecting data and manipulating it through member functions.

Uploaded by

Bhagya S G
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2.

Write a C++ program to:


a) Create a class Box with data members: length, breadth, height. B
b) Define a member function volume() to calculate and return the volume
of the box.
c) Define another member function, compareVolume (Box b), to compare
the volume of the current object with another Box object passed as an
argument.
d) Create two Box objects and display their volumes and comparison
results.

// Program to create a class Box and compare the volumes of two Box objects
#include <iostream>
using namespace std;

// Class definition
class Box {
// Data members
float length, breadth, height;

public:
// Member function to read box dimensions
void getData() {
cout << "Enter length, breadth and height of the box: ";
cin >> length >> breadth >> height;
}

// Member function to calculate volume


float volume() {
return length * breadth * height;
}
// Member function to compare volumes
void compareVolume(Box b) {
float v1 = volume(); // volume of current object
float v2 = [Link](); // volume of the passed object

cout << "\nComparing volumes of two boxes:\n";


if (v1 > v2)
cout << "Box 1 is larger than Box 2\n";
else if (v1 < v2)
cout << "Box 2 is larger than Box 1\n";
else
cout << "Both boxes have equal volume\n";
}
};

// Main function
int main() {
Box box1, box2; // Object declarations

cout << "Enter details for Box 1\n";


[Link]();

cout << "\nEnter details for Box 2\n";


[Link]();
// Display volumes
cout << "\nVolume of Box 1 = " << [Link]();
cout << "\nVolume of Box 2 = " << [Link]() << endl;

// Compare volumes
[Link](box2);

return 0;
}
OUTPUT:
Enter details for Box 1
Enter length, breadth and height of the box: 4 5 6

Enter details for Box 2


Enter length, breadth and height of the box: 3 5 7

Volume of Box 1 = 120


Volume of Box 2 = 105

Comparing volumes of two boxes:


Box 1 is larger than Box 2
Program Explanation:
he provided C++ code defines a Box class to represent a rectangular box.
Here's a breakdown of its components and functionality:
Class Box:
• Data Members:
o length, breadth, height: These are private floating-point variables
that store the dimensions of the box.
• Public Member Functions:
o getData(): This function prompts the user to enter the length,
breadth, and height of the box and stores these values in the
corresponding data members.
o volume(): This function calculates and returns the volume of the box
using the formula length * breadth * height.
o compareVolume(Box b): This function takes another Box object
(b) as input. It then calculates the volume of the current box (v1) and
the volume of the passed box (v2). Finally, it
compares v1 and v2 and prints a message indicating which box has
a larger volume or if they have equal volumes.
main Function:
• Object Declarations:
o Box box1, box2;: Two Box objects, box1 and box2, are created.
• Getting Data:
o [Link]();: Calls the getData() function for box1 to get its
dimensions from the user.
o [Link]();: Calls the getData() function for box2 to get its
dimensions from the user.
• Displaying Volumes:
o cout << "\nVolume of Box 1 = " << [Link]();: Calculates and
prints the volume of box1.
o cout << "\nVolume of Box 2 = " << [Link]() << endl;:
Calculates and prints the volume of box2.
• Comparing Volumes:
o [Link](box2);: Calls the compareVolume() function
on box1, passing box2 as an argument, to compare their volumes.
• Return 0: Indicates successful program execution.
In essence, this program:
1. Defines a blueprint (Box class) for creating box objects.
2. Allows users to input dimensions for two separate boxes.
3. Calculates and displays the volume of each box.
4. Compares the volumes of the two boxes and informs the user which one is
larger or if they are equal.

Program Explanation
Concept Description

The Box class encapsulates data members length, breadth, and


Class Definition
height.

Member getData() inputs dimensions, volume() calculates and returns


Functions volume, compareVolume(Box b) compares two Box objects.

Object
[Link](box2) compares the two objects.
Interaction

Encapsulation Data is protected and manipulated only via member functions.

You might also like