Intro to OOP

hat is object oriented programming (OOP)? This question is a little difficult to answer because the computer industry has a fascination for terminologies and catch words. Not long ago words like “artificial intelligence”, “gui” and “net surfing” were used as if they were to offer a path to heavens. The same overuse seems to be happening to the phrase “objectoriented”. Since it has been proven that object-oriented techniques offer a way to write better programs, everybody seems to be slapping the label “object oriented” on their software products.


So it is important for us to understand what is OOP, why do we need it, what does it do that traditional languages like C, Pascal and Basic don’t and what are the principles behind OOP. This 1

While designing software a guideline that should be followed is ‘The Expression of an algorithm should model the application domain that it supports’. The Beginning. when you see a properly designed inventory control system. The techniques used in that expression determine how successfully the solution models its problem domain. There’s lot of ground to cover here. In a similar sense the purpose of a programming language is to express with algorithms the solution to a data processing problem.. so let’s get started.. you recognise that its purpose is to maintain a record of stock quantities and locations. What we say here will necessarily be general and brief. With the advent of mass storage devices and larger and cheaper computer memories. Mechanical switches were used to load programs. Similarly. the first high-level computer programming languages came into . Don’t worry if you don’t catch everything in this chapter on the first pass. That is. We will be going over these features again in subsequent chapters. For example. Or in other words the solution to a problem should resemble the problem that it solves. procedural programming was used for expression of an algorithm. Of late it is being replaced by objectoriented programming. The earliest computers were programmed in binary. the observer should be able to recognise that purpose of the solution without necessarily knowing the problem in advance. Earlier. you intuitively understand that the problem being solved is one of receiving and manipulating text. OOP is a complicated beast and learning about it takes time. You recognise those things because the solutions resemble and therefore remind you of the problems that they solve. To understand why this is so let’s begin by looking at the history of programming methodologies.2 Let Us C++ chapter addresses these issues and provides an overview of the features to be discussed in the rest of the book. when you see a properly designed word processor.

programmers could write a series of English like instructions that a compiler could translate into the binary language of computers. where and why the control is flowing. As the computers capacity and capability increased. (d) Writing. These languages suffered from the following limitations: (a) There were no facilities to reuse existing program code. limited to about a few hundred lines of source code. understanding and maintaining long programs became a programmer’s nightmare. Tracking down spurious changes in global data in long convoluted programs was a very tedious job. the earlier programming languages were found wanting in performing the complex programming task. instead of thinking in terms of bits and bytes.Chapter 1: Intro to OOP 3 existence. often without any clear indication of how. However. As a result. As a result. so also did the ability to develop more complex computer programs. it was simply duplicated. Wherever the same piece code was required. there was too much jumping around in the program. . (b) The control was transferred via the dangerous goto statement. With their arrival. programs were pretty short. (c) All variables in the program were global. These languages were simple in design and easy to use because programs at that time were primarily concerned with relatively simple tasks like calculations.

Information is passed between functions using parameters and functions can have local data that cannot be accessed outside the function’s scope. A new concept came into existence with structured programming: Abstraction. a structured program minimizes the chance that one procedure will affect another. if any. Abstraction permitted the programmer to look at something without being concerned with is internal details. This also makes it easier to isolate problems. It does not matter to the programmer how that task is . A program could now be divided into functions. In a structured program it is enough to know which task is performed by function. The breakthrough occurred in late 60’s and early 70’s with the introduction of structured programming. a quest began to develop new languages with new features that would help to create more sophisticated applications. which are now replaced by local variables that have a smaller and more controllable scope. Structured programming helps you to write cleaner code and maintain control over each function. There is less need of global variables. By isolating processes within functions. All this makes the development and maintenance of code fast as well as efficient. with each function having a clearly defined purpose and a clearly defined interface to the other functions in the program. The long programs that the programmer found difficult to comprehend could now be broken down into smaller units of few hundred statements. A structured program is built by breaking down the program’s primary purpose into smaller pieces that then become functions within the program.4 Let Us C++ Structured Programming To overcome the limitations mentioned above. Functions/subroutines/procedures were introduced in these languages to make the programs more comprehensible to their human creators. Each function can have its own data and logic.

in a payroll processing application the important part isn’t the function which displays the data. What they do may be more complex. there are no obvious program elements to which a menu or a window would correspond.Chapter 1: Intro to OOP 5 performed so long as the function does it reliably. In a procedural language. but the emphasis is still on doing. Functions do things just as single program statements do. and when changes occur in data types. Data is given a second class status in the procedural paradigm even though data is the reason for program’s existence. Subdivision of a program into functions continued this emphasis. For example. Another problem with structured programming is that its primary components—functions and data structures—don’t model the real world very well. This is called functional abstraction and is the corner stone of structured programming. modifications must be made at every location that acts on those data types within the program. For example. In a structured program. With the constant improvement in the hardware and increasingly more demands from the users for feature-rich programs. the whole emphasis is on doing things. etc. Structured programming ruled the roost for almost two decades. suppose you are writing a program to create the elements of a GUI: menus. the complexity of programs increased multi-fold and that’s the time structured programming approach started showing signs of strain. data types are processed in many functions. This occurred because of the weakness in the procedural paradigm (organising principle) itself. the important part is the payroll data itself. This is a frustrating and time-consuming task for large-sized programs. windows. This is because the structured programming paradigm . or the function that checks for correct input. One of the key reasons for the failure of procedural languages was the role played by data..

The world is. therefore. Data abstraction does for data what functional abstraction does for operations.6 Let Us C++ emphasizes on fitting a problem to the procedural approach of a language. . first you think about the object and its purpose and behavior. However. floating-point numbers are abstracted in programming languages. Because programmers are people. Abstraction for floating-point numbers has existed in programming languages since long. Problem solvers in other crafts do not perceive the world that way. You are not required to know how a floating-point number is represented in binary while assigning a value to it. For example. object-oriented. and the object-oriented programming paradigm expresses computer programs in ways that model how people perceive the world. Then you select your tools and procedures. The object-oriented paradigm is built on the foundation laid by the structured programming concepts and data abstraction. With data abstraction. data structures can be used without having to be concerned about the exact details of implementation. Object Oriented Programming The world and its applications are not organized into values and procedures separate from one another. or repair a carburetor. They deal with their problem domains by concentrating on the objects and letting the characteristics of those objects determine the procedures to apply to them. To build a house. grow a tomato. it is only natural that our approach to the work of the world reflects our view of the world itself. The solution fits the problem. Likewise. it is only recently languages have been developed to define your own abstract data types. you are not bothered how binary multiplication takes place while multiplying floating-point values.

Such a unit is called an object. The basic idea behind object-oriented language is to combine into a single unit. No other functions can access the data. It will read the item and return the value to you. If you want to create a data item in an object. and maintaining the program. the data and the functions that operate on the data. Data and its functions are encapsulated into a single entity. Object . An object’s functions.Chapter 1: Intro to OOP 7 The fundamental change in OOP is that a program is designed around the data being operated upon rather than upon the operations themselves. The organization of a C++ program is shown in Figure 1. A C++ program typically consists of a number of objects which communicate with each other by calling one another’s member functions. both. The data is hidden. debugging. typically provide the only way to access its data. you know exactly what functions interact with it: the member functions in the object. you call a member function in the object.1. If you want to modify the data in an object. You can’t access the data directly. This is to be expected once we appreciate that the very purpose of the program is to manipulate data. so it is safe from accidental alteration. called member functions in C++. This simplifies writing.

8 Let Us C++ Data Member Function Member Function Object Data Member Function Member Function Object Data Member Function Member Function Figure 1. you no longer ask how the problem will be divided into functions. has a surprisingly helpful effect on how easily programs can be designed. OOP attempts to fit the language to the problem. facilitates creating reusable code that can eventually save a lot of work.1 Thus. Another feature called inheritance permits . but how it will be divided into objects. There is more to OOP programming than just binding the data and functions together. OOP. Thinking in terms of objects. No such close match between programming constructs and the items being modelled exists in a procedural language. The match between programming objects and real world objects is the happy result of combining data and functions. rather than trying to fit a problem to the procedural approach of language. The resulting objects offer a revolution in program design. When you approach a programming problem in an object-oriented language. for example. A feature called polymorphism permits you to create multiple definitions for operators and functions. This results from the close match between objects in the programming sense and objects in the real world. rather than functions.

Unlike this. Objects In structured programming a problem is approached by dividing it into functions. 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. in object-oriented programming the problem is divided into objects. instead of concentrating on tasks. OOP introduces many new ideas and involves a different approach to programming than the procedural programming. etc. printer. keyboard. guns. Let us now briefly examine this vocabulary with regards to the major elements of object-oriented languages. Various elements in computer games like cannons. Hardware devices like disk drive. Thinking in terms of objects rather than functions makes the designing of program easier. In short. stacks. Customers. GUI elements like windows. Characteristics of Object-Oriented Languages Object-oriented programming uses a vocabulary that is unfamiliar to the procedural programmer. icons etc. As you can see. queues etc. you concentrate on representing concepts. menus. animals. sales persons in a sales tracking system Computers in a network model Etc. Classes .Chapter 1: Intro to OOP 9 you to derive new classes from old ones. etc.

