Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what's the difference between type struct and type class exactly?

Reply
Thread Tools

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

 
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      02-22-2006
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
 
Reply With Quote
 
 
 
 
Jordan
Guest
Posts: n/a
 
      02-22-2006
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.

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      02-22-2006
"Jordan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
Peter_Julian
Guest
Posts: n/a
 
      02-22-2006

"Jordan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.
|


 
Reply With Quote
 
Jordan
Guest
Posts: n/a
 
      02-22-2006
I stand corrected. Thank you for correcting me.

 
Reply With Quote
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      02-22-2006
John Carson wrote:
> "Jordan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Difference between struct and class and pointer to member data Pep C++ 4 09-10-2009 09:35 AM
any link and binary level difference between c++ struct and class? kjin101@gmail.com C++ 4 10-11-2007 09:48 AM
Difference between struct and class in c++ and c Robben C++ 3 09-07-2005 01:01 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments