블로그 이미지
SANGHO KIM

calendar

1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Notice

Tag

2018. 1. 26. 16:23 C++Language

객체 지향의 언어의 핵심 키워드는 아래와 같다.

1. 객체: 클래스를 사용하여 생성한 인스턴스를 의미한다.

2. 클래스: 클래스는 새로운 데이터형을 사용자가 정의함. 맴버변수와 맴버함수(method)로 구성됨.

2.1 맴버함수와 맴버변수

2.2 생성자와 소멸자

 인스턴스가 호출될 때 호출되는 맴버함수를 의미함. 클래스와 이름을 동일하게 지어줌. 생성자는 반환값이 없음. 주로 객체가 생성될 때 객체의 멤버변수를 초기화하는 용도로 사용됨.

생성자를 정의하는 일반적 방법

 클래스명()

{

실행문

}

소멸자를 정의하는 일반적 방법

 ~클래스명()

{

실행문

}

2.3 매개변수가 있는 생성자

2.4 객체의 포인터

객체의 포인터를 선언하는 방법

객체의 포인터는 생성된 객체를 가리키는 포인터입니다. 객체의 포인터를 사용해서 객체의 멤버에 접근하는 것이 가능합니다. '->'를 사용해서 멤버 함수와 멤버 변수에 접근할 수 있습니다.

 클래스명 *객체명

2.5 Const 멤버함수

아래와 같이 함수를 선언하면 getArea() 함수는 const 멤버함수가 되며 다른 멤버의 값을 변경할 수 없게 됩니다. 즉, 클래스의 멤버함수가 멤버의 값을 사용하기만 하고 변경하지 못하게 할 때 const 멤버함수로 선언할 필요가 있습니다.

int getArea() const 

2.6 객체를 매개변수로 갖는 함수

2.7 객체의 복사와 생성자, 소멸자

2.8 참조에 의한 객체전달

2.9 Friend 함수

2.10 This 포인터

2.11 생성자 오버로드

2.12 멤버함수 오버로드

2.13 연산자 오버로드

2.14 동적 메모리 할다오가 클래스

(1) 정수와 정수배열의 동적 메모리 할당

(2) 객체의 동적 메모리 할당

2.15 복사생성자의 정의

복사 생성자는 객체가 복사될 때 호출되는 성성자입니다. 

복사 생성자를 정의하기 위해 함수의 매개변수 값으로 '객체'를 전달함.

복사 생성자가 필요한 경우?

동적 할당된 메모리 공간이 존재할 경우, 객체를 단순히 복사하면 문제가 발생할 수 있음.

기본 복사 생성자(얕은 복사) VS 복사 생성자(깊은 복사)

기본 복사 생성자:  멤버값이 사본 객체의 멤버값으로 똑같이 단순 복사됨.

복사 생성자: 멤버값이 사복 객체의 멤버값으로 동적 할당된 메모리 공간을 고려하여 복사됨.

3. 캡슐화: 클래스 외부의 잘못된 접근을 차단하기 위한 방법.; public, private 등등이 존재함.

4. 추상화: 프로그래밍하고자 하는 어떤 대상을 클래화 하는 과정.

5. 상속: 부모 클래스의 특성을 자손 클래스가 사용하는 것.

추상 클래스, 파생 클래스, 순수 가상함수

6. 다형성: 이름은 같고 기능은 다른 함수들을 사용하는 것.

오버로드, 오버라이드 

'C++Language' 카테고리의 다른 글

C++언어; 객체의 포인터  (0) 2018.02.01
소유관계  (0) 2018.01.24
3가지 형태의 상속  (0) 2018.01.24
C++언어; 상속(2)  (0) 2018.01.23
C++언어; 상속(1)  (0) 2018.01.23
posted by SANGHO KIM
2018. 1. 24. 12:12 C++Language


#include <iostream>

#include <cstring>

using std::cout;

using std::endl;


class cudgel

{

public:

void Swing(){cout<<"Swing a cudgel!"<<endl;}

};


class Police: public cudgel

{

public:

void UseWeapon(){Swing();}

};


int main()

{

Police pol;

pol.UseWeapon();

return 0;

}


객체 포인터; 객체를 가르킬 수 있는 포인터를 의미함

 


#include <iostream>

using std::endl;

using std::cout;

class Person

{

public:

void Sleep(){ 

cout<<"Sleep"<<endl;

}

};

class Student : public Person

{

public:

void Study(){

cout<<"Study"<<endl;

}

};

class PartTimeStd : public Student

{

public:

void Work(){

cout<<"Work"<<endl;

}

};

int main(void)

{

Person* p1=new Person;

Person* p2=new Student;

Person* p3=new PartTimeStd;

p1->Sleep();

p2->Sleep();

p3->Sleep();

return 0;

}

아래 코드의 에러 원인은 무엇일까?

#include <iostream>

#include <cstring>

using std::endl;

using std::cout;

class Person

{

public:

void Sleep(){cout<<"Sleep"<<endl;}

};

class Student : public Person

{

public:

void Study(){cout<<"Study"<<endl;}

};

class PartTimeStd : public Student

{

public:

void Work(){cout<<"Work"<<endl;}

};

int main(void)

{

Person* p3=new PartTimeStd;

p3->Sleep();

//p3->Study(); // Error의 원인

//p3->Work();  // Error의 원인

return 0;


#include <iostream>

#include <cstring>

using std::endl;

using std::cout;


class A

{

public:

void a()

{

cout<<"a()"<<endl;

}

};

class B:public A

{

public:

void b(){

cout<<"b()"<<endl;

}

};

class C:public B

{

public:

void c(){

cout<<"c()"<<endl;

}

};


int main(){

C* c = new C();

c->a();

c->b();

c->c();

return 0;


#include <iostream>

#include <cstring>

using std::endl;

using std::cout;


class A

{

public:

void a()

{

cout<<"a()"<<endl;

}

};

class B:public A

{

public:

void b(){

cout<<"b()"<<endl;

}

};

class C:public B

{

public:

void c(){

cout<<"c()"<<endl;

}

};


int main(){

C* c = new C();

B* b=c;

A* a=b;

cout<<c<<endl;

cout<<b<<endl;

cout<<a<<endl;


return 0;


 #include <cstring>

#include <iostream>

using std::endl;

using std::cout;

class A

{

public:

void a()

{

cout<<"a()"<<endl;

}

};

class B:public A

{

public:

void b(){

cout<<"b()"<<endl;

}

};

class C:public B

{

public:

void c(){

cout<<"c()"<<endl;

}

};

int main(){

C* c = new C();

B* b=c;

A* a=b;

b->a();

b->b();

// b->c();//에러! 

return 0;


#include <iostream>

#include <cstring>

using std::endl;

using std::cout;

class Employee

{

protected:

char name[20];

public:

Employee(char* _name){strcpy(name, _name);}

const char* GetName();

};

const char* Employee::GetName()

{return name;}



class Permanent : public Employee

{

private:

int salary;  // 기본급여

public:

Permanent(char* _name, int sal);

int GetPay();

};

Permanent::Permanent(char* _name, int sal):Employee(_name)

{salary=sal;}

int Permanent::GetPay()

{return salary;}

class Department

{

private:

Employee* empList[10];

int index;

public:

Department(): index(0) { };

void AddEmployee(Employee* emp);

void ShowList(); // 급여 리스트 출력.

};

void Department::AddEmployee(Employee* emp)

{empList[index++]=emp;}

void Department::ShowList() // 급여 리스트 출력.

{

for(int i=0; i<index; i++)

{

cout<<"name: "<<empList[i]->GetName()<<endl;

//cout<<"salary: "<<empList[i]->GetPay()<<endl;

cout<<endl;

}

}

int main()

{

Department department;

department.AddEmployee(new Permanent("KIM", 1000));

department.AddEmployee(new Permanent("LEE", 1500));

department.AddEmployee(new Permanent("JUN", 2000));

department.ShowList();

return 0;

아래 코드의 의미를 이해하시오.

#include <iostream>

#include <cstring>

using std::endl;

using std::cout;


class Person

{

public:

void Sleep(){cout<<"Sleep"<<endl;}

};


class Student:public Person

{

public:

void Study(){cout<<"Study"<<endl;}

};


class PartTimeStd:public Student

{

public:

void Work(){cout<<"Work"<<endl;}

};


int main(void)

{

PartTimeStd p;

Student&ref1=p;

Person&ref2=p;

p.Sleep();

ref1.Sleep();

ref2.Sleep();

return 0;

};

아래 코드를 분석하시오.

#include <iostream>

#include <cstring>

using std::endl;

using std::cout;


class Person

{

public:

void Sleep(){cout<<"Sleep"<<endl;}

};


class Student:public Person

{

public:

void Study(){cout<<"Study"<<endl;}

};


class PartTimeStd:public Student

{

public:

void Work(){cout<<"Work"<<endl;}

};


int main(void)

{

PartTimeStd p;

p.Sleep();

p.Study();

p.Work();

Person&ref=p;

ref.Sleep();

// ref.Study(); //Error의 원인?

// ref.Work(); //Error의 원인? 

}; 


'C++Language' 카테고리의 다른 글

C++언어; 객체의 포인터  (0) 2018.02.01
객체지향의 언어  (0) 2018.01.26
3가지 형태의 상속  (0) 2018.01.24
C++언어; 상속(2)  (0) 2018.01.23
C++언어; 상속(1)  (0) 2018.01.23
posted by SANGHO KIM
2018. 1. 24. 09:41 C++Language

접근 권한 변경

 

public 상속

protected 상속 

private 상속 

public 멤버

public 

protected 

private 

protected 멤버 

protected 

protected 

private 

private 멤버

접불 

접불 

접불 

예를들어서 B 클래스가 A 클래스를 public으로 상속받는 경우, A클래스의 데이터와 함수는 최소 public이다.

예를들어서 B 클래스가 A 클래스를 protected로 상속받는 경우, A클래스의 데이터와 함수는 최소 protected이다.

#include <cstring>

#include <iostream>

using std::cout;

using std::endl;


class Base

{

private:

int a=1;

protected:

int b=1;

public:

int c=1;

};

class Derived: public Base

{

public:

void ShowData() const {

cout<<a<<endl;

}

};

int main(void)

{

Derived test;

test.ShowData();

return 0;


'C++Language' 카테고리의 다른 글

객체지향의 언어  (0) 2018.01.26
소유관계  (0) 2018.01.24
C++언어; 상속(2)  (0) 2018.01.23
C++언어; 상속(1)  (0) 2018.01.23
C++언어; explicit과 mutable 함수의 사용  (0) 2018.01.23
posted by SANGHO KIM