Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPP Module 03 - Inheritance

This project is part of the 42 C++ modules. The main goal of this module is to understand inheritance in C++ through a small robot hierarchy based on ClapTrap, ScavTrap, FragTrap, and DiamondTrap.

The project is written in C++98 and compiled with strict warning flags.

c++ -Wall -Wextra -Werror -std=c++98

Project Overview

CPP Module 03 focuses on building classes that reuse and extend behavior from other classes. It starts with a basic class, ClapTrap, then introduces derived classes, construction and destruction order, multiple inheritance, and the diamond inheritance problem.

The exercises are organized in order:

ex00/  ClapTrap
ex01/  ScavTrap
ex02/  FragTrap
ex03/  DiamondTrap

Build and Run

Each exercise has its own directory and Makefile.

To build an exercise:

cd ex00
make

To run the program:

./claptrap

To clean object files:

make clean

To remove object files and the executable:

make fclean

To rebuild everything:

make re

Rules Followed

This project follows the main rules of the C++ modules:

  • compiled with c++
  • uses -Wall -Wextra -Werror
  • compatible with -std=c++98
  • does not use forbidden C functions such as printf, malloc, calloc, realloc, or free
  • does not use using namespace std
  • does not use external libraries
  • avoids STL containers and algorithms before the allowed modules
  • keeps function implementations in .cpp files instead of header files
  • uses include guards in header files

Exercise 00 - ClapTrap

Goal

The first exercise introduces a basic ClapTrap class. This class represents a simple robot with a name, hit points, energy points, and attack damage.

Main Class

class ClapTrap

Required Attributes

The class stores:

  • name: the robot's name
  • hitPoints: the robot's health
  • energyPoints: the robot's available energy
  • attackDamage: the amount of damage caused by an attack

Required Member Functions

void attack(const std::string& target);
void takeDamage(unsigned int amount);
void beRepaired(unsigned int amount);

What I Learned

In this exercise, I practiced how to design a basic C++ class with private or protected data and public behavior. I learned how an object can protect its internal state while exposing functions that safely modify that state.

I also practiced the Orthodox Canonical Form, which includes:

  • default constructor
  • copy constructor
  • copy assignment operator
  • destructor

This exercise also reinforced the idea that constructors and destructors are automatically called when objects are created and destroyed.

Key Concepts

Encapsulation

The class keeps its data inside itself and only allows controlled access through member functions. For example, hit points should not be changed directly from outside the class; they should be changed through functions such as takeDamage() or beRepaired().

Object State

The robot's behavior depends on its current state. If it has no hit points or no energy points, it cannot attack or repair itself.

Member Functions

The functions work on the current object. For example, attack() uses the current object's energy points and attack damage.


Exercise 01 - ScavTrap

Goal

The second exercise introduces inheritance by creating a new class named ScavTrap, derived from ClapTrap.

Main Class

class ScavTrap : public ClapTrap

Required Behavior

ScavTrap reuses the base behavior of ClapTrap, but it has different initial values:

  • hit points: 100
  • energy points: 50
  • attack damage: 20

It also has its own special ability:

void guardGate(void);

What I Learned

In this exercise, I learned how a derived class can inherit data and behavior from a base class. ScavTrap is a specialized version of ClapTrap, so it can reuse common functions such as takeDamage() and beRepaired().

I also learned that constructing a derived object first constructs the base part, then the derived part. Destruction happens in the opposite order.

Key Concepts

Inheritance

Inheritance allows a class to be built from another class. ScavTrap inherits from ClapTrap, which means a ScavTrap is a kind of ClapTrap.

Base Class and Derived Class

ClapTrap is the base class. ScavTrap is the derived class. The base class provides shared behavior, while the derived class adds or changes behavior.

Construction Chain

When a ScavTrap object is created, the ClapTrap constructor runs first. This makes sure the base part of the object exists before the derived part is initialized.

Destruction Chain

