C++ Inheritance
Overview
Inheritance is a fundamental object-oriented programming concept that allows a class to inherit properties and methods from another class.
Basic Inheritance
Single Inheritance
cpp
#include <iostream>
#include <string>
// Base class
class Animal {
protected:
std::string name;
int age;
public:
Animal(const std::string& n, int a) : name(n), age(a) {}
void eat() {
std::cout << name << " is eating." << std::endl;
}
void sleep() {
std::cout << name << " is sleeping." << std::endl;
}
void displayInfo() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
// Derived class
class Dog : public Animal {
private:
std::string breed;
public:
Dog(const std::string& n, int a, const std::string& b)
: Animal(n, a), breed(b) {}
void bark() {
std::cout << name << " is barking!" << std::endl;
}
void displayDogInfo() {
displayInfo();
std::cout << "Breed: " << breed << std::endl;
}
};
int main() {
Dog myDog("Buddy", 3, "Golden Retriever");
myDog.eat(); // Inherited method
myDog.sleep(); // Inherited method
myDog.bark(); // Derived class method
myDog.displayDogInfo();
return 0;
}Access Specifiers in Inheritance
cpp
#include <iostream>
class Base {
private:
int private_var = 1;
protected:
int protected_var = 2;
public:
int public_var = 3;
void showBase() {
std::cout << "Private: " << private_var << std::endl;
std::cout << "Protected: " << protected_var << std::endl;
std::cout << "Public: " << public_var << std::endl;
}
};
// Public inheritance
class PublicDerived : public Base {
public:
void showPublicDerived() {
// private_var - not accessible
std::cout << "Protected (from derived): " << protected_var << std::endl;
std::cout << "Public (from derived): " << public_var << std::endl;
}
};
// Protected inheritance
class ProtectedDerived : protected Base {
public:
void showProtectedDerived() {
std::cout << "Protected (from derived): " << protected_var << std::endl;
std::cout << "Public (now protected): " << public_var << std::endl;
}
};
// Private inheritance
class PrivateDerived : private Base {
public:
void showPrivateDerived() {
std::cout << "Protected (now private): " << protected_var << std::endl;
std::cout << "Public (now private): " << public_var << std::endl;
}
};Multiple Inheritance
cpp
#include <iostream>
class Printable {
public:
void print() {
std::cout << "Printing..." << std::endl;
}
};
class Saveable {
public:
void save() {
std::cout << "Saving..." << std::endl;
}
};
class Document : public Printable, public Saveable {
private:
std::string content;
public:
Document(const std::string& c) : content(c) {}
void display() {
std::cout << "Content: " << content << std::endl;
}
};
int main() {
Document doc("Hello, World!");
doc.display();
doc.print(); // From Printable
doc.save(); // From Saveable
return 0;
}Polymorphism and Virtual Functions
cpp
#include <iostream>
#include <vector>
class Shape {
public:
// Virtual function
virtual void draw() {
std::cout << "Drawing a generic shape" << std::endl;
}
// Pure virtual function (abstract class)
virtual double area() = 0;
// Virtual destructor
virtual ~Shape() {}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
void draw() override {
std::cout << "Drawing a circle" << std::endl;
}
double area() override {
return 3.14159 * radius * radius;
}
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
void draw() override {
std::cout << "Drawing a rectangle" << std::endl;
}
double area() override {
return width * height;
}
};
int main() {
// Polymorphism
Shape* shapes[2];
shapes[0] = new Circle(5.0);
shapes[1] = new Rectangle(4.0, 6.0);
for (int i = 0; i < 2; i++) {
shapes[i]->draw();
std::cout << "Area: " << shapes[i]->area() << std::endl;
}
// Clean up
for (int i = 0; i < 2; i++) {
delete shapes[i];
}
return 0;
}