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
#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;
}
Comments
Leave a comment