3

Can some one please tell me why exactly classes were introduced in C++. Are there some things that classes can perform that cannot be achieved using structures?

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Kunal
  • 511
  • 2
  • 6
  • 12
  • 6
    You might want a book explaining all of the aspects of OOP. There's much more to it than data members and function pointers, and C++ has much, much better support for the techniques involved. – chris Jun 14 '12 at 19:32
  • 3
    @chris: OOP as a paradigm has nothing to do with "classes", they simply reflect an implementation. I think it's a good question actually as there are very few difference between a `struct` and a `class` type in C++ (in fact, only two) – Ed S. Jun 14 '12 at 19:34
  • @EdS., My point is that C++ classes relate to OOP in a much more direct and easy way than C data structures. – chris Jun 14 '12 at 19:35
  • 2
    You can achieve nearly everything (in terms of results) with struct and func pointers. **BUT** you need to do a lot of things manually that the C++ compiler does automatically for you. And additionally you will need to perform a pre-processing stage to validate that your struct are not being abused. All in all the extra manual work makes it quite a different experience. – Martin York Jun 14 '12 at 19:35
  • 2
    @chris: And my point is that you are wrong. Really, in C++ there are only two differences between a struct and a class, neither of which have anything to do with OOP principles and the behavior of which can be easily overridden. – Ed S. Jun 14 '12 at 19:36
  • 2
    @EdS., I saw the question as a C->C++ transition instead of a C++ struct -> C++ class transition. As Loki mentions, C++ helps out greatly. – chris Jun 14 '12 at 19:37
  • @chris: I think the OP is asking why classes even exist considering they are so similar to structs and that anything you can do with a class type you can also do with a struct type. Now, is this question a good fit for SO... I don't know, maybe not, but it is an interesting question. – Ed S. Jun 14 '12 at 19:40
  • this has all ready been discussed http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c – nate_weldon Jun 14 '12 at 19:41
  • @EdS., I agree, that makes for an interesting question. It's just that the way I saw it was different, so I gave a different style of comment. – chris Jun 14 '12 at 19:41
  • @chris: Fair enough, the question is certainly not overly detailed. – Ed S. Jun 14 '12 at 19:42
  • 2
    @Kunal Can you clarify: Are you comparing C++ classes to C++ structs, or C++ classes to C structs? They are (unfortunately) different, and that's why people are making all these confusing comments. – Mooing Duck Jun 14 '12 at 19:50
  • possible duplicate of [What are the differences between struct and class in C++](http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c) – Bo Persson Jun 14 '12 at 20:30
  • In a nutshell, the key advantage of C++ over C is that classes provide you automagic `goto`s in the form of destructors. This enables RAII and early exit as the central code flow idioms in C++, which makes the language "local". – Kerrek SB Jun 14 '12 at 23:15

4 Answers4

1

Read Stroustrup's explanation here: What is so great about classes?

A class is the representation of an idea, a concept, in the code. An object of a class represents a particular example of the idea in the code. Without classes, a reader of the code would have to guess about the relationships among data items and functions - classes make such relationships explicit and "understood" by compilers. With classes, more of the high-level structure of your program is reflected in the code, not just in the comments.

A well-designed class presents a clean and simple interface to its users, hiding its representation and saving its users from having to know about that representation. If the representation shouldn't be hidden - say, because users should be able to change any data member any way they like - you can think of that class as "just a plain old data structure"; for example:

struct Pair {
        Pair(const string& n, const string& v) : name(n), value(v) { }
        string name, value;
};

Note that even data structures can benefit from auxiliary functions, such as constructors.

Community
  • 1
  • 1
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 1
    I don't believe this addresses the question. There are two differences between a `struct` and a `class` in C++, namely, the default access modifier, in terms of members and inheritance. So, the question is; why have classes at all? – Ed S. Jun 14 '12 at 19:36
  • See the third para. Defining a new set of access modifiers for your data types makes much more sense with a new keyword. – dirkgently Jun 14 '12 at 19:39
  • Ahh, I read too quickly. You're right, and that's what I have always assumed. – Ed S. Jun 14 '12 at 19:41
0

Actually, structures in C++ are a special case of classes, and are included to make C++ more backward compatible with C. Recall from C that a structure is a description of a collection of data items of potentially different types that's given a name. This is also somethimes called a record in other languages.

In C++ there is a basic thing called a class, wich is a collection of data items of potentially different types along with functions that operate on them.

In C, a structure lets you define a chunk of memory using the structure declaration as a pattern.

In C++, a class lets you define a chunk of memory using the class declaration as a pattern, and have it associated with functions that operate on it.

In C, a common pattern to do things with a structure is something like this:

struct T {
   ?* some declarations */
};

struct T * foo.

struct T * create_a_T(}{/* malloc memory */}
void init_a_T(struct T * t){/* set it up */}
void do_something_with_T(struct T * t, ?* more arguments*/){/* more good things */}

In C++, a class method works exactly the same way, except that you don't need to have the struct T * as an argument; that's take care of automatically.

Along with all that, in C++ a class lets you make certain elements of the class "private" or "protected", which is to say they're not accessible from outside the class methods. This can protect you from many kinds of programming errors.

Also, in C++ you can use inheritance, which means that you can define a new class that is "just like" an existing class, except for code that adds other data declarations or methods. This lets you organize the code around a relationship called "a kind of", so, for example, you can have a class Car, and define a new class Mercedes that is "a kind of" Car, but has special Mercedes-related pieces.

All of this goes along with the notion of "object oriented programming" which is too much to explain in one answer, but which has historically proven to make it easier to build large programs correctly.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
0
class CL
{
    int x;
};

struct ST
{
    int x;
};

The only difference that ST and CL have is that CL.x is private and ST.x is public.

Best practice: when you define a small data structure with only public objects use the keyword struct, differently use the keyword class.

Update:

this is valid --> class A;

this is invalid --> struct B; // error: forward declaration of struct B

Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
-7

C++ is oriented to objects, and the objets are made by classes, so C++ only use it

  • 3
    This is wrong. You're thinking in terms of buzzwords. If you know C++ you know that the semantic differences between a struct and a class are few, minor, and do not in any way conflict with OOP principles. – Ed S. Jun 14 '12 at 19:38