Slaid 2.pptx
- Количество слайдов: 21
Object-oriented paradigm Lecture 2
Class structure Class as the program construction, usually, consists of the denotation, the interface and a body. The denotation is the identifier. The interface describes resources given by a class. The body consists of the constructions, which describe implementation of resources presented by a class.
Class content. 1 As, the class, as a rule, is an abstract type of data, the class body should contain the description of following components: - the values given by an abstract type (class); - the subroutines providing processing of values of an abstract type of data (class).
Class content. 2 The description of values defines a value range, handled by a class (they are named as data-members (data members)) The description of subroutines defines a of operations for values defined in a class (they are named as functions - members (member function) or methods)
Access to class members In a class body it is possible to distinguish parts which are flagged with special public keywords (opened), private (closed), protected (protected). And appropriate parts of a class are considered as opened, closed and protected accordingly.
С++ class Point { public: void set_x (short x) { _x = x; } void set_y (short y) { _x = y; } short get_x() { return _x; }
short get_y() { return _y; } void Show() { cout << get_x() << ", " << get_y() << "n"; } private: short _x, _y; };
class Plane_Data {public: char * get_id (); void set_id (char* s); int get_speed (); void set_speed (int i); int get_altitude (); private: char id [80]; int speed; int altitude; }; Class inteface
char* Plane_Data: : get_id () { return id; } void Plane_Data: : set_id (char*s) { } int Plane_Data: : get_speed () { return speed; } void Plane_Data: : set_speed (int i) { } int Plane_Data: : get_altitude () { return altitude; } Class realization Operation realization
С# class Point { public void set_x(short x) { _x = x; } public void set_y(short y) { _y = y; } public short get_x() { return _x; }
public short get_y() { return _y; } public void Show() { Console. Write. Line("{0}, {1}", get_x(), get_y()); } private short _x; private short _y; };
class Plane_Data { public char[] get_id() { return id; } public void set_id(char[] s) { } public int get_speed() { return speed; } public void set_speed(int i) { } public int get_altitude() { return altitude; } private char[] id; private int speed; private int altitude; };
The object - is a program construction which is created on the basis of a class as its instance and contains all components of a class. The object really exists in the program and can be used as variable. On the basis of one class some objects can be created
Processing and usage of the class description in C++. Separate compilation Fig. 1
Example of separate compilation using. 1 Time Class interface definition (head file time. h) сlass Time { public: void set_Time (int, int); void display_Time (); private: int hour; int min; int sec; };
Example of separate compilation using. 2 Time class resources realization definition (source file Time. cpp) # include “Time. h” void Time : : set_Time (int h, int m, int s) { … } void Time : : display_Time () { … }
Example of separate compilation using. 3 program, which uses objects – Time class instances (file Time. cpp) # include “Time. h” void main () { Time my_Time; my_Time. set = Time (13, 30, 5); my_Time. Display_Time ();
File structure of С++ class Fig. 2
Disadvantage of information hiding implementation in С++ hand-held creation of a heading file, absence of resources of its automated upgrade at the change of implementation demanding appropriate modification of a h-file.
Information hiding implementation in С# Fig. 3
Advantage of С # program structure the metadata is created not by the programmer, but by the environment, the environment automatically refreshes them at every source implementation recompilation, the metadata and implementation class are placed together in one file, there are special tools of their reading and usage by the programmer and software.
Slaid 2.pptx