class My Class private int a float b
Объявление класса class My. Class { private: int a; float b; 9
Объявление класса class My. Class { private: int a; float b; public: void set_a (int x) { a = x; } void show_a ( ) { cout<<“Поле a равно ”<< a << endl; } } [список объектов]; 10
Объявление класса class My. Class { private: int a; float b; int main() { My. Class obj 1, obj 2; My. Class *pobj; public: void set_a (int x) { a = x; } void show_a ( ) { cout<<“Поле a равно ”<< a << endl; } } } 11
Объявление класса class My. Class { private: int a; float b; int main() { My. Class obj 1, obj 2; My. Class *pobj; obj 1. set_a(4); obj 2. set_a(15); public: void set_a (int x) { a = x; } void show_a ( ) { cout<<“Поле a равно ”<< a << endl; } } obj 1. show_a (); obj 2. show_a (); } 12
Объявление класса class My. Class { private: int a; float b; public: void set_a (int x) { a = x; } void show_a ( ) { cout<<“Поле a равно ”<< a << endl; } } int main() { My. Class obj 1, obj 2; My. Class *pobj; obj 1. set_a(4); obj 2. set_a(15); obj 1. show_a (); obj 2. show_a (); pobj = new My. Class; pobj set_a(1); 13
My. Class obj 1, obj 2; 14
My. Class obj 1, obj 2; obj 1 obj 2 a a b b set_a Адрес show_a Адрес 15
My. Class obj 1, obj 2; obj 1 obj 2 a a b b set_a Адрес show_a Адрес My. Class *pobj; 16
My. Class obj 1, obj 2; obj 1 obj 2 a a b b set_a Адрес show_a Адрес My. Class *pobj; pobj 17
My. Class obj 1, obj 2; obj 1 obj 2 a a b b set_a Адрес show_a Адрес My. Class *pobj; pobj = new My. Class; 18
My. Class obj 1, obj 2; obj 1 obj 2 a a b b set_a Адрес show_a Адрес My. Class *pobj; pobj a pobj = new My. Class; b set_a Адрес show_a Адрес 19
My. Class obj 1, obj 2; obj 1 obj 2 a a b b set_a Адрес show_a Адрес My. Class *pobj; pobj a pobj = new My. Class; b set_a Адрес show_a Адрес 20
Объект «Точка» class Point { public : int x; int y; }; int main() { Point p 1, p 2; p 1. x=1; p 1. y=1; p 2. x=15; p 2. y=24; … } struct Point { int x; int y; }; int main() { struct Point p 1, p 2; p 1. x=1; p 1. y=1; p 2. x=15; p 2. y=24; … } 22
Объект «Точка» class Point { private : int x; int y; int main() { } public: void Init () { x = 0; y=0; } void set_x (int px) { x = px; } void set_y (int py) { y = py; } int get_x () { return x; } int get_y () { return y; } }; 23
Объект «Точка» class Point { private : int x; int y; public: void Init () { x = 0; y=0; } void set_x (int px) { x = px; } void set_y (int py) { y = py; } int get_x () { return x; } int get_y () { return y; } }; int main() { Point p 1; p 1. x=1; p 1. y=1; … } 24
Объект «Точка» class Point { private : int x; int y; public: void Init () { x = 0; y=0; } void set_x (int px) { x = px; } void set_y (int py) { y = py; } int get_x () { return x; } int get_y () { return y; } }; int main() { Point p 1; p 1. Init(); p 1. set_x(15); cout<<p 1. get_x(); … } 25
Объект «Точка» . Конструктор class Point { private : int x; int y; public: void Init () { x = 0; y=0; } void set_x (int px) { x = px; } void set_y (int py) { y = py; } int get_x () { return x; } int get_y () { return y; } }; class Point { private : int x; int y; public: Point () { x = 0; y=0; } void set_x (int px) { x = px; } void set_y (int py) { y = py; } int get_x () { return x; } int get_y () { return y; } }; 27
Объект «Точка» . Описание методов class Point { private : int x; int y; public: Point (); void set_x (int px); void set_y (int py) ; int get_x (); int get_y (); }; Point: : Point () { x = 0; y=0; } void Point: : set_x (int px) { x = px; } void Point: : set_y (int py) { y = py; } int Point: : get_x () { return x; } int Point: : get_y () { return y; } 29
Объект «Точка» . Наследование class Point { private: int x; int y; public: Point (); void set (int px, int py); … }; Point: : Point () { x = 1; y=1; } void Point: : set (int px, int py); { x = px; y=py; } 30
Объект «Точка» . Наследование class Point { private: int x; int y; Point: : Point () { x = 1; y=1; } void Point: : set (int px, int py); { x = px; y=py; } public: Point (); void set (int px, int py); … }; class Point. Color: public Point { private: int color; public: void set (int px, int py, int pcolor); … }; 31
Объект «Точка» . Наследование class Point { private: int x; int y; public: Point (); void set (int px, int py); … }; class Point. Color: public Point { private: int color; public: void set (int px, int py, int pcolor); … }; Point: : Point () { x = 1; y=1; } void Point: : set (int px, int py); { x = px; y=py; } void Point. Color: : set (int px, int py, int pcolor); { x = px; y=py; color=pcolor; } 32
Объект «Точка» . Наследование class Point { protected: int x; int y; public: Point (); void set (int px, int py); … }; class Point. Color: public Point { private: int color; public: void set (int px, int py, int pcolor); … }; Point: : Point () { x = 1; y=1; } void Point: : set (int px, int py); { x = px; y=py; } void Point. Color: : set (int px, int py, int pcolor); { x = px; y=py; color=pcolor; } 33
Объект «Точка» . Наследование class Point { protected: int x; int y; public: Point (); void set (int px, int py); … }; class Point. Color: public Point { private: int color; public: void set (int px, int py, int pcolor); … }; Point: : Point () { x = 1; y=1; } void Point: : set (int px, int py); { x = px; y=py; } void Point. Color: : set (int px, int py, int pcolor); { Point: : set (px, py); color=pcolor; } 34
Иерархия классов. Пример № 1 class A { protected: int x; int y; main(void) { } 39
Иерархия классов. Пример № 1 class A { protected: int x; int y; public: int a; int b; main(void) { } 40
Иерархия классов. Пример № 1 class A { protected: int x; int y; public: int a; int b; int f 1(int x, int y) { return (x-y); } }; 41
Иерархия классов. Пример № 1 class A { protected: int x; int y; public: int a; int b; int f 1(int x, int y) { return (x-y); } }; class B: public A { main(void) { } public: int f 2(int x) { return (x+ a); } }; 42
Иерархия классов. Пример № 1 class A { protected: int x; int y; public: int a; int b; int f 1(int x, int y) { return (x-y); } }; class B: public A { public: int f 2(int x) { return (x+ a); } }; main(void) { A min; B max; min. a=10; min. b=20; int x 1=min. f 1(min. a, min. b); max. a=15; int x 2=max. f 2(10); cout<<“x 1=“<<x 1<<“ x 2=”<<x 2; … } 43
min max x x y y 15 a 10 a b 20 b f 1 Адрес f 1 f 2 Адрес f 2 min. a=10; min. b=20; max. a=15; 45
Иерархия классов. Пример № 1 class A { protected: int x; int y; public: int a; int b; int f 1(int x, int y) { return (x-y); } }; class B: public A { public: int f 2(int x) { A: : x=15; return (x+ A: : x); } }; main(void) { A min; B max; min. a=10; min. b=20; int x 1=min. f 1(min. a, min. b); int x 2=max. f 2(10); cout<<“x 1=“<<x 1<<“ x 2=”<<x 2; … } : : - оператор разрешения области видимости 46
- Slides: 48