[C + +] 44 access level in inheritance

Questions worth thinking about

Can children directly access private members of the parent?

Thinking process

  • According to the object-oriented theory:

  • According to C + + syntax:

Programming experiment: access level in inheritance

#include <iostream>

using namespace std;

class Parent
{
private:
    int mv;
public:
    Parent()
    {
        mv = 100;
    }
    
    int value()
    {
        return mv;
    }
};

class Child : public Parent
{
public:
    int addvalue(int v)
    {
        mv = mv + v;          // How to access the non-public members of the parent class?    
    }
};

int main()
{
    Child c;

    return 0;
}
Output:
test.cpp: In member function 'int Child::addvalue(int)':
test.cpp:8: error: 'int Parent::mv' is private
test.cpp:26: error: within this context
test.cpp:8: error: 'int Parent::mv' is private
test.cpp:26: error: within this context

Access level in inheritance

  • Access levels in object-oriented are not just public and private
  • protected access levels can be defined
  • The meaning of the keyword protected

    • Decorated members cannot be directly accessed by the outside world
    • Decorated members can be directly accessed by subclasses

Programming experiment: the first experience of protected

#include <iostream>

using namespace std;

class Parent
{
protected:
    int mv;
public:
    Parent()
    {
        mv = 100;
    }
    
    int value()
    {
        return mv;
    }
};

class Child : public Parent
{
public:
    int addvalue(int v)
    {
        mv = mv + v;       
    }
};

int main()
{
    Parent p;
    
    cout << "p.mv = " << p.value() << endl;
    
    // p.mv = 1000;  // Error
    
    Child c;
    
    cout << "c.mv = " << c.value() << endl;
    
    c.addvalue(50);
    
    cout << "c.mv = " << c.value() << endl;
    
    // c.mv = 1000;  // Error

    return 0;
}
Output:
p.mv = 100
c.mv = 100
c.mv = 150

Access level selection when defining classes

A comprehensive example of combination and inheritance

  • point inherits from Object
  • Line inherits from Object
  • Line contains two point s

Programming experiment: a comprehensive example

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class Object
{
protected:
    string mName;
    string mInfo;
public:
    Object()
    {
        mName = "Object";
        mInfo = "";
    }
    string name()
    {
        return mName;
    }    
    string info()
    {
        return mInfo;
    }
};

class Point : public Object
{
private:
    int mX;
    int mY;
public:
    Point(int x = 0, int y = 0)
    {
        mX = x;
        mY = y;
        
        mName = "Point";
        
        ostringstream oss;
        oss << "p(" << mX << "," << mY << ")";
        mInfo = oss.str();
    }
    int getX()
    {
        return mX;
    }
    int getY()
    {
        return mY;
    }
};

class Line : public Object
{
private:
    Point mP1;
    Point mP2;
public:
    Line(Point p1, Point p2)
    {
        mP1 = p1;
        mP2 = p2;
        
        mName = "Line";
        
        ostringstream oss;
        oss << "Line from " << mP1.info() << " to " << mP2.info();
        mInfo = oss.str();
    }
};

int main()
{
    Object o;
    
    cout << o.name() << endl;
    cout << o.info() << endl;
    
    cout << endl;
    
    Point p1(1, 2);
    Point p2(3, 4);
    
    cout << p1.name() << endl;
    cout << p1.info() << endl;
    
    cout << endl;
    
    Line l(p1, p2);
    
    cout << l.name() << endl;
    cout << l.info() << endl;
    
    return 0;
}
Output:
Object


Point
p(1,2)

Line
Line from p(1,2) to p(3,4)

Summary

  • Access levels in object-oriented are not just public and private
  • protected decorated members cannot be accessed by the outside world
  • protected enables subclasses to access members of the parent class
  • The protected keyword is designed specifically for inheritance
  • Without protected, code reuse in the real sense cannot be completed

The above contents refer to the series courses of Ditai Software Institute, please protect the original!

Tags: C++ Programming

Posted on Sun, 01 Dec 2019 11:48:20 -0800 by nosmasu