Skip to content

Instantly share code, notes, and snippets.

@Yousif-FJ
Last active June 30, 2022 13:50
Show Gist options
  • Save Yousif-FJ/7227fbfb9524dce76ef6f34fe134a47f to your computer and use it in GitHub Desktop.
Save Yousif-FJ/7227fbfb9524dce76ef6f34fe134a47f to your computer and use it in GitHub Desktop.
Q2/ Create class BOX that have three data members (length, height, and width). And the following function members: • Default Constructor to initialize the data members. • Input: to input values to the data members. • Print: to display the data members values. • Overload (=) operation to assign the values of one BOX objects to another. • Friend f…
#include<iostream>
#include<conio.h>
using namespace std;
class Box
{
public:
Box();
void input();
void print();
void operator = (const Box& b);
friend int CalculateVolume(Box b);
private:
int length, height, width;
};
Box::Box()
{
this->height = 0; this->length = 0; this->width = 0;
}
void Box::input()
{
cout << "Enter length, width and height : ";
cin >> length >> width >> height;
}
void Box::print()
{
cout << "length : " << length << endl;
cout << "width : " << width << endl;
cout << "height : " << height << endl;
}
void Box::operator=(const Box& box)
{
length = box.length; width = box.width; height = box.height;
}
int CalculateVolume(Box box)
{
return box.height * box.length * box.width;
}
void main()
{
Box A;
A.input();
A.print();
cout << "volume = " << CalculateVolume(A) << endl;
cout << "box B, a copy of box A :\n";
Box B;
B = A;
B.print();
_getch();
}
@Yousif-FJ
Copy link
Author

image

@Yousif-FJ
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment