You are on page 1of 8

My Concepts

OBJECT ORIENTED PROGRAMING CONCEPT:The object-oriented paradigm is built on the foundation laid by the structured programming concepts and data abstraction. Data abstraction does for data what functional abstraction does for operations. With data abstraction, data structures can be used without having to be concerned about the exact details of implementation. For example, floating-point numbers are abstracted in programming languages. You are not required to know how a floating-point number is represented in binary while assigning a value to it. Likewise, you are not bothered how binary multiplication takes place while multiplying floating-point values. Abstraction for floating-point numbers has existed in programming languages since long. However, it is only recently languages have been developed to define your own abstract data types. The fundamental change in OOP is that a program is designed around the data being operated upon rather than upon the operations themselves. This is to be expected once we appreciate that the very purpose of the program is to manipulate data. The basic idea behind object-oriented language is to combine into a single unit, both, the data and the functions that operate on the data. Such a unit is called an object. An objects functions, called member functions in C+ +, typically provide the only way to access its data. If you want to create a data item in an object, you call a member function in the object. It will read the item and return the value to you. You cant access the data directly. The data is hidden, so it is safe from accidental alteration. Data and its functions are encapsulated into a single entity.

ABC REDDY

My Concepts

If you want to modify the data in an object, you know exactly what functions interact with it: the member functions in the object. No other functions can access the data. This simplifies writing, debugging, and maintaining the program. A C++ program typically consists of a number of objects which communicate with each other by calling one anothers member functions. The organization of a C++ program is shown in Figure 1.1.

Object Data Member Function Member Function

Object Data Member Function Member Function Figure 1.1

Object Data Member Function Member Function

ABC REDDY

My Concepts

Thus, rather than trying to fit a problem to the procedural approach of language, OOP attempts to fit the language to the problem. When you approach a programming problem in an object-oriented language, you no longer ask how the problem will be divided into functions, but how it will be divided into objects. Thinking in terms of objects, rather than functions, has a surprisingly helpful effect on how easily programs can be designed. This results from the close match between objects in the programming sense and objects in the real world. The match between programming objects and real world objects is the happy result of combining data and functions. The resulting objects offer a revolution in program design. No such close match between programming constructs and the items being modeled exists in a procedural language. There is more to OOP programming than just binding the data and functions together. OOP, for example, facilitates creating reusable code that can eventually save a lot of work. A feature called polymorphism permits you to create multiple definitions for operators and functions. Another feature called inheritance permits you to derive new classes from old ones. As you can see, OOP introduces many new ideas and involves a different approach to programming than the procedural programming. In short, instead of concentrating on tasks, you concentrate on representing concepts. Characteristics of Object-Oriented Languages:Object-oriented programming uses a vocabulary that is unfamiliar to the procedural programmer. Let us now briefly examine this vocabulary with regards to the major elements of object-oriented languages. Objects:ABC REDDY

My Concepts

In structured programming a problem is approached by dividing it into functions. Unlike this, in objectoriented programming the problem is divided into objects. Thinking in terms of objects rather than functions makes the designing of program easier. Following are few candidates that can be treated as objects in different programming situations: Employees in a payroll processing system Data structures like linked lists, stacks, queues etc. GUI elements like windows, menus, icons etc. Hardware devices like disk drive, keyboard, printer, etc. Various elements in computer games like cannons, guns, animals, etc. Customers, sales persons in a sales tracking system Computers in a network model Etc. Classes:Most languages offer primitive data types like int, long and float. Their data representation and response to arithmetic, assignment and relational operators are defined as part of the language. However, the language does not know user-defined data types. The programmer defines its format and behavior by defining a class. For example, there can be a user-defined data type to represent dates. The compiler and the computer do not know about dates. Programmers have to define the behavior of dates by designing a date class. This class expresses the format of date and the operations that can be performed on it. The way we can declare many variables of the primitive type int, we can define many objects of the date class. A class serves as a

ABC REDDY

My Concepts

blueprint or a plan or a template. It specifies what data and what functions will be included in objects of that class. Defining the class doesnt create any objects, just as the mere existence of a type int doesnt create any variables. Inheritance:OOP permits you to create your own data types (classes) just like the types built into the language. However, unlike the built-in data types, the userdefined classes can use other classes as building blocks. Using a concept called inheritance new classes can be built from the old ones. The new class referred to as a derived class, can inherit the data structures and functions of the original, or the base class. The new class can add data elements and functions to those it inherits from its base class. For example, we can build a set of classes that describe a library of publications. There are two primary types of publication: periodicals and books. We can create a general publication class by defining data items for the publisher name, the number of pages and the accession number. Publications can be retrieved, stored and read. These are the functions of publication class. Next we can define two derived classes named periodical and book. A periodical has a volume and issue number and contains articles written by different authors. Data items for these should be included in the definition of the periodical class. The periodical class will also need a function, subscribe. Data items for the book class will include the names of its author a cover type (hard or soft) and its ISBN number. As you can see, the book class and the periodical class share the characteristics of publication class while having their own unique attributes. This entire scenario is depicted in Figure 1.2.

ABC REDDY

My Concepts

Feature A Feature B Publicati on class Feature A Feature B Feature C Periodic al class Figure 1.2 Feature A Feature B Feature E Book class

Polymorphism:Extending the same example of the publication, periodical and book, let us now understand another important concept. Our base class, publication, defines methods for storing and retrieving data. A periodical may be stored in a binder, while a book is usually placed on a shelf. Furthermore, the way to find a specific periodical is different from finding a book. Periodicals are located through a guide to periodical literature, while books are found using a card catalogue system. Based on this we can design a

ABC REDDY

My Concepts

find through periodical literature function for a periodical and a find through card catalogue function for a book. OOP provides an elegant facility called polymorphism to handle this situation. In our example, the retrieval method for a periodical is different from the retrieval method for a book, even though the end result is same. Polymorphism permits you to define a function for retrieving a publication that can work for both periodicals and books. When a periodical is retrieved, the retrieve function that is specific to a periodical is used, but when a book is retrieved, the retrieve function associated with a book is used. The end result is that a single function name can be used for the same operation performed on related derived classes even if the implementation of that function varies from class to class. This concept of polymorphism (one thing with several distinct forms) can be extended even to operators, as we would see in later chapters. Reusability:Object-oriented programs are built from reusable software components. Once a class is completed and tested, it can be distributed to other programmers for use in their own programs. This is called reusability. If those programmers want to add new features or change the existing ones, new classes can be derived from existing ones. The tried and tested capabilities of base classes do not need to be redeveloped. Programmers can devote time to writing new code instead of wasting time in rewriting existing code. This way software becomes easier to test since programming errors can be isolated within the new code of derived classes. For example, you might have written (or purchased from someone else) a class that creates a menu system. You are happy with the working of this class and you dont want to change it, but you want to add

ABC REDDY

My Concepts

the capability of displaying context sensitive help for each menu item. To do this, you simply create a new class that inherits all the capabilities of the existing one but adds context sensitive help. The ease with which existing software can be reused is a major benefit of OOP.

ABC REDDY

You might also like