Is concept of classes in c++ an extension of structures in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is concept of classes in c++ an extension of structures in c

in c structures are grouping of related data while in c++ a class is a grouping of data and methods

14th Oct 2018, 6:48 PM
Asgar Ali
Asgar Ali - avatar
2 Answers
+ 1
I think it isn't, they were made to solve different problems. C++ class is OOP implementation. C structure has the same opportunities as C++ class, but its realization will be much more complicated. Incapsulation: define access identifiers by the same key words, but remember, that struct data is public by default, and class data is private by default, which is already bad idea for OOP. Inheritance. Include "base" struct in your "derived" struct. You also can have some problems in access to "base" struct data, but they can be solved. Polymorphism. If you need to declare function in your struct, you can use pointer to function and get access to it in similar way as for class methods. For different data types support for one function name you can use preprocessor(#define) with _Generic key word. Not all gcc compilers can support it. C11 standart I think. C is a great language. C++ just made this things much easier for it.
14th Oct 2018, 10:39 PM
microBIG
microBIG - avatar
+ 1
working with oop in C is a nightmare and requires some kind of hacky workaround, which is not good and inefficient. C++ is like C on steroids with much more elegant oop and implementation.
15th Oct 2018, 12:58 AM
Kuyondo
Kuyondo - avatar