1

Can we implement all the concepts of OOPS using the keyword struct? The answer from here is YES! But I don't understand how? Please Tell!

Yash Jain
  • 1,091
  • 1
  • 8
  • 9
  • 4
    In c++ a struct **is** a class with a couple of tweaks. More here: http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c – user4581301 May 09 '16 at 23:42
  • 1
    which OOP concept that you can implement with `class` but unable with `struct`? – Bryan Chen May 09 '16 at 23:46
  • Actually my son during his initial course of CS in the University implemented a complete OOP system in pure C :-) – user3159253 May 09 '16 at 23:47
  • 1
    can't really do it in C++ either. member functions fall very short of message sending. – Ryan Haining May 10 '16 at 00:11
  • Well you have to use some other keywords as well, e.g. `const`, `virtual`, `private` etc. in order to implement all OO concepts – M.M May 10 '16 at 00:45

2 Answers2

3

A struct is a class, only all attributes/methods are public by default in the struct. You can very well have methods and attributes in a struct. You can also inherit from a struct (default inheritance is public).

So basically, yes, you can use struct to build an OOP.

excalibur1491
  • 1,201
  • 2
  • 14
  • 29
0

With a C struct no! The key concept to OO is polymorphism.

To consider a language to be implementing OO you need a runtime dispatch of messages or function calls. In c++ this is implemented by using the virtual keyword.

Now this might well be a trick question as struct and class in c++ only differs by default visibility of members.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • The struct can carry an set of function pointers. Derived classes simply over ride those methods. Polymorphism can be implemented with pointer to structure. – EvilTeach May 10 '16 at 00:48
  • 1
    @EvilTeach That is a silly argument. Any language can call `member(this, object_functions, arguments).` An OO language comes from language primitives. You can write OO in C as well as ASM and brainfuck. – Captain Giraffe May 10 '16 at 00:53