• 검색 결과가 없습니다.

Day 7(강의):

N/A
N/A
Protected

Academic year: 2022

Share "Day 7(강의):"

Copied!
18
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

Day 7(강의):

보충 문법

멤버 함수의 내부 구현

protected 멤버와 디폴트 생성자 함수(default constructor) 3가지 형태의 상속

객체 포인터 변수 파일 입출력

(2)

보충 문법

 멤버 함수의 내부 구현

외부구현이 일반적(프로그램의 판독성 증대)

내부 구현된 멤버 함수 -> inline 함수로 처리(5줄 이상인 경우 메모리 낭비)

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

#include "pclaf.h“

int oldx=0;

int oldy=0;

int newx=0;

int newy=0;

class penApplication : public application { public:

penApplication(char * apName) : application(apName) { }

penApplication(char * apName, int width, int height, int x, int y) : application(apName, width, height, x, y)

{ }

void mouseDown(int x, int y) {

newx = x;

newy = y;

update();

}

void paint() {

line(oldx, oldy, newx, newy);

oldx = newx;

oldy = newy;

} };

(3)

 protected 멤버와 디폴트 생성자 함수(default constructor)

0102 0304 0506 07 0809 1011 1213 1415 16 1718 1920 21 2223 2425

class Base { protected:

int x;

private:

int y;

};

class Derived : public Base { public:

void setX(int);

private:

int z;

};

void Derived::setX(int a)

{x = a; // x는 Base의 protected 멤버, Derived에서 직접접근 OK!

}

int main(void)

{Derived derivedObject;

derivedObject.setX(3);

return 0;

}

(4)

디폴트 생성자 함수

파라미터도 없고 아무런 기능도 하지 않는 생성자 함수 -> 컴파일러에 의해 자동 삽입

프로그래머가 정의해 놓은 생성자 함수가 하나라도 존재 -> 디폴트 생성자 함수의 자동 삽입 없음

타일 0102

0304 0506

class Base { protected:

int x;

private:

int y;

};

0102 0304 0506 07 08

class Base { public:

Base() {}

protected:

int x;

private:

int y;

};

(5)

protected 멤버

외부에서 보면 private로 보이고 상속 관계에서 보면 public으로 보임

private 멤버와 같다. 다만, private 멤버와 달리 상속 관계에 놓여 있는 경우에 접 근을 허용

Derived derivedObject;

derivedObject.setX(3);

(6)

 3가지 형태의 상속

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21

class Base { public:

int x;

int getZ() { return z; } protected:

int w;

private:

int z;

};

class Derived : public Base { public:

int y;

void setW (int a) { w = a; } };

int main(void) {

Derived derivedObject;

return 0;

}

(7)

Base 클래스의 멤버는 Derived 클래스로 상속되는 과정에서 접근권한이 변경 public 상속

가장 일반적인 상속 형태

접근불가 가시성: private 가시성보다 강한 가시성

멤버 Derived에서의

가시성 비고

x public Base의 public 멤버 변수는 Derived에서 public

getZ public Base의 public 멤버 함수는 Derived에서 public

w protected Base의 protected 멤버는 Derived의 protected

z 접근불가 Base의 private 멤버, 상속되나 Derived에서 접근불가

y public Derived에 추가

setW public Derived에 추가

(8)

protected 상속

private 상속

멤버 Derived에서의

가시성

비고

x protected Base의 public 멤버 변수는 Derived에서 protected

getZ protected Base의 public 멤버 함수는 Derived에서 protected

w protected Base의 protected 멤버는 Derived의 protected

z 접근불가 Base의 private 멤버, 상속되나 Derived에서 접근불가

y public Derived에 추가

setW public Derived에 추가

멤버 Derived에서의

가시성 비고

x private Base의 public 멤버 변수는 Derived에서 private

getZ private Base의 public 멤버 함수는 Derived에서 private

w private Base의 protected 멤버는 Derived의 private

z 접근불가 Base의 private 멤버, 상속되나 Derived에서 접근불가

y public Derived에 추가

setW public Derived에 추가

(9)

 객체 포인터 변수

포인터 변수 int n=10;

int * p=&n;

객체 포인터 변수

penApplication theApp(“PENS”);

penApplication * appPtr=&theApp;

부모 클래스의 포인터 변수는 자식 클래스의 객체도 포인팅 penApplication theApp(“PENS);

application * appPtr=&theApp;

n xx 10 xx

p

application

penApplication application

mouseDown paint run

virtual okBox

mouseDown paint pen-

Application timer- Interval

pen- Application

mouseDown paint pen-

Application pen-

Application theAPP

xx

appPtr xx

(10)

객체 포인터의 권한

xxx 클래스의 객체 포인터는 포인팅하는 대상이 어떤 객체이건 xxx 클래스 내에 선언된 멤버와 xxx 클래스가 상속받은 멤버에만 접근이 가능

01 02 03 04 0506 07 08 09 1011 12 13 14 1516 17 18 19 2021 22 23 24 25 2627 28 29 30

#include <iostream>

