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.
gkpuneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KangaRooCommented:
With structures, inheritance is by default public, with classes it is by default private.

The struct keyword is in the language because its a C feature. C++ originated from C and, at least in its early years, maintained a high degree of compatebilty with C.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhymesCommented:
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
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

dhymesCommented:
Kangaroo, I think I jumped on this question ahead of you. What's up??
0
dhymesCommented:
Ok, maybe i did not understand the question.
0
KangaRooCommented:
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?
0
dhymesCommented:
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
0
KangaRooCommented:
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 {}
};
0
dhymesCommented:
Yep, no doubt about it.
0
gkpuneAuthor Commented:
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.
0
nietodCommented:
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
0
nietodCommented:
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.  
0
gkpuneAuthor Commented:
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.
0
KangaRooCommented:
gkpune, it is thus exactly as I said...
0
nietodCommented:
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.
0
KangaRooCommented:
Thank you nietod. At last some support on this <censored> monday.
0
nietodCommented:
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.
0
KangaRooCommented:
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 :(
0
nietodCommented:
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?
0
KangaRooCommented:
Non-cookie simply doesn't work, at least not the way I'm used to :(
0
nietodCommented:
Can you be more specific, of have you notified customer service of the problems?
0
KangaRooCommented:
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.
0
KangaRooCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.