473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

what is Difference between structure and class in C++

Hi All,

Can any one have any idea about this issue

what is Difference between structure and class in C++
Other then by default structure have public scope and class have private scope For data member and member function .If there is no other difference between Class & structure then why we use 2 key world in C++ we can use either class or structure in C++ ?
Dec 6 '07 #1
9 3285
Meetee
931 Expert Mod 512MB
Hi All,

Can any one have any idea about this issue

what is Difference between structure and class in C++
Other then by default structure have public scope and class have private scope For data member and member function .If there is no other difference between Class & structure then why we use 2 key world in C++ we can use either class or structure in C++ ?
1) Strutures don't provide something like data hiding which is provided by the classes.

2) Structures contain only data while class bind both data and member functions.

3) Structure dosen't support the polymorphism, inheritance and initilization.

4) In a Structure we can't initilize the value to the variable but in class variable we can assign the values.

And you told is already one of the differences!

Regards
Dec 6 '07 #2
gpraghuram
1,275 Expert 1GB
1) Strutures don't provide something like data hiding which is provided by the classes.

2) Structures contain only data while class bind both data and member functions.

3) Structure dosen't support the polymorphism, inheritance and initilization.

4) In a Structure we can't initilize the value to the variable but in class variable we can assign the values.

And you told is already one of the differences!

Regards

Zodilla ,
I dont agree with what you have told.
Structures can have constructors, destructor, VTable, inheritance, overriding, overloading etc. everything a class can have.

Thanks
Raghuram
Dec 6 '07 #3
Meetee
931 Expert Mod 512MB
Zodilla ,
I dont agree with what you have told.
Structures can have constructors, destructor, VTable, inheritance, overriding, overloading etc. everything a class can have.

Thanks
Raghuram
Raghuram,

I don't have ever encountered with such a structure!! Can u give an example to support this? It will clearify my doubt regarding this.

Okie.. I found examples of constructor and overloading! But couldn't found inheritance example!

Thanks
Dec 6 '07 #4
scruggsy
147 100+
Raghuram,

I don't have ever encountered with such a structure!! Can u give an example to support this? It will clearify my doubt regarding this.

Okie.. I found examples of constructor and overloading! But couldn't found inheritance example!

Thanks
Try here.
Decent explanation of the only difference between structs and classes and the reason for having both keywords,
Dec 6 '07 #5
oler1s
671 Expert 512MB
Yup, no difference but the default access type. Yes, it is idiomatic to use structs for arbitrary data types, and classes for the whole, well, class thing. But C++ actually doesn't really differentiate between the two. Crazy, but true.

EDIT: You can express your disbelief as much as you want. If you find it so absurd, write some code and check it for yourself. C++ structs are not the same as C structs. Just because you use them idiomatically as C structs does not mean a thing.
Dec 6 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
Hey, just check it out for yourself.

Write a class with constructors, destructors and member funcitons. Once it's compiled an working go in there and change the class to struct and rec-compile. You will find that everything works as before.

Why is this??

Here's why:

1) The only way to group data variables together in C is to use a struct.
2) C++ is a replacement for C
3) The only way to group data variables together in C++ is to use a struct.
4) Object-oriented features (which you don't have to use) were added to C++
5) That involved a "class" with "public" and "private" members. This was due to the object-technology folks who use a "class of things" and an "instance of a class".
6) The class in C++ was implemented as a struct.
7) To make C++ look like an object-oriented lamguage, the "class" keyword was added as an alternate declaration of a struct.
7) The only difference is that the default access to a class is private whereas the default access for a struct is public.
Dec 6 '07 #7
jac666
1
(...)
7) The only difference is that the default access to a class is private whereas the default access for a struct is public.
C++ "class" = "struct" except:
1. The default access to the members of a class is private whereas the default access for a struct is public
2. The default inheritance is adequate to the default access.

struct A : B {}; // the same as: class A : public B {};
class A : B {}; // the same as: class A : private B {};
Dec 6 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
2. The default inheritance is adequate to the default access.
This is part of the default access: struct is public, class is private.
Dec 6 '07 #9
gpraghuram
1,275 Expert 1GB
Hey, just check it out for yourself.

Write a class with constructors, destructors and member funcitons. Once it's compiled an working go in there and change the class to struct and rec-compile. You will find that everything works as before.

Why is this??

Here's why:

1) The only way to group data variables together in C is to use a struct.
2) C++ is a replacement for C
3) The only way to group data variables together in C++ is to use a struct.
4) Object-oriented features (which you don't have to use) were added to C++
5) That involved a "class" with "public" and "private" members. This was due to the object-technology folks who use a "class of things" and an "instance of a class".
6) The class in C++ was implemented as a struct.
7) To make C++ look like an object-oriented lamguage, the "class" keyword was added as an alternate declaration of a struct.
7) The only difference is that the default access to a class is private whereas the default access for a struct is public.

Thanks for your explanation..
Raghuram
Dec 7 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
22
by: Ook | last post by:
We have had a discussion on the differences between a class and a structure, and no one is in agreement. As I understand it, a structure defaults to public, a class defaults to private. There are...
6
by: nick | last post by:
I have tried finding an answer to this, but most people just explain classes as a more modular way to program. It seems to me that (forgetting OO programming which I don't quite understand) the...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
2
by: Tom | last post by:
I'm getting this error when I try to pass a structure to a dll. An unhandled exception of type 'System.ArgumentException' occured in Test1.exe Additional Information: Type could not be marshaled...
17
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
14
by: teslar91 | last post by:
As a fairly new .NET coder, I would greatly appreciate some comments on any .NET classes that are known to be notoriously slow by comparison to direct API calls. I've already had a bad...
10
by: Simon Brooke | last post by:
The DOM API has included public Node importNode(Node,boolean) as a method of the Document interface for a long time. Does anything actually implement it? Xerces 2 is giving me: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.