Link to home
Start Free TrialLog in
Avatar of gkpune
gkpune

asked on

Difference

Hello friends,
             I have a very simple question.
The only difference I know between Class & Structure is that Class has by default private data members while structure has public.
Can somebody tell me the other differences if any.
If there are not then why do we have two different data types with only one difference.
Avatar of dhymes
dhymes

ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There are so many differences that it would be difficult for me to list them all. I guess the only commonality between the two is that they both encapsulate a data type.

A class can do more then just encapsulate a data type, it encapsulates a data type along with all of the operations that can be performed on that data type (Abstract Data Type). But, a class is more than just an Abstract Data type because it can also inherite the attributes of it's parent class (Inheritance), restrict and hide the details of it's implementation from the users (data hidding). It also has the ability to do (Polymorphism) using virtual function. Virtual function are how we implement Polymorphism when using the C++ language.

Use the link I provided below and work through the tutorial, it clearly shows you the differences between Structurs, Abstract Data types and Classes.

http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html
Kangaroo, I think I jumped on this question ahead of you. What's up??
Ok, maybe i did not understand the question.
Hi dhymes, I hadn't seen your comment. You've probably posted it after I opened the page.
You agree then that the only difference between class and struct is in the default access specifiers?
Hello Kangaroo,

I do agree, I just think that there are, as i explained above, many more differences in the two types that should also be mentioned.

Regards,
Dave
You can do all those things with struct as well:

class A
{
  private:
     int i;

  public:
     A(int ii) i(ii) {}
    virtual ~A(){}

    virtual void f() const = 0;
};

class B : public A
{
   public:
     B(int ii) : A(ii) {}
     ~virtual ~B(){}

     virtual void f() const {}
};

is identical to
struct A
{
  private:
     int i;

  public:
     A(int ii) i(ii) {}
    virtual ~A(){}

    virtual void f() const = 0;
};

struct B : public A
{
   public:
     B(int ii) : A(ii) {}
     ~virtual ~B(){}

     virtual void f() const {}
};
Yep, no doubt about it.
Avatar of gkpune

ASKER

I accept KangaRoo's proposed answer that we can do all those things with struct as well, but my question remains as it is.
I don't know the other differences. Please list them.
There are no other differences.  struct and class are 100% the same except that struct defaults to public enheritance and public membebers and class to private.   There is nothing else to list.

>> why do we have two different data types
>> with only one difference
That is explained well in Bjarne Stroustrup's "The Design and Evolution of C++" (page 76). He basically wanted to make things simple for his front compiler and also for the programmers using the language. The C struct had many of the  fundamental features he needed for classes, so he just extended it to support the other features he needed.  However, propper OO design tends to encourage private data members, so the "class" keyword  was added to create a struct-like item that defaulted to private, which would be easier for the programmer to write good OO classes in, since it woudl default to private.  That is all
from stroustrup

My intent was to have a single concept: a single set of layout rules, a single set of lookup rules, a single set of resolution rules etc.  Maybe we could have lived with two set[s] of rules, but a sigle concept provides a smoother integration of features and simpler implimentations.  I was convinced that if _struct_ came to mean "C and compatibility to users" and _class_ to mean "C++ and advanced features," the community would fall into two distinct camps that would soon stop communicating.  Being able to use as many or as few language features as needed when designing a class was an important idea to me.  Only a single concept would support my ideas of a smooth and gradual transition from "traditional C-style programming," through data abstraction, to object-oriented programming.  Only a single concept would support the notion of "you only pay for what you use" ideal.

I recommend the book highly for those wanting to understand how the language works and why it works the way it does.  
Avatar of gkpune

ASKER

I accept nietod's proposed answer and 50 points should be given to him.
I just want to ensure whether there is another difference.

Thanks very much.
GKPune.
gkpune, it is thus exactly as I said...
First of all, gkpune, the proposed answer is from Kangaroo, so if you grade it now the points woudl go to him, not me.  (IF you wanted the points to go to me, you would have to reject his answer first so that I could answer.)
However, Kangaroo's answer is not wrong.  I didn't propose anythig different than what he said, i just produced a little "proof" of it, so while the choice of who gets the points is up to you, it really seems (to me) like Kangaroo deserves them.
Thank you nietod. At last some support on this <censored> monday.
Has this been a bad one?  Not for me.  I wonder if there is some relation.  One expert has a good day--lots of unexpected points--and another a bad day--lots of lost points.
At least your day is good then. Well, I've had a few very good weeks recently. Part of the trouble was with the new site, they forgot the non-cookie people :(
Is this any better?  There were numerious problems with non-cookie people in the beta.  (I discivered many and I do accept cookies).  I really didn't think they would get fixed in time.  Are they?
Non-cookie simply doesn't work, at least not the way I'm used to :(
Can you be more specific, of have you notified customer service of the problems?
I have notified them.

The usual way in which I visit EE is from the notifictaion messages (he second options is the non-cookie link). The first time in each browser seesion login name and password are requested.
Now however no login request is done and one simply gets to the selected Q without being logged in. A non-cookie login attempt fails to bring you back to the question as a registered user.
I've also tried to login through
www.experts-exchange/secure which brings me to the home alright but any Q thread I review is as if I didn't login. The reload script doesn't work either.