class Circle int x y int radius class

  • Slides: 59
Download presentation

이번 장에서 만들어 볼 프로그램 class Circle { int x, y; int radius; .

이번 장에서 만들어 볼 프로그램 class Circle { int x, y; int radius; . . . } class Rect { int x, y; int width, height; . . . } 중복

// 한 점을 표현하는 클래스 Point 선언 class Point { int x, y; //(x,

// 한 점을 표현하는 클래스 Point 선언 class Point { int x, y; //(x, y) 좌표값 public: void set(int x, int y) 8 { this->x = x; this->y = y; } void show. Point() { cout << "(" << x << ", " << y << ")" << endl; class Color. Point : public Point { } string color; // 점의 색 표현 }; public: void set. Color(string color) { this->color = color; } void show. Color. Point(); }; 예제 void Color. Point: : show. Color. Point() { cout << color << ": "; show. Point(); } int main() { Point p; Color. Point cp; cp. set(3, 4); cp. set. Color("Red"); cp. show. Color. Point(); }

자식 클래스의 객체 구성 10 class Point { int x, y; public: void set(int

자식 클래스의 객체 구성 10 class Point { int x, y; public: void set(int x, int y); void show. Point(); }; class Color. Point : public Point { string color; public: void set. Color(string color); void show. Color. Point(); }; Color. Point cp; Point p; int x int y void set() {. . . } void show. Point() {. . . } 자식클래스의 객체는 기본 클래스의 멤버 포함 기본클래스 멤버 void show. Point() {. . . } string color void set. Color () {. . . } void show. Color. Point() {. . . } 자식클래스 멤버

자식 클래스에서 기본 클래스 멤 버 접근 Color. Point cp 객체 11 x y

자식 클래스에서 기본 클래스 멤 버 접근 Color. Point cp 객체 11 x y 자식클래스에서 기본 클래스 멤버 호출 void set(int x, int y) { this->x= x; this->y=y; } void show. Point() { cout << x << y; } color void set. Color ( ) {. . . } void show. Color. Point() { cout << color << ": "; show. Point(); } Point 멤버 Color. Point 멤버

12 외부에서 자식 클래스 객체에 대 한 접근 3 x y 4 void set(int

12 외부에서 자식 클래스 객체에 대 한 접근 3 x y 4 void set(int x, int y) { this->x= x; this->y=y; } void show. Point() { cout << x << y; } color Color. Point cp; cp. set(3, 4); cp. set. Color("Red"); cp. show. Color. Point(); “Red” void set. Color (string color) { this->color = color; } void show. Color. Point() { cout << color << ": "; show. Point(); } main() Color. Point cp 객체

예제 class Car { int speed; }; class Sports. Car : public Car {

예제 class Car { int speed; }; class Sports. Car : public Car { bool turbo; };

class Car { int speed; // 속도 예제 public: void set. Speed(int s) {

class Car { int speed; // 속도 예제 public: void set. Speed(int s) { speed = s; } int get. Speed() { return speed; } }; // Car 클래스를 상속받아서 다음과 같이 Sports. Car 클래스를 작성한다. class Sports. Car : public Car { int main() bool turbo; { public: Sports. Car c; void set. Turbo(bool new. Value) { turbo = new. Value; c. set. Speed(60); } c. set. Turbo(true); bool get. Turbo() { c. set. Speed(100); return turbo; c. set. Turbo(false); } return 0; }; }

class Shape { int x, y; public: void set. X(int xval) { x =

class Shape { int x, y; public: void set. X(int xval) { x = xval; } void set. Y(int yval) { y = yval; } }; 예제 class Rectangle : public Shape { int width, height; public: void set. Width(int w) { width = w; } void set. Height(int h) { height = h; } int get. Area() { return (width * height); } }; int main() { Rectangle r; r. set. Width(5); r. set. Height(6); cout << "사각형의 면적: " << r. get. Area() << endl; return 0; }

class Shape { int x, y; public: Shape() { ~Shape() { }; 예제 class

class Shape { int x, y; public: Shape() { ~Shape() { }; 예제 class Rectangle : public Shape { int width, height; public: Rectangle() { } ~Rectangle() { } }; int main() { Rectangle r; return 0; } cout << "Shape 생성자() " << endl; } cout << "Shape 소멸자() " << endl; } cout << "Rectangle 생성자()" << endl; cout << "Rectangle 소멸자()" << endl; 실행 결과 는?

부모 클래스 생성자 호출 다음에 멤버 초기화 리스트를 작성할 수 있음 Rectangle(int x=0, int

부모 클래스 생성자 호출 다음에 멤버 초기화 리스트를 작성할 수 있음 Rectangle(int x=0, int y=0, int w=0, int h=0) : Shape(x, y) { width = w; height = h; } Rectangle(int x=0, int y=0, int w=0, int h=0) : Shape(x, y), width(w), height(h) {}

class Shape { int x, y; public: Shape() { cout << "Shape 생성자() "

class Shape { int x, y; public: Shape() { cout << "Shape 생성자() " << endl; } Shape(int xloc, int yloc) : x{xloc}, y{yloc} { cout << "Shape 생성자(xloc, yloc) " << endl; } ~Shape() { cout << "Shape 소멸자() " << endl; } }; class Rectangle : public Shape { 예제 int width, height; public: Rectangle: : Rectangle(int x, int y, int w, int h) : Shape(x, y) { width = w; height = h; cout << "Rectangle 생성자(x, y, w, h)" << endl; } ~Rectangle() { cout << "Rectangle 소멸자()" << endl; } };

class Shape { int x, y; public: 예제 Shape() { cout << "Shape 생성자()

class Shape { int x, y; public: 예제 Shape() { cout << "Shape 생성자() " << endl; } Shape(int xloc, int yloc) : x{xloc}, y{yloc} { cout << "Shape 생성자(xloc, yloc) " << endl; } ~Shape() { cout << "Shape 소멸자() " << endl; } class Rectangle : public Shape { }; int width, height; public: Rectangle: : Rectangle(int x, int y, int w, int h) : Shape(x, y) { width = w; height = h; cout << "Rectangle 생성자(x, y, w, h)" << endl; } ~Rectangle() { cout << "Rectangle 소멸자()" << endl; } }; 실행 결과는 ? int main() { Rectangle r(0, 0, 100); return 0; }

class Rect { protected: int x, y, width, height; public: Rect(int x, int y,

class Rect { protected: int x, y, width, height; public: Rect(int x, int y, int w, int h) : x(x), y(y), width(h), height(h) { } void draw() { HDC hdc = Get. Window. DC(Get. Foreground. Window()); Rectangle(hdc, x, y, x + width, y + height); } }; class Colored. Rect : Rect { int red, green, blue; public: Colored. Rect(int x, int y, int w, int h, int r, int g, int b) : Rect(x, y, h, w), red(r), green(g), blue(b) { } void draw() { HDC hdc = Get. Window. DC(Get. Foreground. Window()); Select. Object(hdc, Get. Stock. Object(DC_BRUSH)); Set. DCBrush. Color(hdc, RGB(red, green, blue)); Rectangle(hdc, x, y, x + width, y + height); } }; 예제

예제 int main() { Rect r 1(100, 80, 80); Colored. Rect r 2(200, 80,

예제 int main() { Rect r 1(100, 80, 80); Colored. Rect r 2(200, 80, 255, 0, 0); r 1. draw(); r 2. draw(); return 0; }

멤버의 접근 지정에 따른 접근성 42 외부 함수 기본 클래스 void function() { class

멤버의 접근 지정에 따른 접근성 42 외부 함수 기본 클래스 void function() { class A { private: private 멤버 protected: protected 멤버 public: public 멤버 }; } class B : public A { }; 자식 클래스 다른 클래스 class C { };

class Person { string name; protected: string address; }; 예제 class Student : public

class Person { string name; protected: string address; }; 예제 class Student : public Person { public: void set. Address(string add) { address = add; } string get. Address() { return address; } }; int main() { Student obj; obj. set. Address("서울시 종로구 1번지"); cout << obj. get. Address() << endl; return 0; }

#include <iostream> #include <string> using namespace std; 예제 class Animal { public: void speak()

#include <iostream> #include <string> using namespace std; 예제 class Animal { public: void speak() { cout << "동물이 소리를 내고 있음" << endl; } }; class Dog : public Animal { public: void speak() { cout << "멍멍!" << endl; } }; int main() { Dog obj; obj. speak(); return 0; }

예제 #include <iostream> #include <string> using namespace std; class Parent. Class { public: void

예제 #include <iostream> #include <string> using namespace std; class Parent. Class { public: void print() { cout << "부모 클래스의 print() 멤버 함수" << endl; } }; class Child. Class : public Parent. Class { int data; public: void print() { //멤버 함수 재정의 Parent. Class: : print(); cout << "자식 클래스의 print() 멤버 함수 " << endl; } };

51 상속 시 접근 지정에 따른 멤버의 접 근 지정 속성 변화 class Base

51 상속 시 접근 지정에 따른 멤버의 접 근 지정 속성 변화 class Base { private: int a; protected: int b; public: int c; }; public 상 속 상속 후 Derived class Derived : public Base {. . // Derived 멤버 }; protected: int b; public: int c; . . // Derived 멤버 protected 상속 private 상 속 class Derived : protected Base {. . // Derived 멤버 }; class Derived : private Base {. . // Derived 멤버 }; Base 영역 Derived 영역 상속 후 Derived protected: int b; int c; Base 영역 . . // Derived 멤버 Derived 영역 상속 후 Derived private: int b; int c; . . // Derived 멤버 Base 영역 Derived 영역

에제 #include <iostream> using namespace std; class Base { public: int x; protected: int

에제 #include <iostream> using namespace std; class Base { public: int x; protected: int y; private: int z; }; class Derived : private Base{ // x는 자식 클래스에서 사용가능하지만 private로 지정된다. // y는 자식 클래스에서 사용가능하지만 private로 지정된다. // z는 자식 클래스에서도 사용할 수 없다. }; int main() { Derived obj; cout << obj. x; }

에제 class Sprite { int x, y; string image; public: Sprite(int x, int y,

에제 class Sprite { int x, y; string image; public: Sprite(int x, int y, string image) : x(x), y(y), image(image) { } void draw() { } int main() void move() { }; Alien a( 0, 100, "image 1. jpg" ); Player p(0, 100, "image 1. jpg"); return 0; class Alien : public Sprite { int speed; } public: Alien(int x, int y, string image) : Sprite(x, y, image) { } void move() { } }; class Player : public Sprite { string name; public: Player(int x, int y, string image) : Sprite(x, y, image) { } void move() { } };

class Passanger. Car { public: int seats; // 정원 void set_seats(int n) { seats

class Passanger. Car { public: int seats; // 정원 void set_seats(int n) { seats = n; } }; 에제 class Truck { public: int payload; // 적재 하중 void set_payload(int load) { payload = load; } }; class Pickup : public Passanger. Car, public Truck { public: int tow_capability; // 견인 능력 void set_tow(int capa) { tow_capability = capa; } int main() }; { Pickup my_car; my_car. set_seats(4); my_car. set_payload(10000); my_car. set_tow(30000); return 0; }

57 예제: Adder와 Subtractor를 다중 상 속받는 Calculator 작성 class Adder { protected: int

57 예제: Adder와 Subtractor를 다중 상 속받는 Calculator 작성 class Adder { protected: int add(int a, int b) { return a+b; } }; class Subtractor { protected: int minus(int a, int b) { return a-b; } }; // 다중 상속 class Calculator : public Adder, public Subtractor { public: int calc(char op, int a, int b); }; int Calculator: : calc(char op, int a, int b) { int res=0; switch(op) { case '+' : res = add(a, b); break; case '-' : res = minus(a, b); break; } return res; } int main() { Calculator hand. Calculator; cout << "2 + 4 = " << hand. Calculator. calc('+', 2, 4) << endl; cout << "100 - 8 = " << hand. Calculator. calc('-', 100, 8) << endl; }

다중 상속 문제점 상속되는 두 클래스에 동일한 멤버 변수가 존재할 경우 class Super. A

다중 상속 문제점 상속되는 두 클래스에 동일한 멤버 변수가 존재할 경우 class Super. A { public: int x; void sub() { cout<<“Super. A의 sub()”<<endl; } }; class Super. B { public: int x; void sub() { cout<<“Super. B의 sub()”<<endl; } }; class Sub: public Super. A, public Super. B { }; int main() { Sub obj; obj. x = 10; //? return 0; }

다중 상속 문제점 상속되는 두 클래스에 동일한 멤버 변수가 존재할 경우에 , 그 변수

다중 상속 문제점 상속되는 두 클래스에 동일한 멤버 변수가 존재할 경우에 , 그 변수 앞에 ‘: : ’를 이용하여 해당 클래스 이름을 명시해 야함 class Super. A { public: int x; void sub() { cout<<“Super. A의 sub()”<<endl; } }; class Super. B { public: int x; void sub() { cout<<“Super. B의 sub()”<<endl; } }; class Sub: public Super. A, public Super. B { }; int main() { Sub obj; obj. Super. A: : x = 10; return 0; }