C++ Program to calculate Volume of Cube using constructor

By | 31.12.2016

Volume of Cube using constructor


Write a C++ Program to calculate Volume of Cube using constructor and destructor. Here’s a Simple Program to calculate Volume of Cube using constructor and destructor in C++ Programming Language.


What is Class and Objects in C++?


  • Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  • The variables inside class definition are called as data members and the functions are called member functions.
  • Class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.

  • Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
  • Each object has different data variables. Objects are initialized using special class functions called Constructors.

Below is the source code for C++ Program to calculate Volume of Cube using constructor and destructor which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to calculate Volume of Cube using constructor  */

#include<iostream>

using namespace std;



class cube

{

            public:

                        double side;

                        double volume()

                        {

                            return(side*side*side);

                        }

                        cube(double side1)

                        {

                            cout << "\nA constructor is called" << endl;

                            side=side1;

                        }

                        cube()

                        {

                            cout << "\nA default constructor is called " << endl;

                        }

                        ~cube()

                        {

                            cout << "\nDestructing " << side << endl;

                        }

};



int main()

{

            cube c1(2.34);

            cube c2;

            cout << "\nThe side of the cube is: " << c1.side << endl;

            cout << "\nThe volume of the first cube is : " << c1.volume() << endl;

            cout << "\nEnter the length of the second cube : " ;

            cin >> c2.side;

            cout << "\nThe volume of second cube is : " << c2.volume() << endl;

            return(0);

}

OUTPUT : :


/*  C++ Program to calculate Volume of Cube using constructor  */

A constructor is called

A default constructor is called

The side of the cube is: 2.34

The volume of the first cube is : 12.8129

Enter the length of the second cube : 4

The volume of second cube is : 64

Destructing 4

Destructing 2.34

Process returned 0

Above is the source code and output for C++ Program to calculate Volume of Cube using constructor which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

 

4.8 8 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments