Thursday, 24 July 2014

Write a Program in C++ which use Structure

Structure of the Problem Requirements 

Structure are use to organize and define your own data type in C++. Structure are just like class in C++ but class is more organize and safe as compare to structure . The main difference between class and structure is in their Encapsulation of data . Here is a very simple example of structure which define its way of defining and putting data in it .

Source Code

//Using a Box Structure

#include <iostream>

using namespace std;

//Structure to Represent a Box

struct Box
double length;
double height;
double breadth;

//Prototype of function to calculate the volume of a box
double volume (const Box& aBox); 
int main()
cout<<"\n\n\n **************** STRUCTURE IN C++ ******************\n\n\n";
Box firstBox = {40.0, 30.0, 10.0};

//Calculate the volume of the box
double firstBoxVolume = volume(firstBox);

<<"Size of first Box object is :  \n"
cout<<"Volume of first Box Object is : \n"<<firstBoxVolume

Box secondBox = firstBox; //Create a secondBox object the same as firstBox
//Increase the dimensions of second Box object by 10
secondBox.length  += 10;
secondBox.breadth += 10;
secondBox.height  += 10;

<<"Size of second Box object is : \n"
cout<<"Volume of second Box Object is : \n"<<volume(secondBox)

return 0;

//Function to calculate the volume of a box

double volume(const Box& aBox)
return aBox.length * aBox.breadth * aBox.height;

Output of the Program

C++ Structure Programming
Structure Programming

Share it Please

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.