PrevUpHomeNext

day-07: c++ class: Inheritance


cpp class: Inheritance

Inheritance is called "derived from" in c++. A class can be derived from another class. If class A is derived from class B, B is called base class, and A is called derived class.

Example

For example, derived_class is derived from base_class, then derived_class almost inherits most gene from base_class.

#include <iostream>

class base_class
{
public:
	void method_1()
	{
		std::cout << "This is a method from class base_class" << std::endl;
	}
};

class derived_class: public base_class	// This line tells that derived_class is derived from base_class
{
public:
	void method_2()
	{
		std::cout << "This a method from class derived_class" << std::endl;
	}
};

int main()
{
	auto object = derived_class{};
	object.method_1();
	object.method_2();
}

output:

This is a method from class base_class
This a method from class derived_class

Written on Sep 20, 2024

Back to index

Index

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ==================================================
  #        The c++ programming language.           #
  #                                                #
  #        Home: cppfx.xyz                         #
  #        Join c++ Discord: yZcauUAUyC            #
  #        Deck                                    #
  ==================================================

PrevUpHomeNext