You are here:




hello sir

i would like to know why there is a need for classes? because as i have tried we can declare public as well private member variables and member functions in a structure .

what i got that is sturcture members are public or classes members are private by default . we can solve ore purpose by making private part in structures then why we use classes in c++?

thank you

You are correct in a way. In fact it is probably better to loose struct than class as class is a better description of what you usually end up defining in C++.

However C++ has always maintained strong compatibility with C and struct types were inherited from C. In C they can only define data and C has no concept of access controls (i.e. public, private, protected) or inheritance and the like. Such compound data types (or data structures) are called Plain Old Data or POD types in the C++ standard.

Classes were added to C++ with all the fun features we know and love above and beyond grouping collections of data together into a structure.

Both the keywords class and struct are used to define class types in C++. The default access rules for classes that are defined using struct were chosen to ensure backward compatibility with C. In general it is better to use class than struct when defining a class with class like features: member functions, base classes, private and public parts, constructors, destructors etc. as class is a better description of what you are defining. The keyword struct is best reserved for plain old data (POD) types - especially if C compatibility is required.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.