When a ScavTrap object is destroyed, the ScavTrap destructor runs first, then the ClapTrap destructor. This is the reverse of construction.

Function Hiding

ScavTrap provides its own version of attack(). When calling attack() on a ScavTrap object, the ScavTrap version is used instead of the ClapTrap version.


Exercise 02 - FragTrap

Goal

The third exercise adds another derived class named FragTrap. Like ScavTrap, it inherits from ClapTrap, but it has different values and a different special ability.

Main Class

class FragTrap : public ClapTrap

Required Behavior

FragTrap has these initial values:

  • hit points: 100
  • energy points: 100
  • attack damage: 30

It also has its own special ability:

void highFivesGuys(void);

What I Learned

In this exercise, I practiced creating another derived class from the same base class. This helped me understand how multiple derived classes can share common behavior while still having their own identity.

I also learned that inheritance helps reduce duplicated code. Since FragTrap and ScavTrap both share basic robot behavior, that shared logic can stay inside ClapTrap.

Key Concepts

Code Reuse

Instead of rewriting the same damage and repair logic in every class, the common logic is placed in ClapTrap. Derived classes can reuse it.

Specialization

FragTrap is a more specific type of ClapTrap. It has its own stats and its own ability, while still keeping the base features of a ClapTrap.

Shared Base Behavior

Both ScavTrap and FragTrap depend on ClapTrap for common behavior. This makes the class hierarchy easier to maintain.


Exercise 03 - DiamondTrap

Goal

The final exercise introduces multiple inheritance and the diamond inheritance problem. DiamondTrap inherits from both FragTrap and ScavTrap.

Main Class

class DiamondTrap : public FragTrap, public ScavTrap

Required Behavior

DiamondTrap combines features from both parent classes:

  • hit points from FragTrap
  • energy points from ScavTrap
  • attack damage from FragTrap
  • attack() from ScavTrap

It also has its own name, while the ClapTrap base part has a different name ending with "_clap_name".

The class also implements:

void whoAmI(void);

This function prints both the DiamondTrap name and the ClapTrap name.

What I Learned

In this exercise, I learned that multiple inheritance can be powerful but also dangerous. If two parent classes come from the same base class, the final class may accidentally contain two copies of the same base class.

This problem is called the diamond problem. To solve it, the project can use virtual inheritance, so the final object contains only one shared ClapTrap base part.

Key Concepts

Multiple Inheritance

Multiple inheritance means one class inherits from more than one parent class. DiamondTrap inherits from both FragTrap and ScavTrap.

Diamond Problem

The diamond problem happens when a class inherits from two classes that both inherit from the same base class. In this project, both FragTrap and ScavTrap inherit from ClapTrap, and DiamondTrap inherits from both of them.

Without a solution, DiamondTrap may contain two separate ClapTrap parts, which can cause ambiguity.

Virtual Inheritance

Virtual inheritance is used to make sure that only one shared base class subobject exists. In this project, it can be used so that DiamondTrap contains only one ClapTrap base part.

Example:

class ScavTrap : virtual public ClapTrap
class FragTrap : virtual public ClapTrap

Name Shadowing

DiamondTrap has its own name, while ClapTrap also has a name. These two names are different pieces of data. When two members have the same name, the more specific one can hide the other. To access the base class version clearly, scope resolution can be used.

Scope Resolution

The :: operator is used to specify exactly which class member should be used.

Example:

ClapTrap::name
ScavTrap::attack(target)

This helps avoid confusion when several parent classes contain members with the same name.


Summary

CPP Module 03 helped me understand how C++ classes can be connected through inheritance. I learned how to build a base class, create derived classes, reuse common behavior, customize child classes, and deal with more complex inheritance structures.

The most important lesson from this module is that inheritance is useful for sharing behavior, but it must be designed carefully. Simple inheritance makes code easier to reuse, while multiple inheritance can create ambiguity if the class hierarchy is not clearly structured.

About

introducing inheritance in C++

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

Contributors

Languages