Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /*
- * C++: class vs struct
- */
- // class
- class foo {
- //public: // Group members as public/private to make this transparent.
- int x, y;
- void print(int, int);
- };
- void foo::print(int x, int y){
- printf("%d\n", x * y);
- }
- // struct
- struct bar {
- int x, y;
- void print(int, int);
- };
- void bar::print(int x, int y){
- printf("%d\n", x * y);
- }
- int main () {
- //HINT: It will not compile if you try foo instead of bar.
- //This is because classes have private members by default. A struct has public by default
- //This is the only difference between classes and structs in C++.
- //foo ex;
- bar ex;
- ex.x = 6;
- ex.y = 7;
- ex.print(ex.x, ex.y);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment