473,624 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(co nst 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(c onst 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
5 4109
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
"Jordan" <jo***********@ gmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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

"Jordan" <jo***********@ gmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
I stand corrected. Thank you for correcting me.

Feb 22 '06 #5
John Carson wrote:
"Jordan" <jo***********@ gmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3289
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
11
5705
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little difference between a struct and a class in C++. Both have inheritance, access modifiers, etc. The only diff I see is the default access level is public vs. private. I have always just used structs as a minimal class, as that is the way
22
3344
by: Ook | last post by:
We have had a discussion on the differences between a class and a structure, and no one is in agreement. As I understand it, a structure defaults to public, a class defaults to private. There are issues about constructors that I'm not clear on. I do know that I can take a simple project with a class that has constuctors, destructors, accessors, and modifiers, change the classes to structs, and it compiles and runs fine. Can some kind soul...
5
96480
by: VM | last post by:
What's marshalling? I've had to use it extensively for a project but I don't know what it means. I tried to look for a definition in the Internet but I couldn't find anything that would explain what it is. Is converting a C-style struct to a C# class (or struct) marshalling? And what about the function exports? Are those Marshalling too? Thanks for your help and thanks for helping me with the Dll exports (the several messages I posted in...
5
8724
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
17
2078
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
2
1782
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng #include <iostream>
32
2713
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
8233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.