Create a class named Cube having the following attributes: length(double), Base (double), height (double)

  

We are learning about the following things by this example:

  • Classes in C++
  • Constructors
  • Parameterized Constructors
  • Generic Data Input

 

Create a class named Cube having the following attributes: length(double), Base (double), height (double)

Task Example:

QUESTION:

Create a class named Cube having the following attributes:

1.        length(double)

2.      Base (double)

3.      height (double)

 

•       Write a function input () that takes input from the user.

•       Write a non-returning display () function to print.

•       Write an inline function calculate volume() which calculates the volume of the cube.

 


Solution

#include <iostream>

using namespace std;

class Cube

{

private:

            double length, width, height;

public:

 

            Cube()

            {

                        length = 0.0;

                        width = 0.0;

                        height = 0.0;

            }

            Cube(double ln, double wd, double ht)

            {

                        length = ln;

                        width = wd;

                        height = ht;

            }

 

            void Setlength(double ln)

            {

                        length = ln;

            }

            void Setwidth(double wd)

            {

                        width = wd;

            }

            void Setheight(double ht)

            {

                        height = ht;

            }

 

            double Getlength()

            {

                        return length;

            }

            double Getwidth()

            {

                        return width;

            }

            double Getheight()

            {

                        return height;

            }

            inline void display()

            {

                        double volume;

                        volume = length*width*height;

 

                        cout << "Volume of Cube is: " << volume << endl;

            }

 

            ~Cube()   //destructor

            {

 

            }

};

int main()

{

            double a, b, c;

            cout << "Enter Length: ";

            cin >> a;

            cout << "Enter Width: ";

            cin >> b;

            cout << "Enter Height: ";

            cin >> c;

            Cube obj;

            obj.Setlength(a);

            obj.Setwidth(b);

            obj.Setheight(c);

            obj.display();

 

            system("Pause");

            return 0;

}

 



 

That's exactly what today's post is all about. We hope you'll like it. You can also message us to see more posts, and if you like it, be sure to share it with your friends. Thank you very much for reading.

 

-----------------------------------------------------------------------------------

Post a Comment

0 Comments