+ 2

What is the difference between a class in C++ and a structure in C ? It looks pretty similar for me.

2nd Aug 2018, 5:38 AM
Georges FRIRE
Georges FRIRE - avatar
15 Antworten
+ 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
2nd Aug 2018, 7:15 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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/
2nd Aug 2018, 7:16 AM
Meet Mehta
Meet Mehta - avatar
+ 3
MEET MEHTA Your claim that you cannot have functions inside structs appear to be untrue.
2nd Aug 2018, 7:11 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
2nd Aug 2018, 7:20 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
2nd Aug 2018, 7:26 AM
Georges FRIRE
Georges FRIRE - avatar
+ 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.
2nd Aug 2018, 7:33 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Yes but you cant use methods (i.e function inside classes) in struct in C language.
2nd Aug 2018, 7:07 AM
Meet Mehta
Meet Mehta - avatar
+ 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.
2nd Aug 2018, 7:09 AM
Meet Mehta
Meet Mehta - avatar
+ 1
Hatsy Rei In C struct we cant use function Right?
2nd Aug 2018, 7:12 AM
Meet Mehta
Meet Mehta - avatar
+ 1
Hatsy Rei He was having query for C struct and we cant have function in them.
2nd Aug 2018, 7:18 AM
Meet Mehta
Meet Mehta - avatar
+ 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.
2nd Aug 2018, 7:19 AM
Georges FRIRE
Georges FRIRE - avatar
+ 1
MEET MEHTA, Hatsy Rei thank you for your answer. it's much clearer for me now. stay amazing! :-)
2nd Aug 2018, 7:41 AM
Georges FRIRE
Georges FRIRE - avatar
0
but if I do something like this struct book { int numberofpage; char title[50]; // etc }; does it look logical ?
2nd Aug 2018, 7:06 AM
Georges FRIRE
Georges FRIRE - avatar
0
functions is notnised
3rd Aug 2018, 4:47 PM
OGUNLANA MATTHEW OLUWADAMILOLA
OGUNLANA MATTHEW OLUWADAMILOLA - avatar
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.
15th Aug 2020, 4:35 PM
Maaz Hassan
Maaz Hassan - avatar