Advertisement

What are classes?

Started by March 15, 2002 03:13 AM
19 comments, last by Mage_gr 22 years, 8 months ago
This idea that structs and classes are different is nonsense. They are exactly the same thing, except structs default to public access.
Yeah I think Sabreman has it right.

-=Lohrno
Advertisement
class & struct are the same except I think members in a struct have defualt pubilc, while members in class are defualt protected I think not sure

The main diffrence between class & struct is it is oop, meaning it allows inheritance & which therfore allows featrues like virtual function & polymorphism (which is excellent)

but for complete detail on OOP you will need a tutorial !

hope this helps WizHarD
who is it that keeps on nicking WizHarD name !! :P
quote: Original post by SabreMan
This idea that structs and classes are different is nonsense. They are exactly the same thing, except structs default to public access.


That is true in C++. In the C sence, however, structs can not have functions (except function pointers, which are really data) and they can not have any concept of access (therfore always public). The reason that there has to be both struct and class in C++ is to allow a class to default to private (much more oo) and C code to still compile since struct was considered public in C.

So, structs and classes are the same -> but both must still be there for compatability.

- mongrelprogrammer
- I hate these user ratings. Please rate me down. (Seriously) -
I *know* SabreMan has it right. Here's a quote from the Standard:

quote:
A structure is a class defined with the class-key struct; its members and base classes are public by default.


So:

struct Thing
{
Thing() {}
~Thing() {}
};

is exactly the same as:

class Thing
{
public:
Thing() {}
~Thing() {}
};

Yes, structs have destructors, and yes you can inherit from structs, and structs can inherit from classes and other structs. A struct *is a* class!

There is one place where the struct keyword cannot replace class, and that is in template parameter lists, such as:

template&ltclass A>
void f( A a )
{
// ...
}

In answer to the OP, a class is the fundemental building block of object-oriented programming. It allows related data and behaviour to be grouped together into a single modular entity. Through its members, a class provides the specification for an object, which is an instance of a class. A fully specified class is also known (under some circumstances) by another term you may have heard: Abstract Data Type (ADT). This is simply because a class effectively defines a type. For example, if you are used to C code, then you are used to seeing expressions such as:

int i = 5;
void f( int x );

Where "int" is a type, and i is an instance of that type. A class allows you to introduce new types into your program. For example, if you wanted your own special int type, you might declare a class like this:

class MyInt
{
public:
MyInt() {}
MyInt( int val ) : val_(val) {}
MyInt& operator=( const MyInt& other );
MyInt& operator=( const int val );
operator int() { return val_;}
private:
int val_;
};

This introduces a new type to the compiler, called MyInt, so you can now write expressions like:

MyInt i = 5;
void f( MyInt x );

And expect them to do something very similar to the earlier example. Of course, this example is silly, but perhaps you would want to make MyInt do something different to a built-in int, which would make the effort worthwhile.

You don't *have to* have classes define types exactly like built-in types. You might want a ErrorLogger class, or a Monster class, or what-have-you. The principle is still the same: you define the structure and behaviour of the class by putting both data and related functions into the class definition, and you have a convenient modular unit of code.

Of course, there's a lot more to it than that, but perhaps that gives you a flavour of what a class is. Oh, and just so you don't forget, a struct *is a* class. Now, go and do some reading!

--


[edited by - SabreMan on March 15, 2002 2:27:31 PM]
quote: Original post by mongrelprogrammer
That is true in C++. In the C sence, however, structs can not have functions

We''re not talking about C.
Advertisement
quote: Original post by SabreMan
We're not talking about C.


I am aware that we are not talking about C. However, you stated that there is no difference between structs and classes except for their default accesability. I just wanted to clarify why there was a need for both since they now perform the exact same operations to someone who didn't even know what a class was. I know that if I asked what a class was and someone said that structs are the same thing I would be confused as to why both structs and classes exist. If the compatability with C was not strived for there would be no reason to have classes -> just structs with private access as default.

Remember, I am not trying to say you are wrong -> you _are_ right : ) I just wanted to clarify a potentially confusing point about why they are both there...

- mongrelprogrammer

[edited by - mongrelprogrammer on March 15, 2002 3:25:17 PM]
- I hate these user ratings. Please rate me down. (Seriously) -
In addition to just defaulting to public members, structs also default to public inheritance.

[edited by - SilentCoder on March 15, 2002 3:53:36 PM]
While we''re on the topic, unions and enums are also classes in C++.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
You''re right about a union, but an enum isn''t really a class. It may be implemented in a similar manner, but it isn''t one.

This topic is closed to new replies.

Advertisement