Advertisement
AlexLeo

An easier way to initialize struct of C++

Nov 6th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef struct
  7. {
  8.     int len;
  9.     int width;
  10.  
  11.     int area()
  12.     {
  13.         return len*width;
  14.     }
  15. }Rectangle;
  16.  
  17. int main()
  18. {
  19.     //Rectangle r1;
  20.     //r1.len=5;
  21.     //r1.width=10;
  22.     Rectangle r1 = {5, 10};
  23.  
  24.     cout << "Len=\t" << r1.len << '\n';
  25.     cout << "Width=\t" << r1.width << '\n';
  26.     cout << "Area=\t" << r1.area() << '\n';
  27.  
  28.     // system("pause");
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement