Answer to Question #205103 in C++ for Ahmad

Question #205103

Design a class Cube which has following data members.

 Length

 Width

 Height

A) Define a Member function of class

 Set record () which sets record.

B) Also define following functions

 Volume () (friend function) which calculates volume of cube.

 Display () (friend function) which displays the data members and volume.

C) execute friend functions in main to execute different functionality


1
Expert's answer
2021-06-09T16:03:28-0400
#include <iostream>
using namespace std;
class Cube{
    float length, width, height;
    public:
        Cube(){}
        void set_record(){
            cout<<"Input length: "; cin>>length;
            cout<<"Input width: "; cin>>width;
            cout<<"Input height: "; cin>>height;
        }
        friend float volume(const Cube &);
        friend void display(const Cube &);
};
float volume(const Cube &other){
    return other.length * other.width * other.height;
}
void display(const Cube &other){
    cout<<"Length: "<<other.length<<endl;
    cout<<"Width: "<<other.width<<endl;
    cout<<"Height: "<<other.height<<endl;
}
int main(){
    Cube cube;
    cube.set_record();
    display(cube);
    cout<<"Volume: "<<volume(cube)<<endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog