nsmartt

struct vs class.cpp

Apr 26th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /*
  4.  * C++: class vs struct
  5.  */
  6.  
  7. // class
  8. class foo {
  9. //public: // Group members as public/private to make this transparent.
  10.   int x, y;
  11.   void print(int, int);
  12. };
  13.  
  14. void foo::print(int x, int y){
  15.   printf("%d\n", x * y);
  16. }
  17.  
  18. // struct
  19. struct bar {
  20.   int x, y;
  21.   void print(int, int);
  22. };
  23.  
  24. void bar::print(int x, int y){
  25.   printf("%d\n", x * y);
  26. }
  27.  
  28.  
  29. int main () {
  30.   //HINT: It will not compile if you try foo instead of bar.
  31.   //This is because classes have private members by default. A struct has public by default
  32.   //This is the only difference between classes and structs in C++.
  33.   //foo ex;
  34.   bar ex;
  35.   ex.x = 6;
  36.   ex.y = 7;
  37.   ex.print(ex.x, ex.y);
  38.   return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment