● public
#include<iostream>
#include<iomanip>
using namespace std;
class A
{
int a1;//1
protected:
int b1;//2
public:
int c1;//3
void setA1(int n){ a1 = n;}
void setB1(int n) { b1 = n; }
int getA1()const { return a1 ; }
int getB1()const { return b1; }
};
class B:public A
{
int a2;//4
protected:
int b2;//5
public:
int c2;//6
void setA2(int n) { a2 = n; }
void setB2(int n) { b2 = n; }
int getA2()const { return a2; }
int getB2()const { return b2; }
};
class C:public B
{
int a3;//7
protected:
int b3;//8
public:
int c3;//9
void setA3(int n) { a3 = n; }
void setB3(int n) { b3 = n; }
int getA3()const { return a3; }
int getB3()const { return b3; }
};
void main()
{
C cc;
// 접근이 불가능한거 함수로 표현
cc.setA1(1);
cc.setB1(2);
cc.c1 = 3;
cc.setA2(4);
cc.setB2(5);
cc.c2 = 6;
cc.setA3(7);
cc.setB3(8);
cc.c3 = 9;
cout << cc.getA1() << "\n" << endl;
cout << cc.getB1() << "\n" << endl;
cout << cc.c1 <<"\n"<< endl;
cout << cc.getA2() << "\n" << endl;
cout << cc.getB2() << "\n" << endl;
cout << cc.c2 <<"\n"<< endl;
cout << cc.getA3() << "\n" << endl;
cout << cc.getB3() << "\n" << endl;
cout << cc.c3 <<"\n"<< endl;
//직접 적근 가능 한거 직접 표현
}
● private
#include<iostream>
#include<iomanip>
using namespace std;
/* privated 거의 안씀
*/
class A
{
int a1;//1
protected:
int b1;//2
public:
int c1;//3
void setA1(int n) { a1 = n; }
int getA1() { return a1; }
void setB1(int n) { b1 = n; }
int getB1() { return b1; }
void setC1(int n) { c1 = n; }
int getC1() { return c1; }
};
class B :private A
{
int a2;//4
protected:
int b2;//5
public:
int c2;//6
void setA1(int n) { A::setA1(n);}
int getA1() { return A::getA1();}
void setB1(int n) { A::setB1(n);}
int getB1() { return A::getB1();}
void setC1(int n) { A::setB1(n);}
int getC1() { return A::getA1();}
void setA2(int n) { a2=n; }
int getA2() { return a2; }
void setC2(int n) { c2 = n; }
int getB2() { return b2; }
void setB2(int n) { b2 = n; }
int getC2() { return c2; }
};
class C :private B
{
int a3;//7
protected:
int b3;//8
public:
int c3;//9
//A
void setA1(int n) { B::setA1(n); }
int getA1() { return B::getA1(); }
void setB1(int n) { B::setB1(n); }
int getB1() { return B::getB1(); }
void setC1(int n) { B::setC1(n); }
int getC1() { return B::getC1(); }
//B
void setA2(int n) { B::setA2(n); }
int getA2() { return B::getA2(); }
void setB2(int n) { B::setB2(n); }
int getB2() { return B::getB2(); }
void setC2(int n) { B::setC2(n); }
int getC2() { return B::getC2(); }
//C
void setA3(int n) { a3 = n; }
int getA3() { return a3; }
void setB3(int n) { b3 = n; }
int getB3() { return b3; }
};
void main()
{
C cc;
//직접
cc.setA1(1);
cout << cc.getA1() << "\n" << endl;
cc.setB1(2);
cout << cc.getB1() << "\n" << endl;
cc.setC1(3);
cout << cc.getC1() << "\n" << endl;
cc.setA2(4);
cout << cc.getA2() << "\n" << endl;
cc.setB2(5);
cout << cc.getB2() << "\n" << endl;
cc.setC2(6);
cout << cc.getC2() << "\n" << endl;
cc.setA3(7);
cout << cc.getA3() << "\n" << endl;
cc.setB3(8);
cout << cc.getB3() << "\n" << endl;
//직접
cc.c3 = 9;
cout << cc.c3 << "\n" << endl;
}
● protected
#include<iostream>
#include<iomanip>
using namespace std;
class A
{
int a1;//1
protected:
int b1;//2
public:
int c1;//3
void setA1(int n) { a1 = n; }
int getA1() { return a1; }
};
class B :protected A
{
int a2;//4
protected:
int b2;//5
public:
int c2;//6
void setA2(int n) { a2 = n; }
int getA2() { return a2; }
};
class C :protected B
{
int a3;//7
protected:
int b3;//8
public:
int c3;//9
void setA1(int n) { A::setA1(n); }
void setB1(int n) { b1 = n; }
void setC1(int n) { c1 = n; }
int getA1() { return A::getA1(); }
int getB1() { return b1; }
int getC1() { return c1; }
void setA2(int n) { B::setA2(n); }
void setC2(int n) { c2 = n; }
void setB2(int n) { b2 = n; }
int getA2() { return B::getA2() ; }
int getB2() { return b2; }
int getC2() { return c2; }
void setA3(int n) { a3 = n; }
void setB3(int n) { c3 = n; }
int getA3() { return a3; }
int getB3() { return c3; }
};
void main()
{
C cc;
//직접
cc.setA1(1);
cout << cc.getA1() << "\n" << endl;
cc.setB1(2);
cout << cc.getB1() << "\n" << endl;
cc.setC1(3);
cout << cc.getC1() << "\n" << endl;
cc.setA2(4);
cout << cc.getA2() << "\n" << endl;
cc.setB2(5);
cout << cc.getB2() << "\n" << endl;
cc.setC2(6);
cout << cc.getC2() << "\n" << endl;
cc.setA3(7);
cout << cc.getA3() << "\n" << endl;
cc.setB3(8);
cout << cc.getB3() << "\n" << endl;
//직접
cc.c3=9;
cout << cc.c3 << "\n" << endl;
}
'Programming > C++' 카테고리의 다른 글
[C++] Pointers (0) | 2018.12.28 |
---|---|
[C++] 오버로딩이 불가능한 연산자 종류 (0) | 2018.04.16 |