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++ ?
9 3317 Meetee 931
Recognized Expert Moderator Contributor
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
gpraghuram 1,275
Recognized Expert Top Contributor
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
Meetee 931
Recognized Expert Moderator Contributor
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
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,
oler1s 671
Recognized Expert Contributor
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.
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.
(...)
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 {};
2. The default inheritance is adequate to the default access.
This is part of the default access: struct is public, class is private.
gpraghuram 1,275
Recognized Expert Top Contributor
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.
I have always just used structs as a minimal class, as that is the way
|
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 issues about constructors
that I'm not clear on. I do know that I can take a simple project with a
class that has constuctors, destructors, accessors, and modifiers, change
the classes to structs, and it compiles and runs fine. Can some kind soul...
|
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
structures in C are the same as classes in another language such as C++
or Java only missing the ability to make the data private.
I've never had this explained sufficiently and would appreciate a good
answer. It doesn't need to be Mickey Mouse,...
|
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"?
|
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 conceptually speaking
: when do I define something as 'struct' and when as 'class' ?
for example : if I want to represent a 'Time' thing, containing :
- data members : hours, mins, secs
| |
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 because the length
of an embedded array instance does not match the declared length in the
layout
What does it mean?
|
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,
|
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 experience with System.IO.DirectoryInfo. My
requirements were to recursively scan a folder, recording all
filenames/dates/sizes/attribs. The target folder contained 88,000
files and 5,000 subfolders.
I originally used System.IO.DirectoryInfo and found...
|
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:
org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not
support the requested type of object or operation.
at org.apache.xerces.dom.CoreDocumentImpl.importNode(Unknown
Source)
at org.apache.xerces.dom.CoreDocumentImpl.importNode(Unknown
Source)
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |