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 3285
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
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
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,
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.
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| |