Re: [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: 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;
}
|