Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Inheritance
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 27 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++IntermediateLesson 27 of 50CamboFreelanceJune 19, 2026

C++ Inheritance

Learn C++ inheritance: derive classes from a base class, reuse code, call base constructors, and build class hierarchies.


Tutorials › C++ › Inheritance
Intermediate9 min readLesson 27 of 50

What is Inheritance?

Inheritance lets a class (child/derived) acquire attributes and methods of another class (parent/base), promoting code reuse.

class DerivedClass : accessSpecifier BaseClass { };
#include <iostream>
using namespace std;

// Base class
class Animal {
  public:
    string name;
    void eat()   { cout << name << " is eating." << endl; }
    void sleep() { cout << name << " is sleeping." << endl; }
};

// Derived class
class Dog : public Animal {
  public:
    void bark() { cout << name << " says: Woof!" << endl; }
};

class Cat : public Animal {
  public:
    void meow() { cout << name << " says: Meow!" << endl; }
};

int main() {
    Dog d; d.name = "Rex";
    d.eat();    // inherited from Animal
    d.bark();   // Dog's own method

    Cat c; c.name = "Whiskers";
    c.sleep();  // inherited
    c.meow();
    return 0;
}

Output:

Rex is eating.
Rex says: Woof!
Whiskers is sleeping.
Whiskers says: Meow!

Constructor Inheritance

The derived constructor calls the base constructor using the initializer list:

class Vehicle {
  public:
    string brand;
    Vehicle(string b) : brand(b) { }
};

class ElectricCar : public Vehicle {
  public:
    int range;
    ElectricCar(string b, int r) : Vehicle(b), range(r) { }
    void info() {
        cout << brand << " — Range: " << range << "km" << endl;
    }
};

ElectricCar tesla("Tesla", 600);
tesla.info();   // Tesla — Range: 600km

Exercise

Create a base class Shape with a color attribute and a describe() method. Derive Circle (with radius) and Square (with side), each with an area() method.

Show Solution
#include <iostream>
#include <cmath>
using namespace std;

class Shape {
  public:
    string color;
    Shape(string c) : color(c) { }
    void describe() { cout << "Color: " << color << endl; }
};

class Circle : public Shape {
  public:
    double radius;
    Circle(string c, double r) : Shape(c), radius(r) { }
    double area() { return 3.14159 * radius * radius; }
};

class Square : public Shape {
  public:
    double side;
    Square(string c, double s) : Shape(c), side(s) { }
    double area() { return side * side; }
};

int main() {
    Circle ci("Red", 5);
    ci.describe();
    cout << "Circle area: " << ci.area() << endl;

    Square sq("Blue", 4);
    sq.describe();
    cout << "Square area: " << sq.area() << endl;
    return 0;
}

Quiz

  1. Which keyword is used to inherit from a base class?

    • A) extends
    • B) inherits
    • C) : (colon)
    • D) <<
    Answer

    C) Colon — e.g., class Dog : public Animal.

Summary

  • Inheritance enables a derived class to reuse base class members.
  • Use class Child : public Parent syntax.
  • Derived constructors call base constructors via initializer lists.
← Previous: Encapsulation Next: Polymorphism →
c++inheritanceoopintermediate
PreviousLesson 26: C++ EncapsulationNextLesson 28: C++ Polymorphism
Back to All Tutorials