ArticleslgStudy

computer science

C++ classes

C++ classes is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand C++ classes rather than just read about it. In short: 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 t…

Key takeaways

  • C++ classes belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect C++ classes to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of C++ classes from memory before moving on to harder problems.

Reference excerpt

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.

Worked examples

Example 1 — a first encounter with C++ classes

Start with the simplest possible case. Write down what C++ classes claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to C++ classes before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about C++ classes ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of C++ classes

In research
C++ classes appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses C++ classes in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
C++ classes is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, Class (computer programming), so understanding it makes those chapters shorter.
In everyday life
Look for C++ classes outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “C++ classes” →

How to study C++ classes in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what C++ classes means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain C++ classes out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is C++ classes in simple terms?

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…

Why does C++ classes matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study C++ classes?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on C++ classes.

Tags

  • C++
  • Class (computer programming)

Keep exploring