A class in C++ is a user-defined type or data structure declared with any of the keywords class, struct or union (the first two are collectively referred to as non-union classes) that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class declared with the keyword class is private. The private members are not accessible outside the class; they can be accessed only through member functions of the class. The public members form an interface to the class and are accessible outside the class. Instances of a class data type are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.
Differences between a struct and a class in C++ In C++, a class defined with the class keyword has private members and base classes by default. A structure is a class defined with the struct keyword. Its members and base classes are public by default. In practice, structs are typically reserved for data without functions. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private.
Aggregate classes
An aggregate class is a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions. Such a class can be initialized with a brace-enclosed comma-separated list of initializer-clauses. The following code has the same semantics in both C and C++. Since C++20, aggregate types may also be initialized with C99-style designated initializers, but unlike in C, these fields cannot be reordered in declaration.
Just as in C, a semicolon must follow the definition of a struct/class. Between the definition of the struct/class and the semicolon, instances of it may be declared.
POD-structs A POD-struct (Plain Old Data Structure) is a non-union aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined assignment operator and no user-defined destructor. A POD-struct could be said to be the C++ equivalent of a C struct. In most cases, a POD-struct will have the same memory layout as a corresponding struct declared in C. For this reason, POD-structs are sometimes colloquially referred to as "C-style structs".
Properties shared between structs in C and POD-structs in C++ Data members are allocated so that later members have higher addresses within an object, except where separated by an access-specifier. Two POD-struct types are layout-compatible if they have the same number of nonstatic data members, and corresponding nonstatic data members (in order) have layout-compatible types. A POD-struct may contain unnamed padding. A pointer to a POD-struct object, suitably converted using a reinterpret cast, points to its initial member and vice versa, implying that there is no padding at the beginning of a POD-struct. A POD-struct may be used with the offsetof macro.
Declaration and usage C++ classes have their own members. These members include variables (including other structures and classes), functions (specific identifiers or overloaded operators) known as member functions, constructors and destructors. Members are declared to be either publicly or privately accessible using the public: and private: access specifiers respectively. Any member encountered after a specifier will have the associated access until another specifier is encountered. There is also inheritance between classes which can make use of the protected: specifier.
The this keyword
To facilitate classes' ability to reference themselves, C++ implements the this keyword for all member functions. The this keyword acts as a pointer to the current object. Its type is that of a pointer to the current object. The this keyword is especially important for member functions with the class itself as the return value:
As stated above, this is a pointer, so the use of the asterisk (*) is necessary to convert it into a reference to be returned. The type of this for a class X is X* const; if the member function is const-qualified, the type is const X* const.
Global and local class A class defined outside all functions is a global class because its objects can be created from anywhere in the program. If it is defined within a function body then it's a local class because objects of such a class are local to the function scope.
Basic declaration and member variables Non-union classes are declared with the class or struct keyword. Declaration of members are placed within this declaration.
The above definitions are functionally equivalent. Either code will define objects of type Person as having two public data members, name and age. The semicolons after the closing braces are mandatory. After one of these declarations (but not both), Person can be used as follows to create newly defined variables of the Person datatype:
Executing the above code will output
Calvin: 30 Hobbes: 20
Member functions An important feature of the C++ class are member functions. Each datatype can have its own built-in functions (referred to as member functions) that have access to all (public and private) members of the datatype. In the body of these non-static member functions, the keyword this can be used to refer to the object for which the function is called. This is commonly implemented by passing the address of the object as an implicit first argument to the function. Take the above Person type as an example again:
In the above example the public printData method is declared in the body of the class and returns member data. Both name and age are private (default for class) and printData is declared as public which is necessary if it is to be used from outside the class. With the member function printData, printing can be simplified into:
… excerpt ends here. Continue reading the full article.