what's the difference between type struct and type class exactly?

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Hi,

Consider this code:

--- beginning of code ---
#include <iostream>
using namespace std;

class Child{
public:
Child(const int& n); // constructor
void Show();
private:
int ChildNmb; // the childs number in the sequence of siblings
const int& NmbOfSibls; // the number of siblings (including itself)
};

// constructor:
Child::Child(const int& n)
:ChildNmb(n), NmbOfSibls(n){ } // initialisation - const int& NmbOfSibls
= n,
// NmbOfSibls becomes an alias for the actual argument variable.
void Child::Show() {
cout << "I am child number: " << ChildNmb << ". I have " <<
NmbOfSibls-1 << " brothers and sisters." << endl;
}

//************************************

int main(){
int n=0; // Number of children
++n; Child c1(n);
++n; Child c2(n);
++n; Child c3(n);
c1.Show(); c2.Show(); c3.Show();

++n; Child c4(n);
c1.Show(); c2.Show(); c3.Show(); c4.Show();
}

--- end of code ---

I'm a bit confused about this code... For instance:

Using "class Child{" gives *EXACTLY* the same result as using "struct
Child{" - both compiles and runs as well... Why? And why is there both
type struct and type class, when it seems like there's no difference?

Another thing is that I'm a bit confused about the constructor:
"Child::Child(const int& n)" - because I can easily understand if n in
this case becomes an alias for another variable. But if one pass a
number such as Child c5(5); to the program, then 5 is not a variable...
So 5 is stored in memory somewhere and therefore it doesn't need an
alias... Or am I missing something?


Best regards / Med venlig hilsen
Martin Jørgensen
 
J

Jordan

Martin,

1. Structs and Classes - Structs and classes appear to be the same, but
there are a few distinctions. First, all members of a struct are
public by default. Stucts are commonly used to represent records which
could be composed of several data types internally. Structs support
member functions but not much else beyond that.

Classes, however, support a wider range of functionality. By default,
all members are private in a class. Classes also support inheritance
and polymorphism where this is not supported by structs.

2. Variables - Even though '5' is technically a constant, its value
will be stored in a temporary memory location during the Child's
constructor call (and thus behave like a variable). All constants that
participate in operations will be stored in variable memory space. For
example, when program execution reaches a method call, several things
are placed on top of a "stack frame" in memory. The stack frame
contains data needed by the method. There will be a segment for both
parameters and local variables. That means that all values passed into
the method call will be placed in a memory location in the stack frame
under the parameters segment. This would be true if you passed in a
variable directly to the method because C++ passes by value.
 
J

John Carson

Jordan said:
Martin,

1. Structs and Classes - Structs and classes appear to be the same,
but there are a few distinctions. First, all members of a struct are
public by default. Stucts are commonly used to represent records
which could be composed of several data types internally. Structs
support member functions but not much else beyond that.

Classes, however, support a wider range of functionality. By default,
all members are private in a class. Classes also support inheritance
and polymorphism where this is not supported by structs.

This is wrong. Inheritance and polymorphism work with structs.

The only differences between a struct and a class are that

1. a struct's members are public by default and a class's members are
private by default.

2. inheritance is public by default for a struct and private by default for
a class. More precisely, given,

struct Derived : Base
{
// stuff
};

the inheritance type is public (regardless of whether Base is a class or
struct). And given:

class Derived : Base
{
// stuff
};

the inheritance type is private (regardless of whether Base is a class or
struct).

To remember this easily, just think of the base class as a special type of
member.

Another way of looking at it is the following. Given

struct Derived : Base
{
// stuff
};

the following is exactly equivalent:

class Derived : public Base
{
public:
// stuff
};

Likewise, given

class Derived : Base
{
// stuff
};

the following is exactly equivalent

struct Derived : private Base
{
private:
// stuff
};
 
P

Peter_Julian

| Martin,
|
| 1. Structs and Classes - Structs and classes appear to be the same,
but
| there are a few distinctions. First, all members of a struct are
| public by default. Stucts are commonly used to represent records
which
| could be composed of several data types internally. Structs support
| member functions but not much else beyond that.
|
| Classes, however, support a wider range of functionality. By default,
| all members are private in a class. Classes also support inheritance
| and polymorphism where this is not supported by structs.

Except for the default access mode, there is absolutely no difference
between a struct and a class. While styles usually prefer keyword struct
when dealing with a POD-type, a struct supports inheritence, templates,
init lists, etc...

|
| 2. Variables - Even though '5' is technically a constant, its value
| will be stored in a temporary memory location during the Child's
| constructor call (and thus behave like a variable). All constants
that
| participate in operations will be stored in variable memory space.
For
| example, when program execution reaches a method call, several things
| are placed on top of a "stack frame" in memory. The stack frame
| contains data needed by the method. There will be a segment for both
| parameters and local variables. That means that all values passed
into
| the method call will be placed in a memory location in the stack frame
| under the parameters segment. This would be true if you passed in a
| variable directly to the method because C++ passes by value.
|
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

John said:
-snip-

the following is exactly equivalent

struct Derived : private Base
{
private:
// stuff
};

Etc... Okay, I learned that a struct's members are public by default and
a class's members are private by default. The other things you talked
about such as inheritance and polymorphism I didn't read about yet, but
I'm almost there in my book and then I'll hopefully understand it better
when I read that part :)

Thanks for the answers. I think I'll just wait a bit and read more in my
book and then read this thread again and ask questions at that time, if
there is something I don't understand at that moment...


Best regards / Med venlig hilsen
Martin Jørgensen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top