Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.

Similar presentations


Presentation on theme: "1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions."— Presentation transcript:

1 1 n Object Oriented Programming

2 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions into groups known as functions n In object oriented programming the problem is divided into number of entities called objects n Here the focus is on objects

3 3 What is an Object?  An object is a software bundle of related variables and methods.  Software objects are often used to model real- world objects you find in everyday life.

4 4 What is a Class?  A class is a prototype that defines the variables and the methods common to all objects of a certain kind.  Class is an implementation of an abstract data type and so encapsulates both data and operations.  Object is run-time instance of class.  Classes just sit there, objects do the real work.  Memory is allocated for Objects not for Classes.

5 5 To be object oriented, a language must support  Encapsulation  Inheritance  Dynamic Binding Some of the popular OO languages are C++ Java FORTRAN90

6 6 Encapsulation:  Wrapping up of data and functions into a single unit is known as encapsulation Often, for practical reasons, an object may wish to expose some of its variables or hide some of its methods. Access Levels: SpecifierClassSubclassPackageWorld PrivateX ProtectedXXX PublicXXXX

7 7 What is Inheritance? A class inherits state and behavior from its superclass. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs. Super Class Subclasses

8 8 Properties:  Each subclass inherits state (in the form of variable declarations) from the superclass.  Subclasses can add variables and methods to the ones they inherit from the superclass.  Subclasses can also override inherited methods and provide specialized implementations for those methods.  You are not limited to just one layer of inheritance. The inheritance tree, or class hierarchy, can be as deep as needed. Benefits:  Re-Usability Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times..

9 9 A B C AB C A-1 A-2 B-1 B-2 AB Multi-level Inheritance Multiple Inheritance Multiple Multi-level Inheritance Types of Inheritance:

10 10 Encapsulation:  Wrapping up of data and functions into a single unit is known as encapsulation Often, for practical reasons, an object may wish to expose some of its variables or hide some of its methods. Access Levels: SpecifierClassSubclassPackageWorld PrivateX ProtectedXXX PublicXXXX

11 11 Polymorphism:  The ability to appear in many forms.  In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class.  It is the ability to redefine methods for derived classes.

12 12 Dynamic Binding:  Binding refers to the linking of a procedure call to the code to be executed in response to the call  Dynamic binding means that the code associated with a given procedure call is not known until the time of call at the run time

13 13 What is a Message? n Software objects interact and communicate with each other using messages. n A message is a request to an object to invoke one of its methods. A message therefore contains  the name of the method and  the arguments of the method.

14 14 #include class item { int number; //private by default float cost; Public: Void getdata(int a,float b); //prototype declaration Void putdata() //function defined inside class { cout<<“number is”<<number; cout<<“cost is”<<cost } }; Void :: getdata(int a, float b) { number=a; //private variables cost=b; } int main() { item x; //create object x x.getdata(100,299.95); x.putdata(); return 0; }

15 15 #include Class item { int number; //private by default float cost; Public: Void putdata() //function defined inside class { cout<<“number is”<<number; cout<<“cost is”<<cost } void getdata(int a, float b) { number=a; //private variables cost=b; } }; int main() { item x; //create object x x.getdata(100,299.95); x.putdata(); return 0; }

16 16 Creating and destroying objects n New->dynamic memory allocation operator n Delete->memory release operator n When we create object statically, it is stored in a storage area known as stack n When we create object dynamically, it is stored in a pool of memory known as heap.

17 17 Garbage collector n As an alternative for delete operator, the environment can provide a feature called garbage collector that automatically discovers when an object is no longer in use and destroys it.

18 18 Exception handling n Error handling n An exception is an object that is thrown from the site of the error and can be caught by an appropriate exception handler designed to handle that particular type of error

19 19 Advantages of OOP n Abstraction (high level) –An object can be used without knowing its internal representation n Modularity & Changeability –The source code for an object can be written and maintained independently of the source code for other objects. –A class can be easily changed without changing others classes n Readability –Easy to understand n Reusability

20 20 Fundamentals of program structure n Using iostreams class n To declare the functions and external data in the iostreams class, include the header file with the statement #include

21 21 Namespace n Each set of c++ definitions in a library or program is wrapped in a namespace, and if some other definition has an identical name, but in a different namespace, then there is no collision. n This is done with the help of a keyword “namespace” n By using the keyword “using” we can expose all the elements from that particular namespace ex: using namespace std;

22 22 n top down: you write first the main function, that calls stubs, then you subdivide these stubs in smaller stubs until a real work has to be done, that you code in the final files. n bottom-up: you write (or gather) small components that do basic actions that are required to do the full program work. Then you assemble them by custom code, then assemble these parts to write the main method.

23 23 Your First c++ program #include Using namespace std; int main() { Cout<<“hello world”; }

24 24 Creating functions n Standard c and c++ use a feature called function prototyping ex: int translate (float x, float y, float z) int translate (float, float, float ) n The compiler check the types of parameters with the types in the prototype whenever a function is called

25 25 Function return values n A c++ prototype must specify the return type of the function n To return a value from a function, use the RETURN statement ex: #include using namespace std; Char cfunc(int i) { if (i==0) return ‘a’; if (i==1) return ‘g’; if (i==5) return ‘c’; } int main() { cout<<“type an integer(0,1,5)”; Int val; cin>>val; cout<<cfunc(value)<<endl; }

26 26 Introducing strings n To use strings you need to include the c++ header file #include using namespace std; int main() { String s1,s2; //empty strings String s3=“hello, world”; //initialized String s4(“I am”); //also initialized s2=“today”; //assigning to a string s1=s3+” “+ s4 //combining strings s1+=“8”; //appending to a string cout<< s1+s2+ “!” <<endl; }

27 27 String operations n By using the function substr() we can create a substring. Ex: #include using namespace std; int main() { String str,s1; str=“how are u”; s1=str.substr(4,3); cout<<s1<<endl; }

28 28 Appending, Inserting and concatenating strings n append() is used to append a string n Insert() is used for inserting one string into another n The size() function returns the number of characters currently stored in a string n The capacity() function returns the number of characters the string can hold with out requesting more storage n For combining 2 strings we can use + operator n For comparing two strings use campare() function

29 29 Examples String str=“how are u”; str.append(“I have been working too hard”); str.insert(1,“hai”); int i=str.size(); int j=str.capacity(); String str1; str1=“hello”; Int a=str.compare(str1);

30 30 Reading and Writing files n To open files for reading and writing, you must include n To open a file for reading, you create an ifstream object, which behaves like cin n To open a file for writing, you create an ofstream object, which behaves like cout

31 31 getline() n getline() function allows you to read one line into a string object n The first argument is the ifstream object you are reading from and the second argument is the string object #include using namespace std; int main() { ifstream in(“scopy1.cpp”); ofstream out(“scopy2.cpp”); String s; while(getline(in,s)) out<<s<<“\n”; }


Download ppt "1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions."

Similar presentations


Ads by Google