Click to See Complete Forum and Search --> : difference between a c++ structure and class
rajalekshmy+r
June 14th, 2002, 06:24 AM
what is the difference between a C++ structure and a class?
What comes to my mind are
1) structures in c++ doesnot provide datahiding where as a class provides datahiding
2)classes support polymorphism, whereas structures donot
Is there anything other than this?
_illusioned
June 14th, 2002, 09:17 AM
How about constructors, destructors, inheritance, and a "this" pointer inside methods.
Zeeshan
June 14th, 2002, 12:58 PM
By default everything in structure is public and everything in class is private, but you can change it.
And one more thing, by default inheritance of structure is public and inheritance of class is private.
Hope it helps.
Paul McKenzie
June 14th, 2002, 01:42 PM
Originally posted by rajalekshmy+r
what is the difference between a C++ structure and a class?
What comes to my mind are
1) structures in c++ doesnot provide datahiding where as a class provides datahiding
2)classes support polymorphism, whereas structures donot
Is there anything other than this? No, you are wrong on both 1 and 2.
There is no difference between a class and a struct except that by default, the members of a struct are public, and the members of a class are private. Also structs are by default inherited publicly and classes by default are inherited privately. Other than that, whatever you can do in a class, you can do in a struct.
Hopefully you weren't asked this question on a job interview.
Regards,
Paul McKenzie
jparsons
June 14th, 2002, 02:32 PM
Originally posted by _illusioned
How about constructors, destructors, inheritance, and a "this" pointer inside methods.
Technically speaking though you can instantiate a structure with the new operator in the simulation of a constructor.
rajalekshmy+r
June 17th, 2002, 01:21 AM
1)can we provide datahiding with structures? if yes, how?
2)can structures suport run time polymorphism? If yes, how?
Paul McKenzie
June 17th, 2002, 04:06 AM
Originally posted by rajalekshmy+r
1)can we provide datahiding with structures? if yes, how?
2)can structures suport run time polymorphism? If yes, how? By writing code? Again, whatever you can do in a class, you can do in a struct. Why not write code and see for yourself?
#include <iostream>
using namespace std;
struct Whatever
{
virtual void foo() { }
virtual ~Whatever() { }
};
struct Derived : Whatever
{
void foo() { cout << "value=" << value; }
Derived(int n) : Whatever(), value(n) { }
private:
int value; // private data member
};
int main()
{
Whatever *pW = new Derived(10);
pW->foo(); // virtual call
delete pW;
return 0;
}
Convinced now?
Regards,
Paul McKenzie
rajalekshmy+r
June 18th, 2002, 06:23 AM
i am fully convinced of your explanation, but if a structure can do whatever a class do why should we have concept of class?
Zeeshan
June 18th, 2002, 06:45 AM
Because Bjarne Stroustrup want to make its memory structure compatable with C. So if you dont use virtual function and virtual inheritance then you C++ class and C structure follow the same memory model.
For further informatoin take a look at
Design and Evaluation of C++
by
Bjarne Stroustrup.
rajalekshmy+r
June 18th, 2002, 07:10 AM
Do you mean C++ structure and C++ class donot follow the same memory model. (If they are following the same memory model the datastructure class would not have been introduced by stroustroup, that is what I am interpreting, is it right?)
Zeeshan
June 18th, 2002, 07:26 AM
Let me explain it in more detail. The C structure, C++ structure and C++ class shares the same memory model until you havent introduce virtual function or inherit virtual C++ class or structure.
But if you introduce virtual function or virtual inheritance then it is no more compatable with C structure. But the C++ structure and C++ class remain same after introducing these.
For more information take a look at
Inside C++ Object Model
by
Stan B Lippman
I have written one artical which discuss the memory model of C++ class and behaviour of virtual function, virtual pointer and virtual table in Visual C++ (the behaviour of virtual table is not standard, so different compiler may use different technique)
http://www.codeguru.com/atl/ATL_UnderTheHood_1.html
It might be helpful to you.
I again suggest to read "Design and Evaluation of C++" by Bjarne Stroustrup to understand the reason of each functionality of C++.
Hope it helps.
rajalekshmy+r
June 18th, 2002, 07:31 AM
yes it is of real help,let me go throu stroustroup
MikeB
June 18th, 2002, 02:46 PM
Bjarne Stroustrup :
"By definition, a struct is a class in which members are by default public; that is,
struct s{ ....
is simply shorthand for
class s{public......
"
This tells me that there is no difference between struct and class, except as mentioned previously, by default everything in a struct is public and everything in a class is by default private.
Mike B
ramacet
July 23rd, 2008, 11:54 AM
Accoring to Zeeshan "C++ structure and C++ class remain same after introducing virtual function or virtual inheritance ." then why we do introduce a keyword class.
I mean if both are doing the same then whu unneceesary ly we have introduce a keyword class. There might be some reason behind the same
angelorohit
July 23rd, 2008, 05:44 PM
structs by default inherit publicly and classes by default inherit privately.
struct MyStruct {};
class MyClass {};
struct MyDerivedStruct : MyClass //Public inheritance.
{}
class MyDerivedClass : MyStruct //Private inheritance
{}
Accoring to Zeeshan "C++ structure and C++ class remain same after introducing virtual function or virtual inheritance ." then why we do introduce a keyword class.
I mean if both are doing the same then whu unneceesary ly we have introduce a keyword class. There might be some reason behind the sameOne word - legacy reasons. Initially C had structs which could hold only data members and did not have access specifiers and all that good stuff. Then, when C++ was being designed, the creators wanted much more from a simple struct. They couldn't very well just break existing code by changing structs both syntactically or semantically. Thus, the class was born. Later on, more power was given to structures while maintaining them almost compatible with legacy code.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.