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.
Source Code
#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); cout<<endl <<"Size of first Box object is : \n" <<firstBox.length <<firstBox.breadth <<firstBox.height <<endl; cout<<"Volume of first Box Object is : \n"<<firstBoxVolume <<endl; 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; cout<<endl <<"Size of second Box object is : \n" <<secondBox.length <<secondBox.breadth <<secondBox.height <<endl; cout<<"Volume of second Box Object is : \n"<<volume(secondBox) <<endl; return 0; } //Function to calculate the volume of a box double volume(const Box& aBox) { return aBox.length * aBox.breadth * aBox.height; }
informative blog
ReplyDelete