객체지향프로그램밍 (Object-Oriented Programming) 1
C++ popular C 객체지향 (object oriented) C++ C : 상위계층언어특징 + 어셈블리언어특징 C++ : 소프트웨어개발플랫폼에객체지향개념제공 객체지향 : 자료와이들자료를어떻게다룰것인지따로생각하지않고단지하나의사물로생각 형 변수가사용하는메모리크기 변수가가질수있는정보 변수에게가능한조작 왜새로운형을만드는가? 복잡한문제를쉽게해결 2
클래스와멤버 클래스 변수들과연관된함수들을결합시킨새로운형 클래스를선언함으로써새로운형생성 멤버변수 (member variable, member data) 클래스내의변수 멤버함수 (member function), 메소드 클래스내의함수, 객체가무엇을하는지를결정 객체 (object), instance 클래스에의해만들어진변수 3
클래스의선언 클래스선언 class 클래스명 <member variable> <member function> class Cat unsigned int itsage; unsigned int itsweight; void Meow( ); 클래스선언시메모리할당되지않음 4
객체 (object) 정의 객체정의 Cat Frisky, Tom, Garfield; unsigned int Weight; 객체, 인스턴스 : 클래스를실체화한것 5
객체 ( 클래스 ) 멤버에접근하기. 연산자 : 객체의멤버에접근 Frisky.itsWeight = 50; Frisky.Meow( ); int = 5; (X) int x; x = 5; (O) Cat.itsAge = 5; (X) Cat Frisky; Frisky.itsAge=5; (O) Frisky.Bark( ); (X) Frisky.Meow( ); (O) 6
#include <iostream> list 6.1 class Cat public: int itsage; int itsweight; int main() Cat Frisky; Frisky.itsAge = 5; std::cout << "Frisky is a cat who is " ; std::cout << Frisky.itsAge << " years old.\n"; return 0; 7
전용 (Private) 과범용 (Public) 범용 클래스밖에서도접근 public : class Cat public: unsigned int itsage; unsigned int itsweight; void Meow( ); Cat Frisky; Frisky.itsAge=5; (O) 8
전용 전용 (Private) 과범용 (Public) (cont.) 클래스안의메소드 ( 멤버함수 ) 에의해서만접근 private : class Cat private: unsigned int itsage; unsigned int itsweight; void Meow( ); Cat Frisky; Frisky.itsAge=5; (X) 9
전용 (Private) 과범용 (Public) (cont.) 명시되지않으면전용이 default ( 기본 ) 효력 : 클래스선언끝또는다른제어 keyword class Cat unsigned int itsheight; unsigned int itsname; public: void Fly( ); unsigned int itsage; private: unsigned int itsweight; void Meow( ); 멤버변수를전용화 전용변수설정및접근 : 범용멤버함수이용 10
class Cat public: unsigned int GetAge(); void SetAge(unsigned int Age); list 6.2 unsigned int GetWeight(); void SetWeight(unsigned int Weight); void Meow(); private: unsigned int itsage; unsigned int itsweight; 11
클래스메소드구현 함수정의하듯이구현 전용 + 범용멤버자료에자유로운접근 리턴형클래스명 :: 함수명 ( 매개변수리스트 )... 12
#include <iostream> class Cat public: int GetAge(); void SetAge (int age); void Meow(); private: int itsage; list 6.3 int Cat::GetAge() return itsage; void Cat::SetAge(int age) itsage = age; void Cat::Meow() std::cout << "Meow.\n"; 13
list 6.3 int main() Cat Frisky; Frisky.SetAge(5); Frisky.Meow(); std::cout << "Frisky is a cat who is " ; std::cout << Frisky.GetAge() << " years old.\n"; Frisky.Meow(); return 0; 14
생성자와소멸자 생성자 (constructor) 주로클래스의멤버자료 (data) 초기화 int a; Cat Frisky; int a = 5; Cat Frisky(20, 10, 4); 클래스그자신과동일한이름을가진특별한멤버함수 객체 (object) 가만들어질때자동호출 사용자가호출할수없음 반환형없음 (void 형도가져서는안됨 ) 필요한수만큼의매개변수가짐 함수중첩 (overloading) 가능 접근지정자는반드시 public 15
생성자와소멸자 (cont.) class Cat public: Cat( int height ); Cat( int height, int weight, int age ); private: unsigned int itsheight; unsigned int itsweight; unsigned int itsage; Cat::Cat( int height ) itsheight = height; Cat::Cat( int height, int weight, int age) itsheight = height; itsweight = weight; itsage = age; 16
생성자와소멸자 (cont.) 소멸자 (destructor) 객체값을소멸, 할당된메모리해제 ~ 클래스 이름을가짐 Ex) ~Cat( ) 사용자가호출할수없음 반환형없음 (void 형도될수없음 ) 매개변수없음 오버로드할수없음 접근지정자는 public Hw) 객체생성시기와소멸자호출순서의관계 17
생성자와소멸자 (cont.) 기본생성자 매개변수도없고함수의내용도없는생성자함수 사용자가생성자함수를만들지않으면기본 (default) 생성자함수가있는것으로취급 Cat Frisky(5, 7, 9); Cat( int a, int b, int c ) 생성자호출 Cat Frisky(5); Cat( int a ) 생성자호출 Cat Frisky; Cat( ) 생성자호출 함수는매개변수가없더라도괄호를필요 법칙의예외 18
#include <iostream> class Cat public: Cat(int initialage); ~Cat(); int GetAge(); void SetAge(int age); void Meow(); private: int itsage; list 6.4 // constructor of Cat, Cat::Cat(int initialage) itsage = initialage; // destructor Cat::~Cat() 19
int Cat::GetAge() return itsage; list 6.4 void Cat::SetAge(int age) itsage = age; void Cat::Meow() std::cout << "Meow.\n"; int main() Cat Frisky(5); Frisky.Meow(); std::cout << "Frisky is a cat who is " ; std::cout << Frisky.GetAge() << " years old.\n"; Frisky.Meow(); Frisky.SetAge(7); std::cout << "Now Frisky is " ; std::cout << Frisky.GetAge() << " years old.\n"; return 0; 20
const 멤버함수 class Cat public: void Func( ) const; private: int itsweight; void Cat::Func( ) const itsweight = 100; (X) 멤버함수를 const 선언시 해당클래스의모든멤버값변경불능 const 멤버함수를통해컴파일러가오류를찾도록만듬 가능한많은멤버함수를 const로선언 21
#include <iostream> class Cat public: Cat(int initialage); ~Cat(); int GetAge() const; void SetAge (int age); void Meow(); private: int itsage; list 6.5 // const function Cat::Cat(int initialage) itsage = initialage; std::cout << "Cat Constructor\n"; Cat::~Cat() std::cout << "Cat Destructor\n"; 22
int Cat::GetAge() const return (itsage++); list 6.5 void Cat::SetAge(int age) itsage = age; void Cat::Meow() std::cout << "Meow.\n"; int main() Cat Frisky; Frisky.Meow(); Frisky.Bark(); Frisky.itsAge = 7; return 0; 23
클래스선언과멤버함수작성 클래스선언 클래스와사용자간의통신인터페이스 클래스의자료형 / 함수종류알림.h /.hpp 파일사용 함수정의 함수의구체적동작정의 클래스제작프로그래머에게만관심.cpp 파일사용 24
#include <iostream> list 6.6 class Cat public: Cat (int initialage); ~Cat( ); int GetAge( ) const return itsage; void SetAge( ) const itsage = age; void Meow( ) const std::cout << Meow. \n ; private: int itsage; CAT.hpp 25
#include "cat.hpp" Cat::Cat(int initialage) itsage = initialage; list 6.7 CAT.cpp Cat::~Cat() int main() Cat Frisky(5); Frisky.Meow(); std::cout << "Frisky is a cat who is " ; std::cout << Frisky.GetAge() << " years old.\n"; Frisky.Meow(); Frisky.SetAge(7); std::cout << "Now Frisky is " ; std::cout << Frisky.GetAge() << " years old.\n"; return 0; 26
클래스메소드의 inline 구현 방법 1 : inline keyword 사용 inline int Cat::GetWeight( ) return itsweight; 방법 2: 정의를클래스안에포함 class Cat public: int GetWeight( ) return itsweight; void SetWeight( int a ); returnint itsweight; 27
기타 다른클래스를멤버자료로가지는클래스 list 6.8 / list 6.9 구조체 (struct) 멤버가기본적으로범용 cf) class 는멤버가기본적으로전용 28