?
Solved

Difference

Posted on 2000-01-24
23
Medium Priority
?
179 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:gkpune
  • 9
  • 6
  • 6
  • +1
23 Comments
 
LVL 7

Accepted Solution

by:
KangaRoo earned 150 total points
ID: 2380854
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
 
LVL 2

Expert Comment

by:dhymes
ID: 2380886
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 2

Expert Comment

by:dhymes
ID: 2380892
Kangaroo, I think I jumped on this question ahead of you. What's up??
0
 
LVL 2

Expert Comment

by:dhymes
ID: 2380900
Ok, maybe i did not understand the question.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2380912
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
 
LVL 2

Expert Comment

by:dhymes
ID: 2380990
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2381023
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
 
LVL 2

Expert Comment

by:dhymes
ID: 2381053
Yep, no doubt about it.
0
 

Author Comment

by:gkpune
ID: 2381198
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
 
LVL 22

Expert Comment

by:nietod
ID: 2381236
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
 
LVL 22

Expert Comment

by:nietod
ID: 2381271
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
 

Author Comment

by:gkpune
ID: 2381334
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2381353
gkpune, it is thus exactly as I said...
0
 
LVL 22

Expert Comment

by:nietod
ID: 2381390
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2381574
Thank you nietod. At last some support on this <censored> monday.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2381635
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2381700
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
 
LVL 22

Expert Comment

by:nietod
ID: 2381800
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2381851
Non-cookie simply doesn't work, at least not the way I'm used to :(
0
 
LVL 22

Expert Comment

by:nietod
ID: 2382785
Can you be more specific, of have you notified customer service of the problems?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2384343
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2384355
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question