Thread: [Dev-C++] What are the differences between a struct and a class?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Andre M. B. <and...@su...> - 2008-12-05 13:05:42
|
Dear users, When we need to develop a OO app, one way is to create classes, with attributes and methods. After this we instanciate the class in our program, as it is, or with some specialization. Thinking only about ANSI-C, we can create structs with the usual fields (attributes), but we can associate functions inside the struct too. After this operation, putting this struct definition in a .h file, we can make a variable declaration defining it as a struct type (the one created in the .h file). In this scenario, disconsidering the protected/public/private features, please talk to me abou what a OO app can do that a struct like .c program can´t. Regards Andre |
From: Derek C. <de...@ci...> - 2008-12-05 16:30:37
|
In C++ the only difference between a struct and a class is that in a class the default access is private whereas in a struct it is public. in all other aspects they are the same. On Fri, Dec 5, 2008 at 12:43 PM, Andre Macario Barros <and...@su...> wrote: > Dear users, > > When we need to develop a OO app, one way is to create > classes, with attributes and methods. After this we instanciate > the class in our program, as it is, or with some specialization. > > Thinking only about ANSI-C, we can create structs with the > usual fields (attributes), but we can associate functions inside > the struct too. After this operation, putting this struct definition > in a .h file, we can make a variable declaration defining it as > a struct type (the one created in the .h file). > > In this scenario, disconsidering the protected/public/private > features, please talk to me abou what a OO app can > do that a struct like .c program can´t. > > Regards > Andre > > ------------------------------------------------------------------------------ > SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada. > The future of the web can't happen without you. Join us at MIX09 to help > pave the way to the Next Web now. Learn more and register at > http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > |
From: a.geo <aqu...@gm...> - 2008-12-05 17:07:39
|
This right, the difference is on the logic,... A struct is a organized data collection... a class is a definition of a live-object, with attributes and activities (methods). 2008/12/5 Derek Clarke <de...@ci...> > In C++ the only difference between a struct and a class is that in a > class the default access is private whereas in a struct it is public. > in all other aspects they are the same. > -- -------------------------------------------------------- VISIT MY BLOG : http://ageo.blogspot.com/ picture blog : http://ageo.deviantart.com/journal/ |
From: Chris M. <lor...@gm...> - 2008-12-06 06:35:23
|
On Fri, Dec 5, 2008 at 8:30 AM, Derek Clarke <de...@ci...> wrote: > In C++ the only difference between a struct and a class is that in a > class the default access is private whereas in a struct it is public. > in all other aspects they are the same. structs are unable to have inheritance if I remember correctly. -- Registered Linux Addict #431495 http://profile.xfire.com/mrstalinman | John 3:16! http://www.fsdev.net/ | http://lordsauron.wordpress.com/ |
From: Derek C. <de...@ci...> - 2008-12-06 11:05:54
|
On Sat, Dec 6, 2008 at 6:35 AM, Chris Miller <lor...@gm...> wrote: > On Fri, Dec 5, 2008 at 8:30 AM, Derek Clarke <de...@ci...> wrote: >> In C++ the only difference between a struct and a class is that in a >> class the default access is private whereas in a struct it is public. >> in all other aspects they are the same. > > structs are unable to have inheritance if I remember correctly. > On Sat, Dec 6, 2008 at 6:35 AM, Chris Miller <lor...@gm...> wrote: > On Fri, Dec 5, 2008 at 8:30 AM, Derek Clarke <de...@ci...> wrote: >> In C++ the only difference between a struct and a class is that in a >> class the default access is private whereas in a struct it is public. >> in all other aspects they are the same. > > structs are unable to have inheritance if I remember correctly. Not true - as I said the *only* difference is the default access. See below, these were tested on VS2008 Express. The classes use accessors because the default access is private, unlike the structs. Foo.h #ifndef FOO_H #define FOO_H struct Foo { int value; Foo() : value(0) {}; Foo(const int new_value) : value(new_value) {}; friend ostream& operator<< (ostream& my_stream, const Foo& my_foo); }; inline ostream& operator<< (ostream& my_stream, const Foo& my_foo) { my_stream << my_foo.value; return my_stream; }; class Foo_class { int value; public: Foo_class() : value(0) {}; Foo_class (const int new_value) : value(new_value) {}; const int Value() const { return value;}; friend ostream& operator<< (ostream& my_stream, const Foo_class& my_foo); }; inline ostream& operator<< (ostream& my_stream, const Foo_class& my_foo) { my_stream << my_foo.Value(); return my_stream; }; #endif /* FOO_H */ Bar.h #ifndef BAR_H #define BAR_H #include "Foo.h" struct Bar : public Foo { int value2; Bar() : value2(0) {}; Bar(const int new_value) : Foo(0), value2(new_value) {}; Bar(const int new_value1, const int new_value2) : Foo(new_value1), value2(new_value2) {}; friend ostream& operator<< (ostream& my_stream, const Bar& my_bar); }; inline ostream& operator<< (ostream& my_stream, const Bar& my_bar) { my_stream << my_bar.value << " " << my_bar.value2; return my_stream; }; class Bar_class : public Foo_class { int value2; public: Bar_class() : value2(0) {}; Bar_class(const int new_value) : Foo_class(0), value2(new_value) {}; Bar_class(const int new_value1, int new_value2) : Foo_class(new_value1), value2(new_value2) {}; const int Value2() const { return value2; }; friend ostream& operator<< (ostream& my_stream, const Bar_class& my_bar); }; inline ostream& operator<< (ostream& my_stream, const Bar_class& my_bar) { my_stream << my_bar.Value() << " " << my_bar.Value2(); return my_stream; }; #endif /* BAR_H */ Foobar.cpp // Foobar.cpp : main project file. #include "stdafx.h" #include "Bar.h" using namespace System; int main(array<System::String ^> ^args) { Foo foo1(37); Foo_class foo2(43); Bar bar1(100, 142); Bar_class bar2(454, 922); cout << "foo1= " << foo1 << endl; cout << "foo2= " << foo2 << endl; cout << "bar1= " << bar1 << endl; cout << "bar2= " << bar2 << endl; return 0; } |