C++

Inheritance Syntax and Visibility Modes in C++

1004
0
Inheritance Syntax and Visibility Modes in C++

Inheritance Syntax in C++

Inheritance is a powerful feature of C++ that allows you to create new classes based on existing ones. The new class, called the derived class, inherits the properties of the existing class, called the base class. The syntax for inheritance in C++ is as follows:

class DerivedClass : visibility-mode BaseClass {
    // ...
};
C++

Here, DerivedClass is the new class being created, and BaseClass is the existing class from which DerivedClass is derived. The visibility-mode specifies the visibility of the base class members in the derived class. We’ll talk more about visibility modes in the next section.

For example, let’s say we have a base class called Animal:

class Animal {
public:
    void eat() {
        std::cout << "The animal is eating." << std::endl;
    }
};
C++

We can create a derived class called Dog that inherits from Animal like this:

class Dog : public Animal {
public:
    void bark() {
        std::cout << "Woof!" << std::endl;
    }
};
C++

The public visibility mode means that the public members of Animal are also public members of Dog. Now we can create Dog objects and call its methods, as well as the inherited Animal methods:

Dog myDog;
myDog.eat(); // The animal is eating.
myDog.bark(); // Woof!
C++

Visibility Modes in C++

C++ provides three visibility modes for base class members in a derived class:

  • public: Public members of the base class are public members of the derived class. This means that they can be accessed by anyone, including code outside the class and derived classes.
  • protected: Protected members of the base class are protected members of the derived class. This means that they can only be accessed by code inside the class and its derived classes.
  • private: Private members of the base class are not accessible in the derived class. This means that they cannot be accessed by anyone, not even the derived class itself.

By default, if no visibility mode is specified, C++ assumes private visibility. However, when deriving a class using the class keyword, the default visibility is private, whereas when deriving a class using the struct keyword, the default visibility is public.

Let’s look at an example. Suppose we have a base class called Person:

class Person {
public:
    std::string name;

protected:
    int age;

private:
    std::string password;
};
C++

We can create a derived class called Student that inherits from Person:

class Student : public Person {
public:
    void study() {
        std::cout << name << " is studying." << std::endl;
        std::cout << "Age: " << age << std::endl;
        //std::cout << password << std::endl; // This won't compile because password is private.
    }
};
C++

In this case, we used public visibility mode, which means that name and age are public members of Student, and can be accessed by anyone who can access Student. However, password is a private member of Person, and cannot be accessed by anyone outside Person.

Student myStudent;
myStudent.name = "John Doe";
myStudent.age = 20;
//myStudent.password = "1234"; // This won't compile because password cannot be accessed outside the `Person` class.
C++

We can create a new derived class called GraduateStudent that also inherits from Person, but with protected visibility mode for name and age:

class GraduateStudent : protected Person {
public:
    void research() {
        std::cout << name << " is researching." << std::endl;
        std::cout << "Age: " << age << std::endl;
        //std::cout << password << std::endl; // This won't compile because password is private.
    }
};
C++

In this case, name and age are protected members of GraduateStudent, which means they can be accessed by code inside GraduateStudent, as well as any derived classes of GraduateStudent. However, they cannot be accessed by code outside of GraduateStudent.

GraduateStudent myGradStudent;
//myGradStudent.name = "Jane Doe"; // This won't compile because name is protected.
//myGradStudent.age = 25; // This won't compile because age is protected.
//myGradStudent.password = "5678"; // This won't compile because password is private.
myGradStudent.research(); // This will compile and execute as expected.
C++

In conclusion, the visibility mode in inheritance determines the accessibility of base class members in the derived class. By default, the visibility mode is private for class inheritance and public for struct inheritance. The three visibility modes in C++ are public, protected, and private, and they control the accessibility of base class members in the derived class.

xalgord
WRITTEN BY

xalgord

Constantly learning & adapting to new technologies. Passionate about solving complex problems with code. #programming #softwareengineering

Leave a Reply