Structs are blueprints in CPP. Like classes, CPP structs are used to create instance of a class, but are preferred only for lightweight objects which are not intended to any future modifications. The lightweight objects include, Rectangle, color, Point etc. A CPP class uses Call by Value method for calling while, CPP structs uses Call by Reference method for calling.
Example:
#include <iostream.h> using namespace std; struct Sphere { int radius; }; int main() { struct Sphere circle; circle.radius=8; cout<<"Area of Sphere is: "<<(4*3.14* circle.radius*circle.radius)<<endl; return 0; } |
Output
Area of Sphere is: 803.84 |