CS210 Lab Related FAQ


1. What is the difference between a C++ struct and a C++ class?

Let me answer this question in two parts. In part 1, let's examine the difference between a C struct and a C++ struct. In part 2, we answer the original question: the difference between C++ struct and C++ class .

1) C struct and C++ struct

In C and C++, a structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. A structure declaration forms a template that may be used to create structures. Generally, all the members of a structure are logically related. The following code, which is valid in both C and C++, defines a structure for an address:

struct address
{
	char name[30];
	char street[40];
	char city[20];
	char province[20];
	unsigned long int zip;
};

The similarity of a C and a C++ struct stops right here.
To declare a variable addr_info of type address in C, we write:

struct address addr_info;

In C++, we write:

address addr_info;

Notice that in C, when you define a structure , you are NOT defining a new data type. That's why when you declare a variable with the newly-defined structure, you need to write down the key word struct before the name of the structure.

In C++, however, a new type is defined when a structure is defined, and we can use this new type to declare variables, and the struct keyword is NOT needed.

C++ structure derives from C structure; therefore any C structure is also a valid C++ structure . However, the C++ structure also has some characteristics that a C structure does not have. Specifically, a C++ structure can also contain functions, while a C structure can only contain variables. For example, the following is a valid definition of a C++ structure, but it is not a valid C structure :

struct  cl{
	int get_i();
	void put_i(int j);
   private:
        int i;
};

As can be seen from the above code segment, a C++ structure bears striking similarity with a class definition in C++. But, are they interchangeable? This leads to the second part of our answer to the original question.

2)C++ structure and C++ class

Actually, C++ structure and C++ class are very closely realated. With one exception, they are interchangeable because the structure can also include the data, and the code that manipulates that data, in just the same way that a class can. The only difference between a C++ structure and a class is that, by default, the members of a class are private, while the members of a structure are public (This also clears up an error I made in the first lab). Aside from this distinction, structures and classes perform exactly the same function.

Here is an example of a structure that uses its class-like features.

One last point just in case you are curious about it. A structure defines a class type. Thus, a structure is a class. This was intentional on the part of the inventor of C++ -- Bjarne Stroustrup. He believed that if structures and classes were made more or less equivalent, the transition from C to C++ would be eased.


References:

[1] Horton, Ivor. Beginning Visual C++ 6 . Birmingham: Wrox, 1998.
[2] Schildt, Herbert. C++: From the Ground Up , Berkeley:McGraw-Hill, 1998.
[3] ------, C: The Complete Reference, Berkeley: McGraw-Hill, 1995.


This page was created by Qiang Hu.  It was last modified on: Thursday, 06-Oct-2011 12:13:32 CST

Copyright: Department of Computer Science, University of Regina


[CS Home][CS210]