1 / 44

Object Oriented Programming Language (OOP)

Object Oriented Programming Language (OOP). بسم الله الرحمن الرحيم. Presented by: Ustaz Mutaz Elradi Saad alla Saeed Faculty of Science &Technologies Nile Valley University Sudan , Atbara. Course Outline. OOP Concept OOP History Basic Concept Classes Types Encapsulation

Download Presentation

Object Oriented Programming Language (OOP)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Language (OOP) بسم الله الرحمن الرحيم Presented by: Ustaz Mutaz Elradi Saad alla Saeed Faculty of Science &Technologies Nile Valley University Sudan , Atbara

  2. Course Outline • OOP Concept • OOP History • Basic Concept • Classes Types • Encapsulation • Inheritance • Private Inheritance • Protected Inheritance • Public Inheritance

  3. Multiple Inheritance • Polymorphism • OOP With C ++ Language

  4. Explanation • A Class : is a way to bind the data and its associated functions together. All the elements of a class are private by default, even elements can be declared as public or protected. An object is an instance of a class. • Syntax: class class-name { access:specifier private data and functions }

  5. In the above syntax the every class has a unique name, the "access:specifier" can either private, public, protected. The "protected" is used when inhertinace is applied.

  6. Example: void main() { datx,y; x.setdat(1000); y.setdat(1245); x.show(); y.show(); } #include <iostream.h> class dat { private: int sdata; public: void setdat( int a) { sdata =a; } void show( ) { cout << "\nSet data is " << sdata; } }; Result: Set Data is:1000 Set Data is:1245

  7. In the above class example the "private" object "sdata" is used only within the function. But the functions "setdat", "show" are used in the main function since they are "public".

  8. Access specifiers • defines the access rights for the statements or functions that follows it until another access specifier or till the end of a class. The three types of access specifiers are "private", "public", "protected". • private:The members declared as "private" can be accessed only within the same class and not from outside the class. • public:The members declared as "public" are accessible within the class as well as from outside the class. • protected:The members declared as "protected" cannot be accessed from outside the class, but can be accessed from a derived class. This is used when inheritaance is applied to the members of a class.

  9. Nestedclass • Nested classis a class defined inside a class, that can be used within the scope of the class in which it is defined. In C++ nested classes are not given importance because of the strong and flexible usage of inheritance. Its objects are accessed using "Nest::Display".

  10. Example: void main() { Nest::Display x; x.sum(12, 10); x.show(); } #include <iostream.h> class Nest { public: class Display { private: int s; public: void sum( int a, int b) { s =a+b; } void show( ) { cout << "\nSum of a and b is:: " << s; } }; }; Result: Sum of a and b is::22

  11. In the above example, the nested class "Display" is given as "public" member of the class "Nest".

  12. Local class • Explanation Local class is a class defined inside a function. Following are some of the rules for using these classes. • Global variables declared above the function can be used with the scope operator "::". • Static variables declared inside the function can also be used. • Automatic local variables cannot be used. • It cannot have static data member objects. • Member functions must be defined inside the local classes. • Enclosing functions cannot access the private member objects of a local class.

  13. Example: #include <iostream.h> int y; void g(); int main() { g(); return 0; } void g() { class local { public: void put( int n) { ::y=n; } int get() {return ::y;} } ab; ab.put(20); cout << "The value assigned to y is::"<< ab.get(); } Result:The value assigned to y is::20

  14. In the above example, the local class "local" uses the variable "y" which is declared globally. Inside the function it is used using the "::" operator. The object "ab" is used to set, get the assigned values in the local class.

  15. Explanation • Object Oriented programmingis method of programming where - a system is considered as a collection of objects that interact together to accomplish certain tasks. Objects are entities that encapsulate data and procedures that operate on the data. • In OOPS first a concept known as "Object Oriented Analysis (OOA)" is used to specify the objects in term of real world requirements, their behavior and interactions required. The next concept would be the "Object Oriented Design (OOD)" that converts these real-time requirements as a hierarchy of objects in terms of software development requirement. Finally OOPS is used to implement the requirements using the C++ programming language. • The main purpose of object oriented programming is to simplify the design, programming and most importantly debugging a program. So to modify a particular data, it is easy to identify which function to use. To add additional features it is easy to identify where to add functions and its related data.

  16. Following are the basic elements of Object oriented programming(OOPS) • Object • Classes • Inheritance • Dynamic Binding • Polymorphism • Message Passing • Encapsulation

  17. Objects • Explanation Objects are instance of a class, that interact with each other at runtime. In OOPs, Objects are declared at the end of the class definition or after the "}" braces. They can be of any type based on its declaration

  18. Example: void main() { Cube x; x.cub(2); x.show(); } #include <iostream.h> class Cube { public: int cub( val) { r = val*val*val; return r; } void show() { cout << "The cube is::" << r; } private: int val, r; }x; Result:The cbe is :: 8 In the above example "x" is an object of the class "Cube“ used to access the functions inside the class.

  19. Classes • Explanation : has the data and its associated function wrapped in it. Classes are also known as a collection of similar objects or objects of same type. In the OOPs concept the variables declared inside a class are known as "Data Members" and the functions are known as "Member Functions".

  20. Syntax: class class-name { private: variable declaration; function declaration; public: variable declaration; function declaration; };

  21. Example: void main() { Square x; x.area(10); x.show(); } #include <iostream.h> class Square { private: int side, a; public: int area( side) { a =side*side; return a; } void show() { cout << "The area is::" << a; } }; Result:The area is:: 100

  22. In the above OOPs example the class "square" has functions "area" and "show" to calculate the area of the square and to display the area. so all these are objects that are related to the class "Square".

  23. The End of lecture

  24. Explanation • Inheritance is a method by which new classes are created or derived from the existing classes. Using Inheritance some qualities of the base classes are added to the newly derived class, apart from its own features The advantage of using "Inheritance" is due to the reusability of classes in multiple derived classes. The ":" operator is used for inheriting a class.

  25. The following table lists the visibility of the base class members in the derived classes

  26. Following are the different types of inheritance followed in C++. • Single Inheritance • Multiple Inheritance • Hierarchical Inheritance • Multilevel Inheritance • Hybrid Inheritance

  27. Example: int main () { Square sq; sq.set_values (5); cout << "The square of 5 is::" << sq.square() << endl; return 0; } #include <iostream.h> class Value { protected: intval; public: void set_values (int a) { val=a;} }; class Square: public Value { public: int square() { return (val*val); } }; Result:The square of 5 is:: 25 In the above example the object "val" of class "Value" is inherited in the derived class "Square".

  28. Explanation • Single Inheritanceis method in which a derived class has only one base class.

  29. Example: int main () { Cube cub; cub.set_values (5); cout << "The Cube of 5 is::" << cub.cube() << endl; return 0; } # include <iostream.h> class Value { protected: intval; public: void set_values (int a) { val=a;} }; class Cube: public Value { public: int cube() { return (val*val*val); } }; Result:The Cube of 5 is:: 125 In the above example the derived class "Cube" has only one base class "Value". This is the single inheritance OOP's concept.

  30. Multiple Inheritance • Explanation Multiple Inheritance is a method by which a class is derived from more than one base class.

  31. Example: int main () { Area r; r.set_values (5); r.show(r.area()); return 0; } #include <iostream.h> using namespace std; class Square { protected: int l; public: void set_values (int x) { l=x;} }; class CShow { public: void show(inti); }; void CShow::show (inti) { cout << "The area of the square is::" << i << endl; } class Area: public Square, public CShow { public: int area() { return (l *l); } }; Result:The area of the square is:: 25

  32. In the above example the derived class "Area" is derived from two base classes "Square" and "CShow". This is the multiple inheritance OOP's concept in C++.

  33. Hierarchical Inheritance • Explanation Hierarchical Inheritanceis a method of inheritance where one or more derived classes is derived from common base class.

  34. Example: int main () { Square s; s.set_values (10); cout << "The square value is::" << s.sq() << endl; Cube c; c.set_values (20); cout << "The cube value is::" << c.cub() << endl; return 0; } #include <iostream.h> class Side { protected: int l; public: void set_values (int x) { l=x;} }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube: public Side { public: int cub() { return (l*l*l); } }; Result:The square value is:: 100 The cube value is::8000

  35. In the above example the two derived classes "Square", "Cube" uses a single base class "Side". Thus two classes are inherited from a single class. This is the hierarchical inheritance OOP's concept in C++.

  36. Multilevel Inheritance • Explanation Multilevel Inheritanceis a method where a derived class is derived from another derived class.

  37. #include <iostream.h> class mm { protected: introllno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:\n"<< rollno << "\n"; } }; class marks : public mm { protected: int sub1; int sub2; public: void get_marks(intx,int y) { sub1 = x; sub2 = y; }

  38. int main() { res std1; std1.get_num(5); std1.get_marks(10,20); std1.disp(); return 0; } void put_marks(void) { cout << "Subject 1:" << sub1 << "\n"; cout << "Subject 2:" << sub2 << "\n"; } }; class res : public marks { protected: float tot; public: void disp(void) { tot = sub1+sub2; put_num(); put_marks(); cout << "Total:"<< tot; } }; Result:Roll Number Is: 5 Subject 1: 10 Subject 2: 20 Total: 30 In the above example, the derived function "res" uses the function "put_num()" from another derived class "marks", which just a level above. This is the multilevel inheritance OOP's concept in C++.

  39. Hybrid Inheritance • Explanation "Hybrid Inheritance" is a method where one or more types of inheritance are combined together and used.

  40. #include <iostream.h> class mm { protected: introllno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:"<< rollno << "\n"; } }; class marks : public mm { protected: int sub1; int sub2; public: void get_marks(intx,int y) { sub1 = x; sub2 = y; } void put_marks(void) { cout << "Subject 1:" << sub1 << "\n"; cout << "Subject 2:" << sub2 << "\n"; } };

  41. int main() { res std1; std1.get_num(10); std1.get_marks(10,20); std1.get_extra(33.12); std1.disp(); return 0; } class extra { protected: float e; public: void get_extra(float s) {e=s;} void put_extra(void) { cout << "Extra Score::" << e << "\n";} }; class res : public marks, public extra{ protected: float tot; public: void disp(void) { tot = sub1+sub2+e; put_num(); put_marks(); put_extra(); cout << "Total:"<< tot; } }; Result:Roll Number Is: 10 Subject 1: 10 Subject 2: 20 Extra score:33.12 Total: 63.12

  42. In the above example the derived class "res" uses the function "put_num()". Here the "put_num()" function is derived first to class "marks". Then it is derived and used in class "res". This is an example of "multilevel inheritance-OOP's concept". But the class "extra" is inherited a single time in the class "res", an example for "Single Inheritance". Since this code uses both "multilevel" and "single" inheritance it is an example of "Hybrid Inheritance".

  43. The End of Lecture

  44. CONTACTS • Mutaz Saeed : mutelr@gmail.com • : mutelr@hotmail.com • : mutazko2000@hotmail.com

More Related