long and float. Next we can define two derived classes named periodical and book. A class serves as a blueprint or a plan or a template. unlike the built-in data types. However. The new class can add data elements and functions to those it inherits from its base class. Programmers have to define the behaviour of dates by designing a date class. However. It specifies what data and what functions will be included in objects of that class. or the base class. For example. There are two primary types of publication: periodicals and books. For example. the language does not know user-defined data types. assignment and relational operators are defined as part of the language. The compiler and the computer do not know about dates. we can define many objects of the date class.10 Let Us C++ Most languages offer primitive data types like int. The new class referred to as a derived class. The programmer defines its format and behaviour by defining a class. Their data representation and response to arithmetic. These are the functions of publication class. can inherit the data structures and functions of the original. the number of pages and the accession number. Inheritance OOP permits you to create your own data types (classes) just like the types built into the language. the user-defined classes can use other classes as building blocks. there can be a user-defined data type to represent dates. Publications can be retrieved. The way we can declare many variables of the primitive type int. This class expresses the format of date and the operations that can be performed on it. Using a concept called inheritance new classes can be built from the old ones. Defining the class doesn’t create any objects. stored and read. we can build a set of classes that describe a library of publications. A . We can create a general publication class by defining data items for the publisher name. just as the mere existence of a type int doesn’t create any variables.

Chapter 1: Intro to OOP 11 periodical has a volume and issue number and contains articles written by different authors. periodical and book. the book class and the periodical class share the characteristics of publication class while having their own unique attributes. Data items for the book class will include the names of its author a cover type (hard or soft) and its ISBN number. Furthermore. As you can see. defines methods for storing and retrieving data.2 Polymorphism Extending the same example of the publication.2. Periodicals are located through a guide to periodical literature. the way to find a specific periodical is different from finding a book. Data items for these should be included in the definition of the periodical class. The periodical class will also need a function. while a book is usually placed on a shelf. This entire scenario is depicted in Figure 1. let us now understand another important concept. Our base class. Feature A Feature B Publication class Feature A Feature B Feature C Periodical class Feature A Feature B Feature E Book class Figure 1. subscribe. publication. while books are found . A periodical may be stored in a binder.

but you want to add the capability of displaying context sensitive help for each menu item. the retrieve function associated with a book is used. it can be distributed to other programmers for use in their own programs. 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. you simply create a new class that . The tried and tested capabilities of base classes do not need to be redeveloped. To do this. Programmers can devote time to writing new code instead of wasting time in rewriting existing code. For example. In our example. Reusability Object-oriented programs are built from reusable software components. This way software becomes easier to test since programming errors can be isolated within the new code of derived classes. even though the end result is same. You are happy with the working of this class and you don’t want to change it. This concept of polymorphism (one thing with several distinct forms) can be extended even to operators.12 Let Us C++ using a card catalogue system. you might have written (or purchased from someone else) a class that creates a menu system. new classes can be derived from existing ones. This is called reusability. If those programmers want to add new features or change the existing ones. Based on this we can design a ‘find through periodical literature’ function for a periodical and a ‘find through card catalogue’ function for a book. Polymorphism permits you to define a function for retrieving a publication that can work for both periodicals and books. but when a book is retrieved. the retrieve function that is specific to a periodical is used. Once a class is completed and tested. as we would see in later chapters. OOP provides an elegant facility called polymorphism to handle this situation. When a periodical is retrieved. the retrieval method for a periodical is different from the retrieval method for a book.

(b) Languages earlier than procedural programming languages made use of only global variables. (i) Objects are to classes as variables are to data types. (h) A class permits us to build user-defined data types. (f) Structured programming languages do not model the real world data as well as the Object Oriented programming languages. (d) It is easier to write. (c) In Object Oriented programming languages the only way to transfer control from one place in the program to another is by using the goto statement. . (e) Data is given a second class status in Procedural programming languages. Exercise [A] State True or False: (a) Object oriented programming languages permit reusability of the existing code.Chapter 1: Intro to OOP 13 inherits all the capabilities of the existing one but adds context sensitive help. (g) Object Oriented programming languages permit functional abstraction as well as data abstraction. understand and maintain programs in Object Oriented languages as compared to Procedural languages. The ease with which existing software can be reused is a major benefit of OOP.

(c) The process of building new classes from existing ones is called .14 Let Us C++ [B] Fill in the blanks: (a) The two major components of an object are & . (d) If a class A inherits its properties from class B. (e) Pascal and C are nguage. (b) The ability of a function or operator to act in different ways on different data types is called . whereas. respectively. C++ is la- . languages. then A and B are known as class and class.

Sign up to vote on this title
UsefulNot useful