+ 2
What is the difference between a class in C++ and a structure in C ? It looks pretty similar for me.
15 ответов
+ 6
MEET MEHTA Yes. C++ structs on the other hand are just classes without access specifiers. There are a few more differences on structs between C and C++.
https://www.quora.com/What-is-the-difference-between-C-structure-C++-structure
+ 4
That class example was of js language.
I am providing you C++ class example to better understand that we can use functions in classes.
https://www.sololearn.com/learn/CPlusPlus/1711/
+ 3
MEET MEHTA Your claim that you cannot have functions inside structs appear to be untrue.
+ 3
Fair enough. The conversation here was going on as if structs were a construct specific to C, so hopefully we have a bit more context in here.
+ 3
Is the following correct ? just to be sure I really got it.
- in C you can just declare variable in a structure.
- in C++ you can have variable and function in a structure.
- in C++ you can have variables, function, and access specifier (public, private) in a class.
+ 2
Georges FRIRE
Sounds about right. Due to the lack of access specifiers in structs, their members are always public by default. I think Meet covered that for you.
+ 1
Yes but you cant use methods (i.e function inside classes) in struct in C language.
+ 1
In struct you cant use functions.
You will learn difference between them as you start buildings project and start using classes in your program.
+ 1
Hatsy Rei
In C struct we cant use function
Right?
+ 1
Hatsy Rei
He was having query for C struct and we cant have function in them.
+ 1
thank you for your answer. So the struct in C is not the same as a struct in C++ ? I haven't got that before neither.
+ 1
MEET MEHTA, Hatsy Rei
thank you for your answer. it's much clearer for me now.
stay amazing! :-)
0
but if I do something like this
struct book {
int numberofpage;
char title[50];
// etc
};
does it look logical ?
0
functions is notnised
0
There are some main difference between struct in c and c++:
In c you can only create data members in the struct while in c++ you can create functions as well.
In both c and c++ the all data members and functions are private by default.
While in classes in c++ it's your choice to make data members either private, public or protected but by default, all data members and functions are private in classes.
Hope it will help you.