for Robot Artificial Inteligence

14. Inheritance-between-Classes

|

(Protected)[http://nlp.kookmin.ac.kr/sskang/lect/cpp/ppp.htm]

  • 보호(protected) 멤버
    1. 필요성
    • 파생클래스에게만 접근이 허용되는 비공개(private) 멤버 2. 속성
    • 기본클래스의 비공개(private) 멤버의 속성을 그대로 유지
    • 다만, 파생클래스의 접근이 허용된다는 점만이 private과 다름!

기본클래스 접근 방식 : private, public, protected

  1. 기본클래스를 public으로 접근할 때 : 원래대로 유지
    • 기본클래스의 공개(public) 멤버 –> 파생플래스의 공개(public) 멤버
    • 기본클래스의 보호(protected) 멤버 –> 파생플래스의 보호(protected) 멤버
  2. 기본클래스를 private으로 접근할 때 : 모두 private으로
    • 기본클래스의 공개(public) 멤버 –> 파생플래스의 비공개(private) 멤버
    • 기본클래스의 보호(protected) 멤버 –> 파생플래스의 비공개(private) 멤버
  3. 기본클래스를 protected로 접근할 때 : 모두 protected로
    • 기본클래스의 공개(public) 멤버 –> 파생플래스의 보호(protected) 멤버
    • 기본클래스의 보호(protected) 멤버 –> 파생플래스의 보호(protected) 멤버
    • 어떤 경우든 기본클래스의 private 멤버들은 파생클래스에 의해 접근될 수 없음!

Exercise

Main.cpp

#include <iostream>
#include "point.h"

using namespace std;
/* inheritance between classes */

void operationOnPoints();

int main()
{
    operationOnPoints();

    return 0;
}
void operationOnPoints()
{
    Point2D p2(10, 67);

    p2.Point::setX(5);

    cout << p2.getX() << endl;
    cout << p2.getY() << endl;
}


point.h

#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED

class Point //base, parent, superclass
{
    protected:
        int x;
    public:
        Point(int =0);
        ~Point();
        int getX() { return x; }
        void setX(int);
};
class Point2D : public Point //derived, child, subclass
{
    protected:
        int y;
    public:
        Point2D(int =0, int =0);
        ~Point2D();
        int getY() { return y; }
        void setY(int);
        void setX(int);
        void setXY(int, int);

};
class Point3D : public Point2D
{

};

/*
    class Point2D : public Point
    everything what is inside Point (excluding constructor and destructor) will be in Point2D
    private - CANNOT ACCESS
    protected - protected
    public - public

    class Point2D : protected Point
    everything what is inside Point (excluding constructor and destructor) will be in Point2D
    private - CANNOT ACCESS
    protected - protected
    public - protected


    class Point2D : private Point
    everything what is inside Point (excluding constructor and destructor) will be in Point2D
    private - CANNOT ACCESS
    protected - private
    public - private

*/
#endif // POINT_H_INCLUDED

point.cpp

#include <iostream>
#include "point.h"


using namespace std;

Point::Point(int x)
{
    this->x = x;
    cout << "The constructor from the Point class has just been invoked" << endl;
}
Point::~Point()
{
    cout << "The destructor from the Point class has just been invoked" << endl;
}
void Point::setX(int x)
{
    this->x = x;
    cout << "Im from Point" << endl;
}

Point2D::Point2D(int x, int y) : Point(x) // parents
{
    this->y = y;
    cout << "The constructor from the Point2D class has just been invoked" << endl;
}
Point2D::~Point2D()
{
    cout << "The destructor from the Point2D class has just been invoked" << endl;
}

void Point2D::setY(int y)
{
    this->y = y;
}
void Point2D::setXY(int x, int y)
{
    setX(x);
    setY(y);
}
void Point2D::setX(int x)
{
    this->x = x;
    cout << "Im from Point2D" << endl;
}

Comments