Download presentation
Presentation is loading. Please wait.
1
Object Oriented Programming Language (OOP)
بسم الله الرحمن الرحيم Object Oriented Programming Language (OOP)
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 Syntax: class class-name
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: #include <iostream.h> class dat { private: int sdata; public: void setdat( int a) sdata =a; } void show( ) cout << "\nSet data is " << sdata; }; void main() { dat x,y; x.setdat(1000); y.setdat(1245); x.show(); y.show(); } 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
Nested class Nested 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".
10
Example: void main() { Nest::Display x; x.sum(12, 10); x.show(); }
#include <iostream.h> class Nest { public: class Display private: int s; void sum( int a, int b) s =a+b; } void show( ) cout << "\nSum of a and b is:: " << s; }; void main() { Nest::Display x; x.sum(12, 10); x.show(); } 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: Result: The value assigned to y is::20
#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 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.
16
Following are the basic elements of Object oriented programming(OOPS)
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: In the above example "x" is an object of the class "Cube“
#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; void main() { Cube x; x.cub(2); x.show(); } 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: };
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; }; void main() { Square x; x.area(10); x.show(); } 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
Derived Class Visibility
The following table lists the visibility of the base class members in the derived classes Derived Class Visibility Base Class Visibility Public derivation Private derivation Protected derivation Private Not inherited Protected Public
26
Following are the different types of inheritance followed in C++.
Single Inheritance Multiple Inheritance Hierarchical Inheritance Multilevel Inheritance Hybrid Inheritance
27
Example: #include <iostream.h> class Value { protected: int val; public: void set_values (int a) { val=a;} }; class Square: public Value int square() { return (val*val); } int main () { Square sq; sq.set_values (5); cout << "The square of 5 is::" << sq.square() << endl; return 0; } 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 Inheritance is method in which a derived class has only one base class.
29
Example: # include <iostream.h> class Value { protected: int val; public: void set_values (int a) { val=a;} }; class Cube: public Value int cube() { return (val*val*val); } int main () { Cube cub; cub.set_values (5); cout << "The Cube of 5 is::" << cub.cube() << endl; return 0; } 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: Result: The area of the square is:: 25 int main () { Area r;
#include <iostream.h> using namespace std; class Square { protected: int l; public: void set_values (int x) { l=x;} }; class CShow void show(int i); void CShow::show (int i) cout << "The area of the square is::" << i << endl; } class Area: public Square, public CShow int area() { return (l *l); } int main () { Area r; r.set_values (5); r.show(r.area()); return 0; } 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 Inheritance is a method of inheritance where one or more derived classes is derived from common base class.
34
cout << "The square value is::" << s.sq() << endl;
Example: #include <iostream.h> class Side { protected: int l; public: void set_values (int x) { l=x;} }; class Square: public Side int sq() { return (l *l); } class Cube: public Side public: int cub() { return (l*l*l); } 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; } 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 Inheritance is a method where a derived class is derived from another derived class.
37
#include <iostream
#include <iostream.h> class mm { protected: int rollno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:\n"<< rollno << "\n"; } }; class marks : public mm int sub1; int sub2; void get_marks(int x,int y) { sub1 = x; sub2 = y; }
38
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; } int main() { res std1; std1.get_num(5); std1.get_marks(10,20); std1.disp(); return 0; } 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
#include <iostream.h> class mm { protected: int rollno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:"<< rollno << "\n"; } }; class marks : public mm int sub1; int sub2; void get_marks(int x,int y) { sub1 = x; sub2 = y; } void put_marks(void) cout << "Subject 1:" << sub1 << "\n"; cout << "Subject 2:" << sub2 << "\n"; }
41
class extra int main() { res std1; std1.get_num(10);
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{ float tot; void disp(void) { tot = sub1+sub2+e; put_num(); put_marks(); put_extra(); cout << "Total:"<< tot; } int main() { res std1; std1.get_num(10); std1.get_marks(10,20); std1.get_extra(33.12); std1.disp(); return 0; } Result: Roll Number Is: 10 Subject 1: 10 Subject 2: 20 Extra score:33.12 Total: 63.12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.