Current location: Hot Scripts Forums » Programming Languages » C/C++ » Difference between c and c++ structure


Difference between c and c++ structure

Reply
  #1 (permalink)  
Old 12-12-06, 02:11 AM
abhishek4solution abhishek4solution is offline
New Member
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Difference between c and c++ structure

Hi all,
I have a confusion, can anybody clear it?
i want to know actually is it possible to inherit structure in c++ or can we perform polymorphic operations on structure in c++.

Waiting for ur replies
Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-12-06, 06:08 AM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
polymorphism can be accomplished through the 'virtual' keyword. The virtual keyword allows you to implement a mechanism in different ways.

And yes, you can inherit in C++. You can actually perform multiple inheritance, but it's seldom needed.
__________________
my site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-13-06, 05:22 AM
abhishek4solution abhishek4solution is offline
New Member
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi all, can anyone clear my confusion why we do not use structure in c++ in place of class? Structure also provide us inheritance and polymorphism and can also be used in place of class then why we prefer class in place of structure.
There must be some benefit using it.What are those?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-13-06, 06:28 AM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
The default accessibility for a class is private while a structure is public. Classes can define behavior and attributes while structures only define attributes. And classes are passed through reference while structures are passed through value.

Structures can still be used in C++.
__________________
my site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 12-13-06, 07:01 AM
abhishek4solution abhishek4solution is offline
New Member
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, I accept it but structures can also be used to define behaviour in c++. I accept this thing that classes can be refrenced but structure do not.
Is there any other important difference between these?


Waithing for your precious reply.

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 01-09-07, 06:03 PM
grafman grafman is offline
Coding Addict
 
Join Date: Dec 2006
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
A structure is just data. It can hold a pointer to a function but you don't define behavior in them.

A class defines data and King Coder said and in it you define "functions" or methods that encapsulate the behavior of the resulting objects.

I suppose that you could define a data only class, but I don't know how practical that would be unless it was embedded in another class. But then you'd probably use a structure.

The way to really tell the difference is that you can define a class to do something to something else. A structure can only have things done to it.
__________________
"Things are difficult only while you don't understand them."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 01-14-07, 06:15 PM
balkman balkman is offline
New Member
 
Join Date: Jan 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by grafman
A structure is just data. It can hold a pointer to a function but you don't define behavior in them.

A class defines data and King Coder said and in it you define "functions" or methods that encapsulate the behavior of the resulting objects.

I suppose that you could define a data only class, but I don't know how practical that would be unless it was embedded in another class. But then you'd probably use a structure.

The way to really tell the difference is that you can define a class to do something to something else. A structure can only have things done to it.
not true... a structure and class function exactly the same except for the default access. structure being public by default, and class being private by default.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 01-15-07, 09:31 AM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by balkman
not true... a structure and class function exactly the same
exactly the same? That's not true either.
__________________
my site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 01-20-07, 08:50 AM
balkman balkman is offline
New Member
 
Join Date: Jan 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Tell how they are different other than their default access type. Anything you can do with a class you can do with a structure.

Edit:
Unless you're talking about how a compiler might build them each in assembly. Otherwise, on a higher level of C++ they're the same.

Last edited by balkman; 01-20-07 at 08:55 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 01-20-07, 09:10 AM
balkman balkman is offline
New Member
 
Join Date: Jan 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by King Coder
The default accessibility for a class is private while a structure is public. Classes can define behavior and attributes while structures only define attributes. And classes are passed through reference while structures are passed through value.

Structures can still be used in C++.

You mentioned earlier that classes are passed by reference, while structures are passed by value. You might be thinking of C# or Java (I don't think Java has structs though) because everything in C++ is passed by value into a function unless otherwise noted with an ampersand (making it pass by reference).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 07:23 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.