YouTip LogoYouTip

Cpp Inheritance

body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; } h1, h2, h3 { color: #333; } code { background-color: #f4f4f4; padding: 2px 5px; border-radius: 3px; } pre { background-color: #f4f4f4; padding: 15px; border-radius: 5px; overflow-x: auto; } a { color: #0066cc; text-decoration: none; } a:hover { text-decoration: underline; } .nav { margin-bottom: 20px; } .nav ul { list-style-type: none; padding: 0; } .nav li { display: inline; margin-right: 10px; } .content { max-width: 800px; margin: auto; } .example { border: 1px solid #ddd; padding: 15px; margin: 20px 0; border-radius: 5px; }

C++ Inheritance |

C++ Tutorial

C++ Tutorial | C++ Introduction | C++ Environment Setup | C++ Basic Syntax | C++ Comments | C++ Data Types | C++ Variable Types | C++ Variable Scope | C++ Constants | C++ Modifier Types | C++ Storage Classes | C++ Operators | C++ Loops | C++ Decision Making | C++ Functions | C++ Numbers | C++ Arrays | C++ Strings | C++ Pointers | C++ References | C++ Date & Time | C++ Basic I/O | C++ struct | C++ vector | C++ Data Structures

C++ Object-Oriented

C++ Classes & Objects | C++ Inheritance | C++ Overloading | C++ Polymorphism | C++ Data Abstraction | C++ Data Encapsulation | C++ Interfaces (Abstract Classes)

C++ Advanced Tutorial

C++ Files and Streams | C++ Exception Handling | C++ Dynamic Memory | C++ Namespaces | C++ Templates | C++ Preprocessor | C++ Signal Handling | C++ Multithreading | C++ Web Programming

C++ Resources

C++ STL Tutorial | C++ Import Standard Library | C++ Standard Library | C++ Useful Resources | C++ Examples | C++ Quiz | <a href="#" title="C++ Standard I/O - ">C++ Standard I/O - <iostream> | <a href="#" title="C++ File I/O Library - ">C++ File I/O Library - <fstream> | <a href="#" title="C++ Standard Library ">C++ Standard Library <sstream> | <a href="#" title="C++ Standard Library ">C++ Standard Library <iomanip> | <a href="#" title="C++ Container Class ">C++ Container Class <array> | <a href="#" title="C++ Container Class ">C++ Container Class <vector> | <a href="#" title="C++ Container Class ">C++ Container Class <list> | <a href="#" title="C++ Container ">C++ Container <forward_list> | <a href="#" title="C++ Container Class ">C++ Container Class <deque> | <a href="#" title="C++ Container Class ">C++ Container Class <stack> | <a href="#" title="C++ Container Class ">C++ Container Class <queue> | <a href="#" title="C++ Container Class ">C++ Container Class <priority_queue> | <a href="#" title="C++ Container Class ">C++ Container Class <set> | <a href="#" title="C++ Container Class ">C++ Container Class <unordered_set> | <a href="#" title="C++ Container Class ">C++ Container Class <map> | <a href="#" title="C++ Container Class ">C++ Container Class <unordered_map> | <a href="#" title="C++ Container Class ">C++ Container Class <bitset> | <a href="#" title="C++ Algorithm Library ">C++ Algorithm Library <algorithm> | <a href="#" title="C++ Standard Library ">C++ Standard Library <iterator> | <a href="#" title="C++ Standard Library ">C++ Standard Library <functional> | <a href="#" title="C++ Standard Library ">C++ Standard Library <numeric> | <a href="#" title="C++ Standard Library ">C++ Standard Library <complex> | <a href="#" title="C++ Standard Library ">C++ Standard Library <valarray> | <a href="#" title="C++ Standard Library ">C++ Standard Library <cmath> | <a href="#" title="C++ Standard Library ">C++ Standard Library <string> | <a href="#" title="C++ Standard Library ">C++ Standard Library <regex> | <a href="#" title="C++ Standard Library ">C++ Standard Library <ctime> | <a href="#" title="C++ Standard Library ">C++ Standard Library <chrono> | <a href="#" title="C++ Multithreading Library ">C++ Multithreading Library <thread> | <a href="#" title="C++ Standard Library ">C++ Standard Library <mutex> | <a href="#" title="C++ Standard Library ">C++ Standard Library <condition_variable> | <a href="#" title="C++ Standard Library ">C++ Standard Library <future> | <a href="#" title="C++ Standard Library ">C++ Standard Library <atomic> | <a href="#" title="C++ Standard Library ">C++ Standard Library <type_traits> | <a href="#" title="C++ Standard Library ">C++ Standard Library <typeinfo> | <a href="#" title="C++ Exception Handling Library ">C++ Exception Handling Library <exception> | <a href="#" title="C++ Exception Handling Library ">C++ Exception Handling Library <stdexcept> | <a href="#" title="C++ Standard Library ">C++ Standard Library <cstdio> | <a href="#" title="C++ Standard Library ">C++ Standard Library <cstdint> | <a href="#" title="C++ Memory Management Library ">C++ Memory Management Library <memory> | <a href="#" title="C++ Memory Management Library ">C++ Memory Management Library <new> | <a href="#" title="C++ Standard Library ">C++ Standard Library <utility> | <a href="#" title="C++ Standard Library ">C++ Standard Library <random> | <a href="#" title="C++ Standard Library ">C++ Standard Library <locale> | <a href="#" title="C++ Standard Library ">C++ Standard Library <codecvt> | <a href="#" title="C++ Standard Library ">C++ Standard Library <cassert> | <a href="#" title="C++ Standard Library ">C++ Standard Library <cwchar> | <a href="#" title="C++ Standard Library ">C++ Standard Library <climits> | <a href="#" title="C++ Standard Library ">C++ Standard Library <cfloat> | <a href="#" title="C++ Standard Library ">C++ Standard Library <cstdlib> | <a href="#" title="C++ Standard Library ">C++ Standard Library <numbers> | C++ OpenCV


C++ Inheritance

The most important concept in object-oriented programming is inheritance. Inheritance allows us to define a class based on another class, making it easier to create and maintain an application. This also achieves the effect of reusing code functionality and improving execution efficiency.

When creating a class, you don't need to rewrite new data members and member functions; you only need to specify that the new class inherits the members of an existing class. This existing class is called the base class, and the new class is called the derived class.

Inheritance represents an is-a relationship. For example, a mammal is an animal, a dog is a mammal, therefore, a dog is an animal, and so on.

Inheritance Diagram

The code is as follows:

// Base class
class Animal {
    // eat() function
    // sleep() function
};

// Derived class
class Dog : public Animal {
    // bark() function
};

Base Class & Derived Class

A class can be derived from multiple classes, meaning it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base classes. The class derivation list names one or more base classes in the following form:

class derived-class: access-specifier base-class

Where, the access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If no access-specifier is used, it defaults to private.

Assume there is a base class Shape, and Rectangle is its derived class, as shown below:

Example

#include<iostream>
using namespace std;

// Base class
class Shape {
public:
    void setWidth(int w) {
        width = w;
    }
    void setHeight(int h) {
        height = h;
    }
protected:
    int width;
    int height;
};

// Derived class
class Rectangle: public Shape {
public:
    int getArea() {
        return (width * height);
    }
};

int main(void) {
    Rectangle R;
    R.setWidth(5);
    R.setHeight(7);
    // Print the area of the object.
    cout << "Total area: " << R.getArea() << endl;
    return 0;
}
← Collection SublistCollection Hashtable Key β†’