Find Box Volume using Objects in C++


This program defines a cuboid class with three public member variables representing the length, breadth, and height of a cuboid. It then creates two cuboid objects, Box1 and Box2, and sets the values of their member variables. Finally, it calculates and prints the volume of each box.

This is a simple program that demonstrates how to define a class and create objects of that class. However, it does not include any member functions or methods that perform operations on the cuboid objects, so it is limited in what it can do.

Source Code

#include<iostream>
using namespace std;
class cuboid
{
   public:
      float length;
      float breadth;
      float height;
};
int main()
{
  cuboid Box1;
  cuboid Box2;
  float volume = 0.0;
  Box1.height = 5.0;
  Box1.length = 6.0;
  Box1.breadth = 7.0;
  Box2.height = 10.0;
  Box2.length = 12.0;
  Box2.breadth = 13.0;
  volume=Box1.height*Box1.length*Box1.breadth;
  cout<<"\nVolume of Box1 : "<<volume<<endl;
  volume=Box2.height*Box2.length*Box2.breadth;
  cout<<"\nVolume of Box2 : "<<volume<<endl;
  return 0;
}
To download raw file Click Here

Output

Volume of Box1 : 210

Volume of Box2 : 1560

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples