Create and implement a program in C++ to define a BOX class and a AREA_VOL class.

Question

1. Create and implement a program in C++ to define a BOX class and a AREA_VOL class. In the class BOX, data member should be length. Member function should set the value of data member (user defined or by passing argument to the member function). Calculate and display the area and volume of the box in AREA_VOL class using friend function. (Note: class AREA_VOL should not be friend class of BOX, but only member function/functions of class AREA_VOL can be friend function/functions of class BOX)

2. Create and Implement program in C++ to define 2 classes i.e. A and B. Declare an integer ‘a’ variables in class A and integer ‘b’ in class B. B class should be friend of class A. Division (a/b), power (a^b) and multiplication (a*b) of variables a and b must be performed using member function/ functions of class B.

 

Summary

  1. Basically, the length of a box is measured, then the area and volume of the box are calculated and printed to the console. The concept of classes, as well as friend functions, are used here.
  2. There are two classes in the given program: A and B, with B being a friend class of A. The goal of this program is to demonstrate the idea of friend class.

Explanation

  1. First, the class BOX is created, with the attribute length indicating the box’s length. Then, given the input passed to it, a function called setLength is defined, which sets the length of the BOX. The area vol header function is then declared. The buddy function area vol is implemented in the class AREA VOL, and its definition is it, which calculates and prints the BOX’s area and volume.
  2. Class ‘A’ has a member named ‘a,’ while class B has a member named ‘b.’ It’s an A-class friend. As a result, it has access to class A’s secret members. In class B, the functions division, power, and multiplication are defined.

Code

#include <iostream>
using namespace std;
class BOX
{
    public:
    int length;
    void setLength(int l){
        length=l;
    }
    void area_vol(BOX& b);
};
class AREA_VOL
{
    public:
    friend void BOX::area_vol(BOX& b);
};
void BOX::area_vol(BOX& b){
    cout<<"Area = "<<b.length*b.length<<endl;
    cout<<"Volume = "<<b.length*b.length*b.length<<endl<<endl;
}
int main()
{
    BOX b;
    b.setLength(5);
    b.area_vol(b);
}

Output

BOX class and a AREA_VOL class

 

Code 2

#include <iostream>
using namespace std;
#include <cmath>
class A 
{
    private:
    int a;
    public:
    A(int a1){a=a1;}
    friend class B;
};
class B 
{
    private:
    int b;
    public:
    B(int b1){b=b1;}
    void Division(A& obj1){
        cout<<"a/b = "<<obj1.a/b<<endl;
    }
    void power(A& obj1){
        cout<<"a^b = "<<pow(obj1.a,b)<<endl;
    }
    void multiplication(A& obj1){
        cout<<"a*b = "<<obj1.a*b<<endl;
    }
};
int main()
{
    A a1(2);
    B b1(3);
    b1.Division(a1);
    b1.power(a1);
    b1.multiplication(a1);
}

Output

 

Also, read the Given below-defined UML class diagram.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *