By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Feb 22 '06 #2

P: n/a
"Jordan" <jo***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com
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
};

--
John Carson

Feb 22 '06 #3

P: n/a

"Jordan" <jo***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
| 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.
|
Feb 22 '06 #4

P: n/a
I stand corrected. Thank you for correcting me.

Feb 22 '06 #5

P: n/a
John Carson wrote:
"Jordan" <jo***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com -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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.