why we use class in C++ ??

As we know the only difference between class an struct is that default declaration of members and default inheritance access specifiers in struct is public and in class it's private.

Correct me if i am wrong. !!

So when we write any program, why we create class why not struct? I am not saying struct is not used, but class is used majority of times and my question is why is it so?
i also don't know.. maybe it's just a programming convention.. most of the time struct is use when you want to store information that is related to something..

classes is use when you want to represent a real world object like a car or a robot.. well that's what i think..

correct me if im wrong guys..
1) Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public.

Class: Class is a successor of Structure. By default all the members inside the class are private.

2) structures in c++ doesn't provide data hiding where as a class provides data hiding

(3)classes support polymorphism(late binding), whereas structures don't
(4) class and structure are very similar. the former is heavyweight while the latter is light weight. reference to the former rests on the heap..while the latter in whole (instance and data) rests on the stack. therefor care should be taken not to make a struct very heavy else it overloads the stack causing memory hogging. class needs to have an instance explicitly created to be used. A struct doesn't have to be explicitly initiated.

(5) The "this" pointer will works only with class.

(6)Structures members can not be declared as protected , but class members can be.

(7)You can not do inheritance in structures.

(8)Structures do not require constructors while classes require
Last edited on
@somshekhar
hey thanks.. that's awesome info.. cheers ;)
@Somshekhar

I am completely agree with (1).

2) Structure do support encapsulation in C++.
3) Structure do support polymorphism in C++.
5) "this" pointer also works with struct in C++.
6) it means Data Hiding.. that comes under Encapsulation and struct do support that in C++
7) we do have support of inheritance in C++ with struct.
8) Even with Classes its not compulsory to provide constructors, just like structure.

I am not sure about (4) so will not comment on that...
8) a class always has at least one default constructor... even if u dont implemt it yourself.

6) how to hide data within a structure like in an class?...
8) A class/struct provides two constructors unless you declare otherwise
aw forgot the copy constructor^^...
blah blah blah....

OP had it right initially.

Venturing a guess as to why OP sees what s/he sees: years ago structures in C++ did not support methods
or a this pointer or any of that stuff. They were identical in usage to C structs. Everyone learned at that time
to use classes in C++ for OOP.

The C++ standard was eventually changed so that structs and classes are now identical except for default
access and inheritance (structs have an implied "public:" whereas classes an implied "private:" if left
unspecified).
Topic archived. No new replies allowed.