330 likes | 534 Views
Paradigm of the object oriented programming, class, object. Paradigm. paradigm (Collins English Dictionary) — a very general conception of the nature of scientific endeavour within which a given enquiry is undertaken programming paradigms structural programming object oriented programming.
E N D
Paradigm paradigm (Collins English Dictionary) — a very general conception of the nature of scientific endeavour within which a given enquiry is undertaken • programming paradigms • structural programming • object oriented programming
Paradigm of the object oriented programming object oriented programming — a paradigm of programming by use of objects, interpreting a programming problem as a set of objects and as a relations among objects.
Object • Common understanding of „object” • „Object” in programming • It represents (in the program) real-life or abstract objects (in the common understanding of object) • It’s a generalized variable (structure) • It is being defined and used according to syntactic and semantic rules of the programming language
Object - a generalized variable (structure) • Structure • set of data elements, usually of different types • Generalized • object = data + methods for manipulating data
Many objects • usually many objects share specific set of attributes (features, qualities), we do need to define the set once, and to use it several times class — a group of things, people, etc., possessing some quality or qualities in common; • We do need a class for all the similar objects
Class in programming • Class in programming—generalized type defined by the user (of the language, i.e. by programmer) • it is used for defining objects (generalized variables) • providers programmer with lots of new and exciting possibilities (to be discussed later :-) • The class should clearly represent specific concept, idea, or real-life being, that is not yet described by approppriate type
Why object oriented programming? • next stage in the SE evolution: • structural • procedural • modular • tool for implementong OO projects (there is OO analysis, OO design) • languages that support OO programming C++, Java, … • languages that permit OO programming all (programming languages)
A structural example – person struct person { int age; char FName[20], LName[30]; }; void person_input(person *o); void person_set(person *o, int age, char *pFN, char *pLN); void person_output(person *o); • no access controll over data members • it’s a pogrammer responsibility to use proper functions for manipulating persons
An OO example – person class person { int age;// class members – class variables char FName[20], LName[30]; public: void input();// class members – class methods void set(int age, char *pFN, char *pLN); void output(); }; // declaration ends with „ ; ” • data and methods together • default for class: no external access to class variables
Class members access rules • private: // private members // accessible for methods of this class, // and for methods and functios declared as friends // all „class” members are private by default • public: // public members // accessible from outside the class // all „struct” members are public by default • protected: // protected members // access like to private members // but the derived class also may access this members
Class members access rules class person { int age;// private char FName[20];// private public: void input(); // public private: char LName[30];// private public: void set(int, char *, char *);// public void output();// public };
Hermetization, Encapsulation Gathering all data and methods in single class(encapsulation) allows the programmer to decide and restrict access to class members (hermetization). • All the class members are private by default • OOOP ;-) — orthodox OO programming: all class data members are private, from outside we access them through class methods only.
Object - a generalized structure • Declaring: class personme, You; personboss;// while declaring/defining obiect // „class”, „struct” or „union” may be skipped • Using boss.input(); boss.output();
Operators for accessingclass members • dot „ . ” object.variable; //just like in C structure object.method(); // encapsulation • scope operator „ :: „ class::variable; // sizeof, static variables class::method(); // defining methods, static methods • Scope definition ( object. and class::) usually may be skipped • class method accessing members of its own class • declaring/defining methods inside class declatation
How to define methods? • Inside class declatation class person { … void input() { cin>>age>>FName>>LName; } // semicolon not required … }; • above is inline by default
How to define methods? • outside the class use the scope operator • by default, such method is not inline void person::set(int age, char *pFN, char *pLN) { osoba::age=age; // scope operator since local argument „age” visible, not person::age strcpy(FName, pFN); strcpy(LName, pLN); } • want the method to be inline? inline void person::output() { cout<<„age: "<<age<<" first name: "<<FName<<" last: "<<LName<<"\n"; }
How to define methods? • when creating libraries we place class declaration and definitions of inline methods in the header file (*.h), other methods in *.cpp. • Methods just like functions may (and often are) overloaded, and may have default arguments void set(int, char *pFN="Jan", char *pLN="Kowalski"); void set(const person & example); boss.set(You); boss.set(50, „Osama”, „bin Laden”); boss.set(50, „Osama”); boss.set(50); // boss.set(); ERROR!
How to define methods? • methods and variables defined in class are visible from the beginning of the class definition. They are also visible in class methods declared in the class, but defined outside class A { public: void input() { cin>>i;// declaration of „i” is below output(); // as above } void output(); int i; };
How to define methods? • To remind: from outside class methods, the class members must be specified either by class name, or by object name int test() { A a; int j=sizeof(A::i); void (A::*p)()=&A::input; a.i=3;// i is public in A }
int fun(); int i; class C { int i; public: void test(); int fun(); }; void C::test() { i++; // C::i++ ::i++; // global i fun(); // C::fun() ::fun(); // global fun() } The scope operator as a not OO C++ extension
Example Task • declare the point class, that represents 2D points • no public data members • public methods: input, output, move(by a vector specified by a pair of coordinates), distance (between this point and the method argument - a reference to point), methodscoordX i coordYthat return appropriate coordinate of the point
Example class point { double x, y; public: void input(); void output(); void move(double dx, double dy); double distance(const punkt &p); double coordX();// a.k.a. accessors double coordY(); };
Example Task • define inline methods • input() • output() • move() • distance()
Example class point { double x, y; public: void input() {cin>>x>>y; }; void output() {cout<<x<<y; }; … }; inline void point::move(double dx, double dy) { x+=dx; y+=dy; }
Example inline double point::distance(point &p) { return sqrt( (x-p.x)*(x-p.x) + (y-p.y)*(y-p.y) ); } • remark:„private” is defined for the whole class, not for the single object only.
Classes and Abstract Data Types • classes are perfectly suitable for implementing Abstract Data Types • classes are Abstract Data Types • We define class interface – when we use methods allowed for the specific class, we do not care how the methods are implemented. Thanks to hermetization implementation details are separated from the class interface. • Examples: stack, queue, point, segment
Example Task • declare the point class, that represents 2D segments • no public data members • public methods: input, output, move(by a vector specified by a pair of coordinates), length (length of a segment).
class segment { point p1, p2; public: void input() { p1.input(); p2.input(); } void output() { p1.output(); p2.output(); } void move(double dx, double dy) { p1. move(dx, dy); p2. move(dx, dy); } double length() { return p1.distance(p2); } }; Example
Interesting fact: nested class declarations • class declarationmay be nested inside other class declaration • nested class is not visible in the global scope
class X { class M1 { int m; }; public: class M2 { int m; }; }; void f() { M1 m1; // error // not in global scope X::M1 xm1;// error //M1 is a private class in X X::M2 xm2;// ok. } Interesting fact: nested class declarations
Interesting fact: nested class declarations • X has neither member variables nor member methods, it contains only types (still, we may create objects of this class).class with nested class and class variables: class X_d { public: class M2 { int m; }; M2 m2; // here }; • generally nested class declarations are to be avoided and actually are used rarely (except very small classes) — nested class declarations are barely usefull and barely visible.