using std::cout;

using std::endl;

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 partStd;

person * p = &partStd;

p->sleep(); // OK1

// p->study(); // 컴파일 error!

// p->work(); // 컴파일 error!

return 0;

}

(11)

 파일 입출력

0102 0304 05 0607 0809 10 1112 1314 15 1617 1819 20 2122 2324 25 2627 2829

#include <fstream>

#include <iostream>

using std::ifstream;

using std::ofstream;

using std::cout;

using std::endl;

int main(void) {

ifstream inStream;

ofstream outStream;

inStream.open("infile.txt");

if(inStream.fail())

cout << "input file opening failed!" << endl;

outStream.open("outfile.txt");

if(outStream.fail())

cout << "output file opening failed!" << endl;

int firstNum, secondNum, thirdNum;

inStream >> firstNum >> secondNum >> thirdNum;

outStream << (firstNum + secondNum + thirdNum)

<< (firstNum + secondNum + thirdNum) / 3 << endl;

inStream.close();

outStream.close();

return 0;

}

(12)

ifstream(input file stream) 클래스와 ofstream(output file stream) 클래스 ifstream 클래스 -> 파일로부터 입력 전담 소프트웨어 부품원판 ofstream 클래스 -> 파일로 출력 전담 소프트웨어 부품원판 std 이름공간에 존재

ifstream 클래스로부터 입력 전담 소프트웨어 부품인 inStream 객체 생성 ofstream 클래스로부터 출력 전담 소프트웨어 부품인 outStream 객체 생성 open 멤버 함수

fail 멤버 함수

>>와 << 연산(cin, cout과 사용법과 유사) close 멤버 함수

using std::ios

outStream.open(“outfile.txt”, ios::app);

ios::app 모드: 첨부 모드 using std::ios:

ifstream 클래스의 생성자 함수 사용 ifstream inStream;

inStream.open(“infile.txt”); ifstream inStream(“infile.txt”);

(13)

ifstream(input file stream) 클래스의 eof 멤버 함수 파일의 끝에 도달하면 true 값을 리턴

0102 03 0405 0607 08 0910 1112 13

int xc, yc, rc;

inStream.open("circle.txt");

outStream.open("circle2.txt");

inStream >> xc >> yc >> rc;

while(! inStream.eof()) { circle(xc, yc, rc);

outStream << xc << " " << yc << " " << rc << endl;

inStream >> xc >> yc >> rc;

}

inStream.close();

outStream.close();

(14)

Day 8(실습):

응용 과제 1번 실습

응용 과제 3번 실습

(15)

응용 과제 1번 실습

 실습 과제

(16)

코드

pclaf의 wostream 클래스: 윈도우에 출력 전담 부품 원판 wout 객체 멤버

01

02 wout << setpos(newx, newy);

wout << “hello world!”;

window

window line

virtual run virtual

mouseDown virtual paint clientX clientY wout- - -

line

window line

virtual run virtual

mouseDown virtual paint clientX clientY wout

- - -

line theWindow

wostream wostream

width width

<< <<

<<

fieldWidth floatPrecision winPtr

- - -

wostream width width

<< <<

<<

fieldWidth floatPrecision winPtr

- - -

(17)

응용 과제 3번 실습

 실습 과제

(18)

코드

01 0203 0405 06 0708 0910 11 1213 1415 16 1718 1920 2122 2324 25 2627

class myChild : public childWindow { public:

myChild(char *,int,int,int,int);

void mouseDown(int,int);

void paint();

};

myChild::myChild(char * cName, int width, int height, int x, int y) : childWindow(cName, width, height, x, y)

{

// no action }

int childX = 50;

int childY = 50;

void myChild::mouseDown(int x, int y) {childX = x;

childY = y;

update();

}

void myChild::paint() {

circle(childX, childY, 30);

}

참조

관련 문서

메소드의 인자 타입과 개수가 달라야 함 메소드 인자 타입과 개수가 동일해야 함 정적 바인딩 , 즉 컴파일 시에 중복된 메. 소드

 인자로 들어온 콜백 함수를 계속해서 호출하여 배열의 요소들을 왼쪽에서 오른쪽 방향으로 나아가면서 하나의 값으로 줄임. – 콜백 함수: 어떻게 배열의 원소들을

• 아키텍처 설계(Architecture design) – 예비 설계 또는 상위 수준 설계?. – 소프트웨어 시스템의

– 이름을 입력 받기 위해 Control 들을 화면에 배치하여 Layout 구성 – 작성한 Dialog를 Control 하기 위한 CDialog의 파생 Class 생성 및 연결. ◈ Edit

문자열 name과 같은 이름을 가진 인수 값을 배열 형태로 가져 옴 checkbox, multiple list 등에

천체투영관 시스템 구조 - 소프트웨어, SkyExplorer. 천체투영관 시스템 구조

• 어떤 객체가 다른 객체에게 어떤 일을 수행하도록 명령하기 위해서 우리는 그 객체에 메시지를

– 답장이 오면 (onreadystatechange 이벤트 핸들러 이용) –