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

OOP Course

Embed Size (px)

DESCRIPTION

OOP Course

Citation preview

  • 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 Inheritance Private Inheritance Protected Inheritance Public Inheritance

  • Multiple Inheritance Polymorphism OOP With C ++ Language

  • ExplanationA 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 }

  • 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.

  • Example: #include class dat { private: int sdata; public: void setdat( int a) { sdata =a; } void show( ) { cout
  • 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".

  • Access specifiersdefines 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.

  • Nested classNested class is 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".

  • Example: #include class Nest { public: class Display { private: int s; public: void sum( int a, int b) { s =a+b; } void show( ) { cout
  • In the above example, the nested class "Display" is given as "public" member of the class "Nest".

  • Local classExplanation 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.

  • Example: #include 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

  • 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.

  • Explanation Object Oriented programming is 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.

  • Following are the basic elements of Object oriented programming(OOPS)

    Object Classes Inheritance Dynamic Binding Polymorphism Message Passing Encapsulation

  • 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

  • Example:#include class Cube { public: int cub( val) { r = val*val*val; return r; } void show() { cout
  • 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".

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

  • Example: #include class Square { private: int side, a; public: int area( side) { a =side*side; return a; } void show() { cout
  • 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".

  • The End of lecture

  • ExplanationInheritance 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.

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

    Derived Class VisibilityBase Class VisibilityPublic derivationPrivate derivationProtected derivationPrivateNot inheritedNot inheritedNot inheritedProtectedProtectedPrivateProtectedPublicPublicPrivateProtected

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

  • Example: #include class Value { protected: int val; public: void set_values (int a) { val=a;} }; class Square: public Value { public: int square() { return (val*val); } }; Result: The square of 5 is:: 25In the above example the object "val" of class "Value" is inherited in the derived class "Square".

  • ExplanationSingle Inheritance is method in which a derived class has only one base class.

  • Example: # include class Value { protected: int val; 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.

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

  • Example: #include using namespace std; class Square { protected: int l; public: void set_values (int x) { l=x;} }; class CShow { public: void show(int i); }; void CShow::show (int i) { cout
  • 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++.

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

  • #include 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); } };Example:int main (){ Square s; s.set_values (10); cout
  • 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++.

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

  • #include class mm { protected: int rollno; public: void get_num(int a) { rollno = a; } void put_num() { cout
  • void put_marks(void) { cout
  • Hybrid Inheritance Explanation "Hybrid Inheritance" is a method where one or more types of inheritance are combined together and used.

  • #include class mm { protected: int rollno; public: void get_num(int a) { rollno = a; } void put_num() { cout
  • class extra { protected: float e; public: void get_extra(float s) {e=s;} void put_extra(void) { cout
  • 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".

  • The End of Lecture

  • CONTACTSMutaz Saeed : [email protected] : [email protected] : [email protected]