상속과 접근지정자

클래스에서 접근지정자로 접근권한을 제한할 수 있다.
상속도 마찬가지로 접근지정자로 제어할 수 있다.

protected는 외부에선 접근 불가능하지만 자식클래스에서 접근가능하다.

유도된 클래스(자식클래스)에 새로운 기능 추가할때
protected를 사용하면 private 변수일때 public에 getValue()를 만들어 사용하지않고 퍼포먼스를 향상시킬 수 있다.

 

#include <iostream>
using namespace std;
class Base {
protected:
	int m_public;

public:
	Base(const int & a) 
	:m_public(a){

	}
};

class Derived : public Base
{
public:
	Derived(const int & a) 
	:Base(a){
	
	}

	void setValue(int value) {
		Base::m_public + value;
	}
};
int main()
{

	return 0;
}


다중상속일때 단계가 높은 부모클래스에 접근하지 못하게할때 사용한다.

즉, private 으로 상속하면 다중상속일때 하하위 클래스가 직접접근하지 못하게한다.

 

#include <iostream>

using namespace std;

class Base {
public:
	int m_public;

	int funPublic() {
		return 0;
	}
protected:
	int m_protected;
	int funProtected() {
		return 0;
	}
private:
	int m_private;
};

class Derived : public Base
{
public:
	Derived() {
		m_public = 123;
		m_protected = 123;
		//m_private = 123;
		funPublic();
		funProtected();
	}
};

class Derived2 : protected Base
{
public:
	int a;
	Derived2() {
		m_public = 123;
		m_protected = 123;
		//m_private = 123;
		a = funPublic();
		a = funProtected();
	}
protected:
	int a2 = 0;
};

class Derived3 : private Base
{
public:
	int a;

	Derived3() {
		m_public = 123;
		m_protected = 123;
		//m_private = 123;
		a = funPublic();
		a = funProtected();
	}

};

class GrandChild : public Derived3 {
public:
	GrandChild() {
		//m_public; 불가능하다
		cout << a << endl;
	}
};

int main()
{
	Derived der;

	der.m_public = 123;
	//base.m_protected = 123;
	//base.m_private = 123;
	
	//protected 상속
	Derived2 der2;
	//der.m_public = 123;

	//private 상속
	Derived3 der3;
	//der.m_public = 123;


	return 0;
}

캡슐화 Encapsulation
접근 지정자 Access Specifiers 
접근함수Access Functions

큰프로그램을 개발하게 되면 내용이 복잡해진다.
많은변수,많은함수,많은클래스가 사용된다.

뛰어난 프로그래머는 복잡해보이는걸 단순하게 정리잘한다.
재사용을 강조한다.

접근 지정자 Access Specifiers 
public 밖에서 접근이 가능하게한다.
private 밖에서 접근하는걸 막아준다.(기본으로 지정)

private으로 접근지정자가 지정된 클래스는 access Functions(접근함수)를 만들어 주어야한다.
클래스 내부안에 access Functions는 private 변수나 함수에 접근 가능하다.

class cDate
{
//private 밖에서 접근이 불가능하다.
private:	//access specifier(접근지정자)
	int m_month;
	int m_day;
	int m_year;
//access functions 생성
public:
	void setDate(const int &m, const int &d, const int &y)
	{
		m_month = m;
		m_day = d;
		m_year = y;
	}
}

같은 클래스로 생성된 인스턴스는 같은 클래스의 인스턴스에 접근이 가능하다.

class cDate
{
private:	//access specifier(접근지정자)
	int m_month;
	int m_day;
	int m_year;
//access functions 생성
public:
	void copyFrom(const cDate& original) {
		//original안에 변수들은 private이다.
		//같은 클래스 변수접근
		m_month = original.m_month;
		m_day = original.m_day;
		m_year = original.m_year;
	}
};


캡슐화장점
public로 생성 밖에서 변수에 접근해 작업하면 클래스안에 변수가 변경되면 변경해야할 코드가 엄청많아진다.

 

#include <iostream>

using namespace std;


struct sDate
{
	int m_month;
	int m_day;
	int m_year;
};

class cDate
{
private:	//access specifier(접근지정자)
	int m_month;
	int m_day;
	int m_year;
//access functions 생성
public:
	void setDate(const int &m, const int &d, const int &y)
	{
		m_month = m;
		m_day = d;
		m_year = y;
	}

	const int& getMonth() {
		return m_month;
	}

	const int& getDay() {
		return m_day;
	}

	const int& getYear() {
		return m_year;
	}

	void copyFrom(const cDate& original) {
		//original안에 변수들은 private이다.
		//같은 클래스 변수접근
		m_month = original.m_month;
		m_day = original.m_day;
		m_year = original.m_year;
	}
};

int main()
{
	//struct는 바로접근이 가능하다.
	sDate toDay{ 8,4,2025 };
	toDay.m_month = 8;
	toDay.m_day = 4;
	toDay.m_year = 2025;

	//클래스는 바로 접근할 수 없다.
	//접근하려면 public 접근지정자를 선언해주어야한다.
	cDate cToDay;
	cToDay.setDate(8, 4, 2025);
	cToDay.getMonth();

	cDate copy;
	copy.copyFrom(cToDay);

	return 0;
}

'개발 소발 > 개발 C++(기초)' 카테고리의 다른 글

c++ 생성자멤버초기화,위임생성자  (0) 2019.08.05
c++ 생성자 Constructor  (0) 2019.08.05
c++ OOP 객체지향 프로그래밍  (0) 2019.08.02
c++ assert,Ellpsis  (0) 2019.08.02
c++ 방어적프로그래밍 기초  (0) 2019.08.02

+ Recent posts