OOPortal OOPortal


OO Encapsulation  «Prev  Next»
Lesson 5What is a class?
Objective Understand what a class is.

What is a class in C++?

A class is an extension of the idea of struct found in C. To facilitate encapsulation, the concept of struct is augmented in C++ to allow functions, as well as data, to be members. In OOP terminology, a member function is frequently called a method..
For our immediate purposes, we can consider class and struct to be practically the same thing. We will be discussing how class differs from struct a little later in this module. For the next few lessons though, we will use struct in our examples because struct is more familiar to C programmers.
A class is an expanded concept of a data structure, in that instead of holding only data, it can hold both data and functions.
An object is an instantiation of a class. If one were to compare this with a procedural language, a class would be the type, and an object would be the variable.
Classes are generally declared using the keyword class, with the following format:

class ClassConcept {
  access_specifier_1:
    member1;
  access_specifier_2:
    member2;
  ...
} object_names;

Where ClassConcept is a valid identifier for the class, object_names is an optional list of names for objects of this class.
The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.