Difference btw struct and class?

I’ve learned c language before and class here feels the same as struct so are the same and can we use struct in c++ instead of class

@tharunnayak14, the structure in c++ is quite different from the struct in c ,

  • Structures in C cannot have member functions inside structure but Structures in C++ can have member functions along with data members.
  • We cannot directly initialize structure data members in C but we can do it in C++.
  • In C, we need to use struct to declare a struct variable. In C++, struct is not necessary. For example, let there be a structure for Record. In C, we must use “struct Record” for Record variables. In C++, we need not use struct and using ‘Record‘ only would work.

there are some more difference , but in c++, the difference between struct and class is very minor , struct by default has default declaration of its members as public and in class its private by default that’s it . so if you know struct of c then it won’t be of much use so i will suggest you to spend some time in understanding the concepts of class .

In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer

so now u say i should learn class and struct in c++ and we can say struct and class are very similar in c++

yes exactly !! …

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.