logo of SA Coder
sign in


Relationship between Structures and Classes

structures and classes are closely related. Structures are part of the C subset and were inherited from the C language, while classes are a core feature of C++. At first glance, a class may appear similar to a struct in syntax, but their relationship is much closer than that. In fact, the only difference between a class and a struct is that all members are public by default in a struct and private in a class. In all other respects, structures and classes are equivalent.

This means that in C++, a structure defines a class type. For example, you can use a structure to declare a class that controls access to a string, as shown in the code snippet above. In this case, the only difference between using a struct and a class is the default access control of the members.

Why C++ contains both struct and class keywords

The reason why C++ contains both struct and class keywords is justified for several reasons. First, there is no fundamental reason not to increase the capabilities of a structure. In C, structures already provide a means of grouping data, and allowing them to include member functions is a small step. Second, structures and classes are related, so it may be easier to port existing C programs to C++. Finally, providing two different keywords allows the definition of a class to be free to evolve while remaining compatible with C.

Although you can use a struct where you use a class, most programmers prefer to use a class when they want a class and a struct when they want a C-like structure. This is the style that this book follows. Sometimes, the acronym POD is used to describe a C-style structure - one that does not contain member functions, constructors, or destructors. It stands for Plain Old Data.



#include <iostream>
#include <cstring>
using namespace std;

// Define a structure called mystr with a char array of size 255
struct mystr {
    void buildstr(char *s); // public member function declaration
    void showstr(); // public member function declaration
    char str[255]; // private member variable

// Implementation of buildstr function
void mystr::buildstr(char *s)
    if(!*s) *str = '\0'; // initialize string
    else strcat(str, s); // concatenate string

// Implementation of showstr function
void mystr::showstr()
    cout << str << "\n"; // output string

// Main function
int main()
    mystr s; // create object of type mystr
    s.buildstr(""); // initialize string
    s.buildstr("Hello ");
    s.showstr(); // output final string
    return 0;

This example demonstrates how to use a structure to define a class and implement member functions. The `mystr` structure contains a private member variable `str` and two public member functions `buildstr` and `showstr`. The `buildstr` function initializes or concatenates a string, and the `showstr` function outputs the final string. The main function creates an object of type `mystr` and calls its member functions to build and output the final string.

The class mystr could be rewritten by using class as shown here


// Define the mystr class
class mystr {
    // Private member variable to hold the string
    char str[255];

    // Public member function to set the value of str
    void buildstr(char *s);

    // Public member function to print the value of str
    void showstr();

Please login first to comment.