Day 7(강의):
보충 문법
멤버 함수의 내부 구현
protected 멤버와 디폴트 생성자 함수(default constructor) 3가지 형태의 상속
객체 포인터 변수 파일 입출력
보충 문법
멤버 함수의 내부 구현
외부구현이 일반적(프로그램의 판독성 증대)
내부 구현된 멤버 함수 -> 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;
} };
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;
}
디폴트 생성자 함수
파라미터도 없고 아무런 기능도 하지 않는 생성자 함수 -> 컴파일러에 의해 자동 삽입
프로그래머가 정의해 놓은 생성자 함수가 하나라도 존재 -> 디폴트 생성자 함수의 자동 삽입 없음
타일 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;
};
protected 멤버
외부에서 보면 private로 보이고 상속 관계에서 보면 public으로 보임
private 멤버와 같다. 다만, private 멤버와 달리 상속 관계에 놓여 있는 경우에 접 근을 허용
Derived derivedObject;
derivedObject.setX(3);
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;
}
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에 추가
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에 추가
객체 포인터 변수
포인터 변수 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
객체 포인터의 권한
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;
}
파일 입출력
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;
}
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”);
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();
Day 8(실습):
응용 과제 1번 실습
응용 과제 3번 실습
응용 과제 1번 실습
실습 과제
코드
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
- - -
응용 과제 3번 실습
실습 과제
코드
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);
}