0

While reading a book, I came across these lines :

There is very little syntactical difference between structures and classes in C++ and, therefore, they can be used interchangeably with minor modifications. The only difference between structure and a class is that, by default, the members of a class are private,while, by default, the members of structure are public.

However, my teacher told me that we cannot have functions inside structures. The book I've read is quite popular and I think that what written on it is correct. Is it possible to have functions inside structures ? If yes, kindly leave an example or reference link.

H G Sur
  • 121
  • 1
  • 1
  • 10
  • 1
    Your teacher is wrong. However, the book is also not quite right; in addition to default access being public for `struct`s, default inheritance is also public. – Pete Becker Jan 02 '16 at 13:03
  • @PeteBecker Thank you! – H G Sur Jan 02 '16 at 13:07
  • 1
    Well, you have two options: change the teacher or buy another book (about C this time, but keep the other)... ;). Jokes apart, maybe your teacher was explaining the differences betwen C and C++ (good idea) or his course is intended to start with a brief tour about the C language and after then he will introduce C++ (not really a good idea IMHO). – Bob__ Jan 02 '16 at 13:45
  • @Bob__ I asked my teacher to clarify my doubt and showed and example program. She said, "You'll get the output but you're not supposed to do that. Never do that again. " Also, she gave me three differences one of which is "Classes can contain both functions and variables but structures can only contain variables." . It is only C++ and C is never introduced to us. – H G Sur Jan 04 '16 at 15:12
  • 1
    Your teacher needs a teacher. As all the answers pointed out, in C++ structs can have member functions as those are nothing less then classes with default public visibility for members. Maybe it's a matter of style convention for her, but the rules are clear. – Bob__ Jan 04 '16 at 15:32

3 Answers3

5

A C struct cannot have member functions. (It can have function pointers, which however are not the same thing.)

A C++ struct is equivalent to a class in every way but the default visibility of its members, as stated by your book (public for struct, private for class), and its default inheritance.


class MyClass : public BaseClass
{
     public:
         MyClass();
         virtual ~MyClass();

         void someFunction();

     private:
         int member_;
};

In the above example, which does state visibility (public, private) explicitly instead of relying on defaults (a practice I endorse, for clarity), the keyword class could be swapped for struct without any change of meaning or result.

There is some understanding that struct is preferred for plain data collections, while class is preferred for fully-fledged classes with non-trivial functionality, but that is as far as it goes.


Perhaps your teacher was talking about C structs.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Thank you, Sir. We don't have C at school only C++. Can you give me a example program so that I can show it to my teacher ? – H G Sur Jan 02 '16 at 13:07
  • 1
    @HGSur: Any piece of C++ with member functions in it, really. If you always give visibility explicitly instead of relying on the default (as you should, for clarity), `struct` and `class` are just interchangeable keywords. – DevSolar Jan 02 '16 at 14:31
  • so struct something{private : } is equivalent to class ? – H G Sur Jan 04 '16 at 15:16
  • Thanks again. Now I understand struct can do anything a class can do with some keywords :) – H G Sur Jan 04 '16 at 15:24
2

An example with method in struct:

#include <iostream>

struct HelloWorld {
    void operator()() const
    {
        std::cout << "hello world\n";
    }
};

int main() 
{
    HelloWorld{}();
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

structs in C++ are basically classes. Hence, they can have member functions. Read this for more information on the difference between both.

However, my teacher told me that we cannot have functions inside structures.

That's correct when speaking about C. C only allows data to be stored in a struct. Best thing you can do is store a function pointer in a struct.

Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67