C++ Class Construct  «Prev  Next»
Lesson 1

C++ Class Construct

Question: What are the internal workings of the Class construct in C++?
In C++, the class construct serves as the blueprint for creating objects, and it is one of the core principles of the Object Oriented Programming (OOP) paradigm. It encapsulates data members (variables) and member functions (methods) into a single unit, allowing data and behaviors to be tied together.
A simple class declaration in C++ might look like this:
class MyClass {
public:
    int myVar;
    void myMethod() {
        // some code here
    }
};

Key C++ Class Concepts

  1. Data Members: These are variables declared within a class. They represent the state of an object of that class. Data members can be of any type: basic types, other classes, or user-defined types.
  2. Member Functions: These are functions declared within a class. They manipulate the state of an object and define the behaviors that the object can perform.
  3. Access Modifiers: These are keywords used in the class to specify the accessibility of data members and member functions. There are three access modifiers in C++:

  1. public: Members declared as public are accessible from any part of the program.
  2. private: Members declared as private are only accessible within the class they are declared in, not from outside the class or derived classes.
  3. protected: Members declared as protected are accessible within the class they are declared in, and from derived classes.

The access modifier for members of a class is private by default.

Class Instantiation and Objects

To use a class, you must create an instance of that class, commonly referred to as an object. Each object is allocated its own memory for the data members, but all objects share the same member functions.
MyClass obj1;  // object of MyClass

Here, obj1 is an object of MyClass. You can access members of the class using the dot operator (.) like
obj1.myVar and 
obj1.myMethod().

Constructors and Destructors

A class in C++ can have special member functions called constructors and destructors, which are automatically called when an object of the class is created and destroyed, respectively.
A constructor has the same name as the class and does not have a return type. It is mainly used for initializing the data members of the class. If a constructor is not provided, the compiler provides a default constructor.
A destructor also has the same name as the class, preceded by a tilde (~). It is used to clean up resources (like dynamic memory) that the object might have acquired during its lifetime.

class MyClass {
public:
    MyClass() {   // constructor
        // initialization code here
    }
    ~MyClass() {  // destructor
        // cleanup code here
    }
};


Member Function Definitions

The definitions of member functions can be written inside the class (inline) or outside the class. If it is outside the class, the scope resolution operator :: is used.
class MyClass {
public:
    void myMethod();  // function declaration
};

void MyClass::myMethod() {  // function definition outside the class
    // code here
}

SEMrush Software

Inheritance and Polymorphism

C++ classes support the OOP concepts of inheritance and polymorphism.
Inheritance allows a new class to be defined based on an existing class, leading to a parent-child relationship. The child class inherits the members of the parent class, and can add its own members too.
Polymorphism allows a single interface to represent different types at runtime. This is done through virtual functions and can be used to achieve dynamic behavior. Understanding the internal workings of the class construct in C++ allows you to fully utilize the power of OOP in your programs, leading to code that is more modular, easier to understand, and simpler to maintain.
This module explores some of the basic concepts of building classes in C++ and introduces you to the internal workings of the class construct.

We have learned about structures, which provide a way to group data elements. We have examined functions, which organize program actions into named entities. In this module, we will put these ideas together to create classes and introduce several classes, starting with simple ones and working toward more complicated examples. In addition, we will focus first on the details of classes and objects. At the end of the module we will take a wider view, discussing what is to be gained by using the OOP approach.

A Simple Class

Our first program contains a class and two objects of that class. Although it is simple, the program demonstrates the syntax and general features of classes in C++. Here is the listing for the SMALLOBJ program.

// smallobj.cpp
// demonstrates a small, simple object
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class smallobj //define a class{
 private:
 int somedata; //class data
 public:
  void setdata(int d) //member function to set data{ 
   somedata = d; 
  }
  void showdata() //member function to display data{ 
    cout << "Data is " << somedata << endl; 
  }
};

////////////////////////////////////////////////////////////////
int main(){
 smallobj s1, s2; //define two objects of class smallobj
 s1.setdata(1066); //call member function to set data
 s2.setdata(1776);
 s1.showdata(); //call member function to display data
 s2.showdata();
 return 0;
}

Data and Functions

The class smallobj defined in this program contains one data item and two member functions. The two member functions provide the only access to the data item from outside the class. The first member function sets the data item to a value, and the second displays the value.
Placing data and functions together into a single entity is a central idea in object-oriented programming. This is shown in Figure 3.1.

Figure 3-1: Data and Functions
Figure 3-1: Data and Functions

Example of Class Rectangle in C++

class CRectangle {
 int x, y;
 public:
  void setValues (int,int);
  int area (void);
} rect;

Declares a class called CRectangle and an object of this class called rect. This class contains four members:
  1. two data members of type int (member x and member y) with private access (because private is the default access level) and
  2. two member functions with public access: setValues() and area(),
of which for now we have only included their declaration, not their definition.

Ad The C++ Programming Language