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.

C++ Programming Language

C++ Class is extension of struct Concept

In C++, a class is an extension of the concept of a struct in C. While a struct is primarily used to group data members (typically variables), a class expands this idea by adding the ability to include both data members and member functions (methods). This facilitates encapsulation, one of the fundamental principles of Object-Oriented Programming (OOP). Encapsulation allows for bundling data (attributes) and functions (methods) together while also controlling access to them, typically through access specifiers such as `private`, `protected`, and `public`.
  • Private members: These are only accessible within the class itself.
  • Public members: These are accessible from outside the class.
  • Protected members: These are accessible within the class and derived classes.

This structure allows you to hide the internal workings of a class from outside interference and only expose what is necessary. Here's an example of a `Tank` class with the data members you listed:
#include <iostream>
#include <string>

class Tank {
    // Private data members (attributes)
private:
   std::string periscope;
   std::string gunMantlet;
   std::string coaxialGun;
   std::string mainGun;
   std::string driversOptics;
   std::string driversHatch;
   std::string continuousTrack;
   int machineGunAmmunition;
   std::string hatchOrCupola;
   std::string gunTurret;
   std::string hull;
   std::string engineAirIntake;
   std::string engineCompartment;
   std::string link;
   std::string roadWheel;

public:
   // Constructor
   Tank(std::string ps, std::string gm, std::string cg, std::string mg, std::string do_,
        std::string dh, std::string ct, int ammo, std::string hoc, std::string gt, std::string hl,
        std::string eai, std::string ec, std::string lnk, std::string rw) 
       : periscope(ps), gunMantlet(gm), coaxialGun(cg), mainGun(mg), driversOptics(do_),
         driversHatch(dh), continuousTrack(ct), machineGunAmmunition(ammo), hatchOrCupola(hoc),
         gunTurret(gt), hull(hl), engineAirIntake(eai), engineCompartment(ec), link(lnk), roadWheel(rw) 
   {}

   // Public method to display tank information
   void displayTankInfo() const {
       std::cout << "Tank Details:" << std::endl;
       std::cout << "Periscope: " << periscope << std::endl;
       std::cout << "Gun Mantlet: " << gunMantlet << std::endl;
       std::cout << "Coaxial Gun: " << coaxialGun << std::endl;
       std::cout << "Main Gun: " << mainGun << std::endl;
       std::cout << "Driver's Optics: " << driversOptics << std::endl;
       std::cout << "Driver's Hatch: " << driversHatch << std::endl;
       std::cout << "Continuous Track: " << continuousTrack << std::endl;
       std::cout << "Machine Gun Ammunition: " << machineGunAmmunition << std::endl;
       std::cout << "Hatch or Cupola: " << hatchOrCupola << std::endl;
       std::cout << "Gun Turret: " << gunTurret << std::endl;
       std::cout << "Hull: " << hull << std::endl;
       std::cout << "Engine Air Intake: " << engineAirIntake << std::endl;
       std::cout << "Engine Compartment: " << engineCompartment << std::endl;
       std::cout << "Link: " << link << std::endl;
       std::cout << "Road Wheel: " << roadWheel << std::endl;
   }
};

int main() {
   // Creating a tank object
   Tank armoredTank("Advanced Periscope", "Heavy Gun Mantlet", "7.62mm Coaxial Gun", "120mm Main Gun", 
	   "Night Vision Optics", "Reinforced Hatch", "Steel Continuous Track", 300, 
	   "Commander's Hatch", "Rotating Gun Turret", "Heavy Armor Hull", "Rear Air Intake", 
	   "V12 Engine Compartment", "Reinforced Link", "High-Durability Road Wheel");

   // Display the tank information
   armoredTank.displayTankInfo();
    return 0;
}
Key Points:
  1. Private Data Members: The internal components of the tank (e.g., periscope, gun mantlet) are encapsulated within the class using private access. This prevents them from being directly accessed outside the class.
  2. Constructor: The constructor initializes all the data members when an instance of the `Tank` class is created.
  3. Public Method: The `displayTankInfo` method allows us to display the tank's details, providing controlled access to the private data members.

This code encapsulates the tank's data and provides a clear interface for interacting with it through member functions.

SEMrush Software