• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Oops index
 

Oops index

on

  • 1,209 views

 

Statistics

Views

Total Views
1,209
Views on SlideShare
1,209
Embed Views
0

Actions

Likes
0
Downloads
10
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Oops index Oops index Document Transcript

    • OOPS Concepts using C++C++ as better C ................................................................................................................... 2Keys to Object..................................................................................................................... 6Defining a class in C++ .................................................................................................... 10Objects and Encapsulation................................................................................................ 16Messages and Polymorphism............................................................................................ 17Data Abstraction ............................................................................................................... 19Natural Building Blocks ................................................................................................... 20Polymorphism in an application ....................................................................................... 61Program Using Polymorphism.......................................................................................... 74 L & T Infotech Page 1 of 87
    • OOPS Concepts using C++ C++ as better C C++ as better CC has been in use for over 25 years. Therefore, there is a huge amount of legacy C code inplace. However, once people learn C++, they find its features more powerful than those of C, sothey choose to move to C++.C++ is a hybrid language, so it is possible to program in either a C-style, an object-oriented style,or both. Most programmers take advantage of rich collections of existing classes and functions inC++ classes and the ANSI C standard function library (which C++ borrows from ANSI C). Therefore, there are two parts to learning C++: • learning C++ itself • learning how to use the classes in various C++ class libraries and the functions in the ANSI C standard library If you want to mix C and C++ code in a program, • your C++ compiler should direct the linking process (so it can access its special libraries) • your C and C++ compilers probably need to come from the same vendor and have compatible versions (so they have the same calling conventions) You can include a standard C header file in your C++ code. Using C functions in C++Lets now consider using C functions in C++. It is possible to call functions written and compiledwith a C compiler from a C++ program. However, C++ specially encodes function names for type-safe linkage while C doesnt encode its function names. So a function compiled in C will not berecognized when an attempt is made to link C code with C++ code because the C++ codeexpects a specially encoded function name.This function name encoding is often referred to as name manglingC++ enables you to provide linkage specifications to • inform the compiler that a function was compiled on a C compiler • prevent the function name from being encoded by the C++ compilerLinkage specifications are particularly useful when large libraries of specialized functions havebeen developed. They are useful also when another programmer doesnt have access to thesource code of those library functions for recompilation into C++ or time to convert them from C toC++.To tell a C++ compiler that one or several functions have been compiled in C, precede thefunction prototypes with storage class specifier extern. L & T Infotech Page 2 of 87
    • OOPS Concepts using C++ C++ as better C extern "C" function prototype //Single function extern "C" //Multiple functions { function prototypes } Using extern means • name encoding (or name mangling) wont be performed on the functions listed in the linkage specification • the specified functions can be linked properly with the program extern "C" function prototype //Single function extern "C" //Multiple functions { function prototypes }C++ compilers normally include the standard C libraries, so no need to use linkage specificationsfor those functions. Object-Oriented TechnologyObject oriented programming is essentially building a program around self-contained collectionsof data and code to modify that data; this programming model is in contrast to a model that usesfunction that act on data scattered throughout a program. Object-oriented programming (orcoding, as programming is commonly referred to) is an organizational style, but it helpsprogrammers create reusable code because the code to do a specific thing is entirely containedwithin a single section of code, and to use the code to perform tasks - for instance, creating amenu - involves using only a small number of functions to access the internals of the class.A black box that can be easily carried from place to place, and that performs complex actionssimply at the press of a button: for instance, a microwave lets you heat food for a specified timelimit - say, two minutes - by typing in the time and pressing the heat button. You do not need toknow how the microwave operates or why the physics works.In the same way that self-contained appliances simplify life for the consumer, object-orientedprogramming simplifies the transfer of source code from one program to another program byencapsulating it - putting it all in one place.Object-oriented technology is both immense and far-reaching. End users of computer systemsand computer-based systems notice the effects of object-oriented technology in the form ofincreasingly easy-to-use software applications and operating systems and in more flexibleservices being provided by such industries as banking, telecommunications, and cable television. L & T Infotech Page 3 of 87
    • OOPS Concepts using C++ C++ as better CFor the software engineer, object-oriented technology encompasses object-oriented programminglanguages, object-oriented development methodologies, management of object-oriented projects,object-oriented computer hardware, and object-oriented computer aided software engineering,among others.It is not surprising, therefore, that there is some confusion regarding object-oriented terms andconcepts. Many of the terms commonly used in object-oriented technology were originally used todescribe object-oriented programming (coding) concepts. Specifically, although the terms wereborrowed from a non-computer-software perspective, they were first used extensively to describeconcepts embodied in object-oriented programming languages, such as Smalltalk, C++, andEiffel. However, these terms are quite useful even if one never intends to write any software at all. The progress of abstractionAll programming languages provide abstractions. It can be argued that the complexity of theproblems you’re able to solve is directly related to the kind and quality of abstraction. Assemblylanguage is a small abstraction of the underlying machine. Many so-called “imperative” languagesthat followed (such as Fortran, BASIC, and C) were abstractions of assembly language. Theselanguages are big improvements over assembly language, but their primary abstraction stillrequires you to think in terms of the structure of the computer rather than the structure of theproblem you are trying to solve. The programmer must establish the association between themachine model (in the “solution space,” which is the place where you’re modeling that problem,such as a computer) and the model of the problem that is actually being solved (in the “problemspace,” which is the place where the problem exists). The effort required to perform this mapping,and the fact that it is extrinsic to the programming language, produces programs that are difficultto write and expensive to maintain, and as a side effect created the entire “programmingmethods” industry.The alternative to modeling the machine is to model the problem you’re trying to solve. Earlylanguages such as LISP and APL chose particular views of the world (“All problems are ultimatelylists” or “All problems are algorithmic”). PROLOG casts all problems into chains of decisions.Languages have been created for constraint-based programming and for programmingexclusively by manipulating graphical symbols. (The latter proved to be too restrictive.) Each ofthese approaches is a good solution to the particular class of problem they’re designed to solve,but when you step outside of that domain they become awkward.The object-oriented approach goes a step farther by providing tools for the programmer torepresent elements in the problem space. This representation is general enough that theprogrammer is not constrained to any particular type of problem. We refer to the elements in theproblem space and their representations in the solution space as “objects.” (Of course, you willalso need other objects that don’t have problem-space analogs.) The idea is that the program isallowed to adapt itself to the lingo of the problem by adding new types of objects, so when youread the code describing the solution, you’re reading words that also express the problem. This isa more flexible and powerful language abstraction than what we’ve had before. Thus, OOP allowsyou to describe the problem in terms of the problem, rather than in terms of the computer wherethe solution will run. There’s still a connection back to the computer, though. Each object looksquite a bit like a little computer; it has a state, and it has operations that you can ask it to perform.However, this doesn’t seem like such a bad analogy to objects in the real world; they all havecharacteristics and behaviors.Some language designers have decided that object-oriented programming by itself is notadequate to easily solve all programming problems, and advocate the combination of variousapproaches into multiparadigm programming languages.[4]Alan Kay summarized five basic characteristics of Smalltalk, the first successful object-orientedlanguage and one of the languages upon which C++ is based.These characteristics represent a pure approach to object-oriented programming: L & T Infotech Page 4 of 87
    • OOPS Concepts using C++ C++ as better C1. Everything is an object. Think of an object as a fancy variable; it stores data, but you can “make requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program.2. A program is a bunch of objects telling each other what to do by sending messages.To make a request of an object, you “send a message” to that object. More concretely, youcan think of a message as a request to call a function that belongs to a particular object.3. Each object has its own memory made up of other objects. Put another way, you create a new kind of object by making a package containing existing objects. Thus, you can build complexity in a program while hiding it behind the simplicity of objects.4. Every object has a type. Using the parlance, each object is an instance of a class, in which “class” is synonymous with “type.” The most important distinguishing characteristic of a class is “What messages can you send to it?”5. All objects of a particular type can receive the same messages. This is actually a loaded statement, as you will see later. Because an object of type “circle” is also an object of type “shape,” a circle is guaranteed to accept shape messages. This means you can write code that talks to shapes and automatically handles anything that fits the description of a shape. This substitutability is one of the most powerful concepts in OOP. L & T Infotech Page 5 of 87
    • OOPS Concepts using C++ Keys to Object Keys to ObjectWhy Objects?The chasm between conventional software development and true, object-oriented development isnot easily crossed. A company really has to want to be on the other side in order to make the leapsuccessfully. So, its only fair to begin with the most basic question: Why bother?The answer, in a nutshell, is this:Objects are the enabling technology for adaptive business systems. The Adaptive OrganizationNatural selection, the engine of adaptation in all living systems, has just shifted into high gear. Itis now operating at the level of organizations rather than organisms, and the cycle of adaptationsis measured in months rather than millennia. The competitive environment of business iscontinuously changing, and the pace of that change is increasing at an accelerating rate. Where itwas once possible for a company to stake out its marketing turf and defend its position for years,static positioning is now viable only in a few isolated industries. For most companies today, theonly constant is change.How is a company to cope with this kind of change? The message from the management gurus isclear and consistent: The key to survival in todays chaotic business environment is rapidadaptation. The adaptive organization can move quickly into new market niches, deliver customsolutions instead of fixed products, and continuously outmaneuver its competition in the ongoingbattle for market share.Unfortunately, its a lot easier to preach the benefits of adaptivity than to realize them.Organizations have a natural inertia that inhibits any change in direction, and that inertiaincreases with the mass of the company. Much of the resistance stems from human nature—people stake out their turf within organizations and tend to oppose any change that threatenstheir position. Reward structures keyed to quarterly earnings only serve to reinforce the statusquo and discourage rapid change. But even if all the human and organizational barriers to changecould be overcome, there is another source of inertia with a mass approximating that of a blackhole—namely, corporate information systems. From Productivity to AdaptivityThe software construction primer in the appendix offers a quick overview of the way we buildinformation systems and how that approach has evolved over the past 50 years. Althoughinformation systems now allow organizations to do things that would have been unthinkable priorto the advent of computers, there is one thing they hinder far more than help: the process ofchange. Large information systems are notoriously resistant to change, so much so that manycompanies find themselves locked in place by the very systems that helped them to becomecompetitive just a few short years ago.For many years, the standard answer to this problem was to increase the speed of softwaredevelopment. Fourth-generation languages (4GLs), computer-aided software engineering (CASE)tools, and yes, object technology, have all promised and failed to deliver the "order-of-magnitudeproductivity improvement" that has long served as the holy grail of software development.Although I remain convinced that objects can deliver on that promise, I no longer believe it is theright goal. We have passed the point where building new applications faster can solve theproblem. No matter how much we accelerate the development process, the increasing pace ofbusiness change will continue to outstrip our ability to create new software.The only enduring solution to the challenge of constant change lies in the development ofadaptive business systems—systems that can change at least as fast as the organizations they L & T Infotech Page 6 of 87
    • OOPS Concepts using C++ Keys to Objectsupport. This is a radical departure from the time-honored practice of developing new applicationsfrom scratch to meet new business requirements. It requires us to construct software systems ofsufficient flexibility that they can quickly be modified in response to new opportunities andchallenges. In short, the answer lies not in productivity but in adaptivity. The Enabling TechnologyThe key benefit of object technology is that it is the enabling technology for adaptive businesssystems. However, this adaptivity is not an automatic consequence of adopting objects. Manycompanies are using objects simply as a different tool for doing what they have always done—creating new applications to solve specific business problems. Objects themselves are naturallyadaptive units of software, but they lose all their flexibility if they are locked into conventionalapplications.The key to building adaptive systems is to understand and uphold the principles of objecttechnology at every level of a system, from the lowest-level object to the enterprise itself.Fortunately, this isnt very hard to do. The most difficult part is simply getting out of the way—setting aside our preconceptions of how software should be built and discovering where objectswill take us if we remain true to their principles as we build our way up to the enterprise. The Three KeysThe definition of object technology has been a source of debate throughout its history.However, there is an industry-standard definition of object-oriented technology, and it can besummarized in terms of three key concepts: 1. Objects that provide encapsulation of procedures and data 2. Messages that support polymorphism across objects 3. Classes that implement inheritance within class hierarchiesThese three concepts and their associated terminology are explained in the remaining sections ofthis chapter. But even without further explanation, the three concepts can be used to make adistinction between languages that are and are not object-oriented.The object languages that are most widely used in commercial applications are Smalltalk,C++, and Java. Eiffel has gained widespread acceptance in Europe; the latest version of Adaqualifies as object-oriented; and Object COBOL is finally making its way into the market. Of all theobject languages currently available, Java is having the greatest impact on the industry andshows the most promise for building adaptive business systems.By contrast, the original versions of C, Ada, and COBOL are anything but object-oriented. Closerto the border are languages like Visual Basic (VB), which began as a conventional language butnow supports most of the mechanisms of object technology. As you can see from the examples,many languages are adding object features, so the list of options is constantly growing. VisualBasic is a good illustration of this—all that version 5 lacks is inheritance, and VB could very wellbe fully object-oriented in its next release. The important point is not whether a language is "truly"object-oriented but how easy it is to apply the principles of objects in the environment provided bythe language. Object & ClassAristotle was probably the first to begin a careful study of the concept of type; he spoke of “theclass of fishes and the class of birds.” The idea that all objects, while being unique, are also partof a class of objects that have characteristics and behaviors in common was used directly in thefirst object-oriented language, Simula-67, with its fundamental keyword class that introduces anew type into a program. L & T Infotech Page 7 of 87
    • OOPS Concepts using C++ Keys to ObjectSimula, as its name implies, was created for developing simulations such as the classic “bankteller problem.” In this, you have a bunch of tellers, customers, accounts, transactions, and unitsof money – a lot of “objects.” Objects that are identical except for their state during a program’sexecution are grouped together into “classes of objects” and that’s where the keyword classcame from. Creating abstract data types (classes) is a fundamental concept in object-orientedprogramming. Abstract data types work almost exactly like built-in types: You can createvariables of a type (called objects or instances in object-oriented parlance) and manipulate thosevariables (called sending messages or requests; you send a message and the object figures outwhat to do with it). The members (elements) of each class share some commonality: everyaccount has a balance, every teller can accept a deposit, etc. At the same time, each memberhas its own state, each account has a different balance, each teller has a name. Thus, the tellers,customers, accounts, transactions, etc., can each be represented with a unique entity in thecomputer program. This entity is the object, and each object belongs to a particular class thatdefines its characteristics and behaviors.So, although what we really do in object-oriented programming is create new data types, virtuallyall object-oriented programming languages use the “class” keyword. When you see the word“type” think “class” and vice versa.Since a class describes a set of objects that have identical characteristics (data elements) andbehaviors (functionality), a class is really a data type because a floating point number, forexample, also has a set of characteristics and behaviors. The difference is that a programmerdefines a class to fit a problem rather than being forced to use an existing data type that wasdesigned to represent a unit of storage in a machine. You extend the programming language byadding new data types specific to your needs. The programming system welcomes the newclasses and gives them all the care and type checking that it gives to built-in types.The object-oriented approach is not limited to building simulations. Whether or not you agree thatany program is a simulation of the system you’re designing, the use of OOP techniques caneasily reduce a large set of problems to a simple solution.Once a class is established, you can make as many objects of that class as you like, and thenmanipulate those objects as if they are the elements that exist in the problem you are trying tosolve. Indeed, one of the challenges of object-oriented programming is to create a one-to-onemapping between the elements in the problem space and objects in the solution space.But how do you get an object to do useful work for you? There must be a way to make a requestof the object so that it will do something, such as complete a transaction, draw something on thescreen or turn on a switch. And each object can satisfy only certain requests. The requests youcan make of an object are defined by its interface, and the type is what determines the interface.A simple example might be a representation of a light bulb: L & T Infotech Page 8 of 87
    • OOPS Concepts using C++ Keys to Object Light lt; lt.on();The interface establishes what requests you can make for a particular object. However, theremust be code somewhere to satisfy that request. This, along with the hidden data, comprises theimplementation. From a procedural programming standpoint, it’s not that complicated. A type hasa function associated with each possible request, and when you make a particular request to anobject, that function is called. This process is usually summarized by saying that you “send amessage” (make a request) to an object, and the object figures out what to do with that message(it executes code).Here, the name of the type/class is Light, the name of this particular Light object is lt, and therequests that you can make of a Light object are to turn it on, turn it off, make it brighter or makeit dimmer. You create a Light object by declaring a name (lt) for that object. To send a messageto the object, you state the name of the object and connect it to the message request with aperiod (dot). From the standpoint of the user of a pre-defined class, that’s pretty much all there isto programming with objects.The diagram shown above follows the format of the Unified Modeling Language (UML). Eachclass is represented by a box, with the type name in the top portion of the box, any data membersthat you care to describe in the middle portion of the box, and the member functions (thefunctions that belong to this object, which receive any messages you send to that object) in thebottom portion of the box. Often, only the name of the class and the public member functions areshown in UML design diagrams, and so the middle portion is not shown. If you’re interested onlyin the class name, then the bottom portion doesn’t need to be shown, either. ClassesStructured programming languages use functions to structure a program. C++, on the otherhand, provides both functions and classes. Data abstraction is implemented in C++ by the classuser-defined datatype.Classes in C++ evolved from the C struct concept. A struct represents one or more data values,which can be manipulated in a manner similar to individual variables.Access to a struct is not restricted – so once a struct variable is within scope, it can be modified.In C++, you use classes to define new datatypes as encapsulations of data and operations.Each class contains data as well as the set of functions that manipulates that data. The datacomponents of a class are called data members. And the function components of a class arecalled member functions. L & T Infotech Page 9 of 87
    • OOPS Concepts using C++ Object and EncapsulationDefining a class in C++1. Defining classesC++ provides data abstraction through the class construct. This is a typical class definition.class clock{private : short tHour; short tMin; short tSec;public: void set_time(int,int,int); void disp_time();}stopwatch;A class • encapsulates data • names the class • controls access to functions by means of the private, public, and protected keywords • declares instances of the class (if any)int x=20;main() { int x; x=10; }class clock{private : short tHour; short tMin; short tSec;public: void set_time(int,int,int); void disp_time();}stopwatch;Class instances are created when you declare a variable of the class type. L & T Infotech Page 10 of 87
    • OOPS Concepts using C++ Object and EncapsulationBy default, the members of a class are private. This means they can only be accessed by otherclass members.The following are some guidelines for declaring a class: • all members should be defined as private, public, or protected • members that have the same access control should be kept togetherint x=20;main() { int x; x=10; }#include <iostream.h>class date{private : short dd; short mm; short yy;public: void set_date(); short validate(); short find_day();};The code in the example declares data members.Data members of a class must not be directly initialized as in the next code.int x=20;main() { int x; x=10; }short mm=12;#include <iostream.h>class date{private : short dd; short mm=12; short yy; L & T Infotech Page 11 of 87
    • OOPS Concepts using C++ Object and Encapsulationpublic: void set_date(); short validate(); short find_day();};Member function declarations are similar to function declarations.For example, the member function validate() in short validate ( ); is declared as taking noarguments and returning a value of the short datatype.int x=20;main() { int x; x=10; }#include <iostream.h>class date{private : short dd; short mm=12; short yy;public: void set_date(); short validate(); short find_day();}; Declaring class instancesInstances of a class can be declared in a number of ways. An instance is a single occurrence ofa class.For example, an instance of the class clock could be stopwatch.int x=20;main() { int x; x=10; }class clock{ L & T Infotech Page 12 of 87
    • OOPS Concepts using C++ Object and Encapsulationprivate : short tHour; short tMin; short tSec;public: void set_time(int,int,int); void disp_time(); } stopwatch, digital, analog;You can define a class instance by specifying an instance name in the part of the structure calledthe instance list.The above code defines three instances of the class clock.You can define an instance of a class separately from the class declaration.A class instance may also be defined by using the keyword class followed by the class name anda unique identifier.In this case, the code is class numbers.An instance of the class numbers is declared in class numbers no;int x=20;main() { int x; x=10; }class numbers{private : short x, y, z;public: void InitNumbers (short nx, short ny); void getZ(void); void displayZ(void);};}void main (void){ class numbers no; no.InitNumbers (1, 2); no.getZ(); no.displayZ();};The datatype specifier class is dropped from the definition in numbers no;.The compiler recognizes the class name "numbers" from its previous declaration and creates aninstance of that class called "no".Because of this, the keyword class is usually dropped from the definition. L & T Infotech Page 13 of 87
    • OOPS Concepts using C++ Object and Encapsulation Accessing classesIn a class construct you can control access to class members by using the public,private, and protected access control specifiers.Data members of a class are usually private, though they can be public.Private data members can only be accessed by other members of their class.Public functions can be accessed outside their class.This means that private members can be accessed through public access or member functions.A classs data and function members belong to that classs scope inshort date :: validate ()C++ requires that a member function definition specifies the class for which it is in scope.This is done by using the :: scope resolution operator in the header of the definition.Resolving the scope of the get_data() function means that the private data members dd, mm, yycan be accessed.int x=20;main() { int x; x=10; }private: short dd; short mm; short yy;public: void get_data(short, short, short);}void date :: get_data(short d,short m,short y){ dd = d; mm = m; yy = y;}main (){ short c; short day_of_the_week; date sd; } L & T Infotech Page 14 of 87
    • OOPS Concepts using C++ Object and EncapsulationClass members can be accessed using the dot operator (.) as inday.validate ();The dot operator accesses a class member by means of the variable name for the object or areference to the object. The dot operator can be used to access public members of a class onlywhen the instance of a class being accessed is named.date day ;day.validate ();The dot operator is used with the public function to validate the users input.A classs data and function members belong to that classs scope, C++ requires that a memberfunction definition specify the class for which it is in scope, and class members can be accessedusing the dot operator. L & T Infotech Page 15 of 87
    • OOPS Concepts using C++ Object and Encapsulation Objects and EncapsulationAlthough object technology came into the commercial limelight relatively recently, its actuallymore than 25 years old. All the basic concepts of the object approach were introduced in theSimula programming language developed in Norway during the late 1960s. Modeling Physical ObjectsSimula, an acronym for "simulation language," was created to support computer simulations ofreal-world processes. The authors of Simula, O. J. Dahl and Kristen Nygaard, wanted to buildaccurate working models of complex physical systems that could contain many thousands ofcomponents.It was apparent even back in the 1960s that modular programming is essential for buildingcomplex systems, and modularization played a central role in the design of Simula. What isspecial about Simula is the way in which modules are defined. They are not based onprocedures, as they are in conventional programming. In Simula, modules are based on thephysical objects being modeled in the simulation.This choice makes a lot of sense because the objects in a simulation offer a very natural way ofbreaking down the problem to be solved. Each object has a range of behavior to be modeled, andeach has to maintain some information about its own status. The interaction of these objectsdefines the simulation. Why look for some other way to package procedures and data when thereal world has already organized them for you? Inside ObjectsThe concept of software objects arose out of the need to model real-world objects in computersimulations. An object is a software package that contains a collection of related procedures anddata. These procedures are often called methods to distinguish them from conventionalprocedures that arent attached to objects. In keeping with traditional programming terminology,the data elements are usually referred to as variables because their values can vary over time.Consider how you might represent an automated guided vehicle (AGV) in the simulation of afactory. The vehicle can exhibit a variety of behaviors, such as moving from one location toanother or loading and unloading its contents. It must also maintain information about itscharacteristics (pallet size, lifting capacity, maximum speed, and so on) as well as its currentstate (contents, location, orientation, and velocity).To represent the vehicle as an object, you would program its behaviors as methods and declarevariables to contain information about its characteristics and states. During the simulation, theobject would carry out its various methods, changing its variables as needed to reflect the effectsof its actions.The concept of an object is simple yet powerful. Objects make ideal software modules becausethey can be defined and maintained independently of one another, with each object forming aneat, self-contained universe. Everything an object "knows" is captured in its variables, andeverything it can do is expressed in its methods. L & T Infotech Page 16 of 87
    • OOPS Concepts using C++ Data AbstractionMessages and PolymorphismSending a MessageReal-world objects can exhibit an infinite variety of effects on one another—creating, destroying,lifting, attaching, buying, bending, sending, and so on. This tremendous variety raises aninteresting problem: How can all these different kinds of interactions be represented in software?The authors of Simula came up with an elegant solution to this problem: the message. The wayobjects interact with one another is to send messages asking objects to carry out their methods.A message is simply the name of an object followed by the name of a methodthe object knows how to execute. If a method requires any additional information in order toknow precisely what to do, the message includes that information as a collection of data elementscalled parameters. The object that initiates a message is called the sender of that message,and the object that receives the message is called the receiver.To make an automated vehicle move to a new location, for example, some other object mightsend it the following message: vehicle104 moveTo: binB7In this example, vehicle104 is the name of the receiver, moveTo is the method it is being askedto execute, and binB7 is a parameter telling the receiver where to go.In order for a message to make sense, the sender and receiver must agree on the format of themessage. This format is stipulated in a message signature that specifies the name of themethod to be executed and the parameters to be included. The moveTo message in thepresent example has a simple signature: Its name is moveTo, and it sends one parameter,which must be a location. The signature requires all senders of this message to identify it by thatname and to supply exactly one parameter of the correct type. In the context of objectinteractions, the shorter term signature is often used.An object program, then, consists of a number of objects interacting by sending messages to oneanother. Since everything an object can do is expressed by its methods, this simple mechanismsupports all possible interactions between objects. Responding to a MessageThe fact that methods are always associated with specific objects has an interesting side effectthat turns out to be highly advantageous. Different objects can respond to the same genericmessage, but each object can interpret the message in a different way. For example, a truckobject could implement its own version of the moveTo message, as could a ship, a train, anaircraft, a person, or anything else that moves. The way these real-world objects determine theirroutes, plan their movements, and carry out these movements differs radically. But all of themwould understand a common request to go to a specified destination.The ability of different objects to respond to the same message in different ways is calledpolymorphism, a Greek term meaning "many forms." The term can be intimidating, andpolymorphism is often considered an advanced concept in object technology. But the basic ideacouldnt be simpler: Each object can have a unique response to the same message.For most business people, polymorphism is so obvious that they have a hard time seeing what isso special about it. The reason is that human communication is naturally polymorphic. You canask a dozen people the same question and they could all determine their answers in a differentway, but the meaning of the question and the form of the answer would be the same across all of L & T Infotech Page 17 of 87
    • OOPS Concepts using C++ Data Abstractionthem. You dont much care about their private mental processes; you just want to hear theiranswers. All polymorphism does is allow object interactions to enjoy some of the same flexibilityas human interactions.The real surprise to most business people is that software doesnt normally work this way. Inconventional programming, procedures arent attached to objects, and every name must beunique. This means that you would have to call a different procedure for each kind of object inorder to get it to do something different. The simple moveTo message would turn into an open-ended list of specialized requests: AGVMoveTo, truckMoveTo, shipMoveTo,trainMoveTo, aircraftMoveTo, personMoveTo, and so on. It seems like a rathercumbersome way to build software in retrospect, but there was no other way to do it prior toobjects. L & T Infotech Page 18 of 87
    • OOPS Concepts using C++ Data Abstraction Data AbstractionSometimes, a simulation involves only a single example of a particular kind of object. It is muchmore common, however, to need more than one object of each kind. An automated factory, forexample, might have any number of guided vehicles. This possibility raises another concern: Itwould be extremely inefficient to redefine the same methods in every single occurrence of thatobject. Templates for ObjectsHere again, the authors of Simula came up with an elegant solution: the class. A class is asoftware template that defines the methods and variables to be included in a particular kind ofobject. The methods and variables that make up the object are defined only once, in the definitionof the class. The objects that belong to a class—commonly called instances of that class—contain only their own particular values for the variables.To continue the previous example, a simulated factory might contain many automated vehicles,each of which carried out the same actions and maintained the same kinds of information. Allsuch vehicles would be represented by a class called AutomatedVehicle, which would define itsmethods and variables. The actual vehicles would be represented by instances of this class, eachwith its own unique identity (vehicle101, vehicle102, vehicle103, and so on). Each of theseinstances would contain data values that represented its own particular contents and location.Another important function of classes is to specify the messages that objects of this kind willmake available to other objects. The set of messages an object commits to respond to is calledits message interface. This interface is specified as a collection of message signatures,each of which defines the name and parameters for a particular message. The only designrequirement placed on a class is that it provide a method to implement each message specified inits interface. The internals of the class are completely hidden behind this interface and mayinclude any number of variables as well as "invisible" methods that are used only by the objectitself.An object, then, is an instance of a particular class. Its methods and variables are defined in theclass, and its values are stored in the instance. To keep my explanations simple, I usually talkabout objects wherever possible, referring to classes and instances only when its important topoint out where the objects information is actually stored. For example, if I say that the objectvehicle104 has a method called moveTo, this is simply a more convenient way of saying thatvehicle104 is an instance of a class that defines a method called moveTo. Inheriting Class InformationSimula took the concept of classes one step further by allowing classes to be defined as specialcases of each other. If you needed to represent two different kinds of automated vehicle, youcould define one vehicle class in detail and then define the other as everything in the first classplus any additional methods and variables you wanted to add. Simply by declaring the secondclass to be a special case of the first, you would automatically give it access to everything the firstclass could do.The mechanism whereby one class of objects can be defined as a special case of a more generalclass is known as inheritance. Special cases of a class are commonly known assubclasses of that class; the more general class, in turn, is known as the superclass of itsspecial cases. In addition to the methods and variables they inherit, subclasses may define theirown methods and variables. They can also redefine any of the inherited methods, a techniqueknown as overriding. L & T Infotech Page 19 of 87
    • OOPS Concepts using C++ Data AbstractionThe class AutomatedVehicle could be broken down into two subclasses, PalletAGV andRollAGV, each of which will inherit the general characteristics of the parent class. Eithersubclass could establish its own special characteristics by adding to the parents definition or byoverriding its behavior.Because classes define message interfaces, these interfaces are also inherited by theirsubclasses. This means that all the subclasses of a given class are guaranteed to respond to anymessages that can be handled by the parent class. This is an extremely useful property becauseit allows us to treat all the specialized forms of a class as equivalent at the general level. if we define a Product class that includes messages for production, pricing, and shipping in itsinterface, then we have an ironclad guarantee that all the subclasses of Product will implement allthese messages. Any given subclass may use the implementations it inherits from Product, or it may define its ownimplementations; it doesnt matter to us at this level. What matters is that we can design oursystem to produce, price, and ship an unlimited variety of products without having to beconcerned about how each specialized type carries out these tasks. Hierarchies of ClassesClasses can be nested to any degree, and inheritance will automatically accumulate downthrough all the levels. The resulting treelike structure is known as a class hierarchy. A classcalled Part, for example, could be broken down into special kinds of parts such as Motor,Chassis, Connector, and so on. Motor, in turn, could be divided into DriveMotor andSteppingMotor, each of which could be broken down further as needed. An instance of, say,VariableSpeedDriveMotor would inherit all the characteristics of the Part class, as wellas those of Motor and DriveMotor.Class hierarchies increase the ability of objects to reflect the way we view the real world. Humanknowledge is often organized in a hierarchical manner, relying on generic concepts and theirrefinement into increasingly specialized cases. Object technology takes the same conceptualmechanisms we employ in everyday life and uses them to build sophisticated yet understandablesoftware systems. This is particularly evident in the generalization and specialization provided bya class hierarchy.Natural Building BlocksObject programming is often said to be more natural than traditional programming, and this is trueon a couple of different levels. On one level, object programming is more natural because itallows us to organize information in ways that are familiar to us, as illustrated in the classhierarchies described in the preceding chapter. On a deeper level, it is more natural in that itreflects natures own techniques for managing complexity. This chapter opens and closes withbrief looks at the structure of living organisms to establish a framework for understanding theadaptive nature of objects. The remainder of the chapter looks at the actual technologyunderlying objects and demonstrates why objects provide a better foundation for flexible businesssystems.The basic building block out of which all living things are composed is the cell. Cells are organic"packages" that, like objects, combine related information and behavior. Most of the information is L & T Infotech Page 20 of 87
    • OOPS Concepts using C++ Data Abstractioncontained in protein molecules within the nucleus of the cell. The behavior, which may range fromenergy conversion to movement, is carried out by structures outside the nucleus.Cells are surrounded by a membrane that permits only certain kinds of chemical exchanges withother cells. This membrane protects the internal workings of the cell from outside intrusion, and italso hides the complexity of the cell and presents a relatively simple interface to the rest of theorganism. All interactions between cells take place through chemical messages recognized bythe cell membrane and passed through to the inside of the cell.This message-based communication greatly simplifies the way cells function. The cells dont haveto read each others protein molecules or control each others structures to get what they needfrom each other. All they do is send the appropriate chemical message, and the receiving cellresponds accordingly.The cell is truly a universal building block. All cells share a common structure and operateaccording to the same basic principles. Within this generic structure, however, infinite variability ispossible. For example, plant cells have a hard outer wall to make them rigid; blood cells aremobile and specialized to transport gases; and muscle cells are able to distort their shape toperform mechanical work. But this tremendous variability is not chaotic; its all neatly organized—or "classified"—in a hierarchy of specialized types and subtypes. The accompanying diagramgives the merest hint of the depth and breadth of this diversity.Objects, as defined in object technology, have many of the essential characteristics of living cells.A closer look inside the structure of an object reveals some of these similarities. The Anatomy of an ObjectPackaging related data and procedures together is called encapsulation. As you can seefrom the structure of a cell, encapsulation is an idea that has been around for a very long time.The fact that it has worked so well in natural systems suggests that we may be on the right trackwith this mechanism!Encapsulating ObjectsThe key to object encapsulation is the message interface. As the accompanying illustrationindicates, this interface surrounds the object and acts as the point of contact for all incomingmessages. The message interface serves the same function as the membrane of a cell—toprovide an essential barrier between the internal structure of the object and everything that liesoutside the object. Like a cells membrane, the message interface ensures that all interactionswith the object take place through a predefined system of messages that the object is guaranteedto understand and handle correctly.The encapsulation mechanism of object technology is a natural extension of the information-hiding strategy developed in structured programming, as described in the software constructionprimer in the appendix. Object technology improves on this strategy with better mechanisms topull the right kinds of information together, including all related data and procedures, and hidetheir details more effectively. There is simply no way for any other object to access the data ormethods hidden behind an objects interface.Data inside an object is accessed only by methods that implement the objects interface. Just ascells dont "read" each others protein molecules, objects dont touch each others data structures.Rather, objects send each other messages that call methods into action. These methods, in turn,access the required variables. L & T Infotech Page 21 of 87
    • OOPS Concepts using C++ Data AbstractionThis same mechanism also controls access to methods that are used only by the object itself. Forexample, determining the price of a product can require a complex set of interacting calculations,each of which could involve a number of methods and variables. Allowing other objects to triggerthese internal methods directly could create havoc within the object. To prevent this kind ofdisruption, the object hides all of its pricing logic behind an interface that only allows other objectsto inquire about its current price or set a new base price for future calculations.Message interfaces offer two important kinds of protection. First, they protect an objects internalcomponents from being corrupted by other objects. If other objects had direct access to anobjects variables and internal methods, eventually one of these other objects would handle avariable incorrectly or call the wrong method and damage the object. An object protects itselffrom this kind of error by hiding its variables and internal methods behind its message interface.The second and less obvious kind of protection works in the opposite direction. By hiding itsvariables and internal methods, an object protects other objects from depending on its internalstructure. For example, they are spared having to keep track of each variables name, the type ofinformation it contains, the amount of space it takes up in storage, and a host of other details thatwould complicate all their procedures for accessing an objects variables. With encapsulation, anobject only needs to know how to ask another object for information. All the details about how thatinformation is stored are neatly tucked out of sight behind the message interface. Facilitating ChangesIn addition to simplifying the interactions between objects, encapsulation also makes themodification of objects much easier than it would otherwise be. Hiding all the implementationdetails of an object inside its message interface allows the object to be modified extensively inresponse to changing business needs. As long as the portions of the message interface currentlyin use by other objects remain intact, internal modifications wont require changes to any otherpart of the system.Consider making the following change to the AutomatedVehicle class discussed in the precedingchapter. When you first defined the class, you declared a fixed carrying capacity for the vehicleand stored it in a variable. Subsequently, you discover that you need to perform one of severaldifferent calculations to determine capacity depending on the kind of load involved. To make thischange, you remove the capacity variable and define a set of internal methods for calculatingcapacity. Each of these methods might require one or more variables, so you add these variablesas needed. Finally, you perform the switchover—you change the load method to call your newmethods to verify that the vehicle can handle the load.In a conventional system, this change would require significant restructuring. You would need torewrite all the subroutines in the system that dealt with vehicle carrying capacities to make themcall functions rather than access a data value. You would have to repeat the same selection logicfor using the correct calculation in all the affected subroutines. Every routine that needed todetermine capacity would have to be changed; all these changes would have to be madecorrectly; and all these changes would have to be synchronized to occur at the same time.Anything less would result in a broken system.In short, modeling the automated vehicle as a self-contained object allows you to restrict all yourchanges to a single object. There is no duplication of the selection logic, no doubt about whetherall the necessary subroutines have been modified, and no synchronization problem. Just modifythe AutomatedVehicle class, and youre done. All other objects continue to interact with it just asthey did before. You have made a fundamental change in how information is handled, convertingit from data to a collection of specialized procedures, and none of the other objects even "know"that a change has taken place! L & T Infotech Page 22 of 87
    • OOPS Concepts using C++ Data Abstraction Using Composite InterfacesIn most cases, the message interface surrounding an object is a single, unbroken surface.However, it is possible to divide this interface into segments by first defining some simplerinterfaces and then combining them into a composite interface for a particular object. Thistechnique can be used to further protect the internals of objects by giving other objects accessonly to selected components of the objects overall interface.For example, the message interface for a Product object might be composed of four componentinterfaces: one for defining the product, a second for producing it, a third for selling it, and a fourthfor shipping it. Each of these interfaces deals with very different aspects of the product and wouldbe accessed by different parts of the companymanufacturing interface. Only the manufacturing object would be affected by Notice that thesegmentation of the message interface increases the two kinds of protection described earlier.First, other objects cant access methods they have no right to invoke. A Sale object, for example,couldnt get access to the manufacturing interface to change the production schedule for aproduct. Second, other objects are protected from changes to interfaces they dont have accessto. If a change in the way a product was manufactured required changes to the interface of theProduct object, these changes would be restricted to the the changes—the marketing, sales, andshipping objects would be completely unaffected. Objects Inside Other ObjectsThe variables contained within objects can be used in two different ways. First, they can be usedto store data values, such as the number 12.5 or the text string "Approved." More interestingly,they can contain references to other objects. The form of the reference is an implementationdetail and varies considerably among object systems. The important point is that the referenceheld by a variable provides the containing object with a "handle" or effective address for thecontained object, allowing the containing object to manage its component by sending itappropriate messages.Objects that contain other objects are called composite objects. Composite objects areimportant because they can represent far more sophisticated structures than simple objects can.An aircraft consists of wings, engines, and other components that are far too complex to berepresented as simple numbers or text.The objects contained in composite objects may themselves be composite objects, and thisnesting can be carried out to any number of levels. The major components of an aircraft, forexample, are very complex objects in their own right. In any reasonable model of an aircraft, eachof these components would be represented by a composite object that, in all likelihood, would becomposed of still more composite objects, and so on. Collections of ObjectsThere is a special kind of class, often called the collection class, that can be found in the classlibrary shipped with most commercial languages. As the name suggests, the basic function of acollection is to gather together objects that must be managed as a group. In a model of anaircraft, for example, we wouldnt create a separate named variable for every seat object. Rather, L & T Infotech Page 23 of 87
    • OOPS Concepts using C++ Data Abstractionwe would gather all the seat objects into a collection and place a reference to that collection in asingle variable called Seats.This example illustrates how collections can simplify composite objects while also making themmore flexible. In addition to reducing several hundred variables down to one, the Seats collectionallows us to vary the number of seats without reprogramming the model. In addition, thecollection makes it easier to carry out group operations on the seats, such as scanning them tosee which ones are currently reserved. The method that performed this task would repeatedly askthe collection for its next element until the collection reported that it was empty. This is muchsimpler than writing a method to examine several hundred named variables. The Benefits of CompositionBecause objects can be composed of other objects, object languages can represent real-worldobjects at the level we naturally think about them. Even a complex, deeply nested structure suchas a jet aircraft can be treated as a single, unified object. And since that complex object can haveits own behavior, other objects can use it with very little awareness of its internal complexity. Thisis a good example of how object technology can bring order to large-scale systems. Compositeobjects not only keep simple things simple, they can make complex things simple as well.Composite objects offer another important advantage—they lay the foundation for a mechanismcalled delegation, in which an object assigns a task to another object. Delegation is a verycommon pattern in object designs because it supports the division of labor encouraged byobjects.Determining the charge for a telephone call is a fairly complex process because the charge canbe based on physical distance, arbitrary zones, or other factors. Rather than building thiscomplexity directly into a Call object, we could give Call a component Rater to perform thistask. Then, the Call could simply send its end points and duration to the Rater and get back theappropriate charge. Because it would not know anything about how the Rater did its work, anykind of Rater could be placed in the Call without affecting the behavior of the Call object inany way. The hidden implementationIt is helpful to break up the playing field into class creators (those who create new data types) andclient programmers (the class consumers who use the data types in their applications). The goalof the client programmer is to collect a toolbox full of classes to use for rapid applicationdevelopment. The goal of the class creator is to build a class that exposes only what’s necessaryto the client programmer and keeps everything else hidden. Why? Because if it’s hidden, theclient programmer can’t use it, which means that the class creator can change the hidden portionat will without worrying about the impact to anyone else. The hidden portion usually representsthe tender insides of an object that could easily be corrupted by a careless or uninformed clientprogrammer, so hiding the implementation reduces program bugs. The concept ofimplementation hiding cannot be overemphasized.In any relationship it’s important to have boundaries that are respected by all parties involved.When you create a library, you establish a relationship with the client programmer, who is also aprogrammer, but one who is putting together an application by using your library, possibly to builda bigger library. L & T Infotech Page 24 of 87
    • OOPS Concepts using C++ Data AbstractionIf all the members of a class are available to everyone, then the client programmer can doanything with that class and there’s no way to enforce rules. Even though you might really preferthat the client programmer not directly manipulate some of the members of your class, withoutaccess control there’s no way to prevent it. Everything’s naked to the world.So the first reason for access control is to keep client programmers’ hands off portions theyshouldn’t touch – parts that are necessary for the internal machinations of the data type but notpart of the interface that users need in order to solve their particular problems. This is actually aservice to users because they can easily see what’s important to them and what they can ignore.The second reason for access control is to allow the library designer to change the internalworkings of the class without worrying about how it will affect the client programmer. Forexample, you might implement a particular class in a simple fashion to ease development, andthen later discover that you need to rewrite it in order to make it run faster. If the interface andimplementation are clearly separated and protected, you can accomplish this easily and requireonly a relink by the user.C++ uses three explicit keywords to set the boundaries in a class: public, private, andprotected. Their use and meaning are quite straightforward. These access specifiers determinewho can use the definitions that follow. public means the following definitions are available toeveryone. The private keyword, on the other hand, means that no one can access thosedefinitions except you, the creator of the type, inside member functions of that type. private is abrick wall between you and the client programmer. If someone tries to access a private member,they’ll get a compile-time error. protected acts just like private, with the exception that aninheriting class has access to protected members, but not private members.In C++, an object is an instance of a user-defined class. For example, a person would be aclass, but Tom (the person) would be an instance of a person class. Objects can hide their dataand implementation.Only the information provided by the class (through class members) is accessible, and everythingelse is hidden.You can control access to a classs data members and functions, using the following accessspecifiers: • public • private • protectedPublic members are accessible outside the class, private and protected members are accessiblewithin the class, and protected members are also available to subclasses.By default, all members of a class are private.Making data members private and controlling access through public member functions helps toensure data integrity in your programs. Private data members can be manipulated only by othermember functions of the class.This means that a classs implementation may change internally, but its public interface need not,minimizing the change to the class. Attributes and behaviorsA class can represent an object in the real world, such as a radio.In the class, you can declare the data that describes that object. And you can declare theoperations that are possible on that data. L & T Infotech Page 25 of 87
    • OOPS Concepts using C++ Data AbstractionA radio has attributes and behavior.A radio has attributes, such as the volume at which it plays and the frequency to which it is tuned.These attributes are analogous to a classs data members.And it has behavior or operations (member functions), such as changing volume and tuning to astation.The real-world object – the radio – helps illustrate the concepts of encapsulation and data hiding.You can change the volume and tune to a station without having to know how the radio interpretsand acts on those commands. Just as the radio is held within one unit, the data and functions aresaid to be encapsulated. The attributes of volume and frequency are declared as private.In other words, the way in which the radio implements them is a matter of concern only tomembers of the radio class, which are its internal circuits and controls.In the example of data hiding, Change_volume() and Tune_station() functions are declared aspublic, but "external" elements (such as the person listening to the radio) can use them.public: void Change_volume (short nNewVolume) void Tune_station (void); C++ program generationReleased in 1985, C++ is an object-oriented programming language created by BjarneStroustrup. C++ maintains almost all aspects of the C language, while simplifying memorymanagement and adding several features - including a new datatype known as a class (you willlearn more about these later) - to allow object-oriented programming. C++ maintains the featuresof C which allowed for low-level memory access but also gives the programmer new tools tosimplify memory management.C++ is a general-purpose, strongly-typed programming language based on the C language.i.eEverything must be declared before it is used, both variables and functions. C ++ has a few basicbuilding blocks, which can be grouped into types, operations and functions. It is the result ofresearch into extending C to incorporate the principles of object-oriented programming.Thisresearch was carried out by Dr Bjarne Stroustrup of AT&T Bell Laboratories in the early 1980s.C++ development systems generally consist of several parts: • a text editor • a C++ compiler/linker • the C standard library • class libraries • a debugger L & T Infotech Page 26 of 87
    • OOPS Concepts using C++ Data AbstractionThese components are sometimes integrated into a single development environmentThe steps involved in generating a C++ program include • editing • preprocessing • compiling • linking EditingYou write the source code for a C++ program in the text editor, after which it is translated intomachine code by the compiler. PreprocessingThe C++ preprocessor executes before the compilers translation phase begins. Thepreprocessor obeys commands called preprocessor directives, which indicate that certainmanipulations are to be performed on the program before compilation. These manipulationsusually involve including the contents of other source code files within the current file, andperforming various text replacements.The preprocessor also performs conditional compilation, that is, it allows different sections ofcode to be compiled depending on preprocessor definitions. A common preprocessor directive is#include. Lines beginning with the pound sign (#) are processed by the preprocessor before theprogram is compiled. CompilingThe compiler translates the C++ program into machine code, also referred to as object code.Programs dont always work the first time. Each of the steps can fail because of various types oferrors. The compiler reports warnings and errors.Only errors prevent a program from compiling. The compiler indicates where each error occurred,which helps you in locating and rectifying the errors. LinkingThe fourth step is called linking. C++ programs typically contain references to data and tofunctions defined elsewhere.These may be defined in the C++ standard libraries, the private libraries of programmers workingon a project, or in third-party libraries.A linker links the object code with the code for the missing functions to produce an executablefile. When a compile/link succeeds, an executable module is produced.Using a debugger you can follow the source code as its machine code counterpart is executing.This enables you to step through the code to ensure that it is doing what you expect. Also, youcan trace a bug or an unexpected runtime action in the program. L & T Infotech Page 27 of 87
    • OOPS Concepts using C++ Data Abstraction Comments and functionsLets consider how to develop a simple C++ program.You can use comments to add explanatory notes to a program.A line beginning with // indicates that a comment follows.Comments are ignored by the compiler.// Take in a students name, grade and percentage#include <iostream.h>main(){ }A comment that begins with // is called a single-line comment because the comment ends at theend of the line. Comments can also be multi-line, but they use different symbols to represent this.The following code contains a preprocessor directive. This line tells the preprocessor to includethe contents of the input/output stream header file iostream.h in the program.This file must be included for any C++ program that outputs data to the screen or receives inputdata from the keyboard using the proposed C++ standard stream input/output.// Take in a students name, grade and percentage#include <iostream.h>main(){}C++ programs contain one or more functions, one of which must be called main(). Theparentheses after main() indicate that it is a function. C++ programs begin executing at main() bydefault, even if it isnt the first function in the program.Some C++ compilers allow a different entry point function to be specified in the compiler settings.The left brace, {, must begin the body of every function and the right brace, }, must end eachfunction.// Take in a students name, grade and percentage#include <iostream.h>main(){} L & T Infotech Page 28 of 87
    • OOPS Concepts using C++ Data Abstraction VariablesYou store data in a C++ program by assigning values to variables.A variable is a name that is associated with memory set aside for storing the variables value.char sStudent[50];char cGrade;short nPercentage;Every variable has a name, a type, a value, a size and a lifetime.All variables must be declared with a name and a data type before they can be used in aprogram.A variable name is any valid identifier. Variable names must be unique within their scope.An identifier is a series of characters consisting of letters, digits, and underscores (_).Identifiersare case sensitive and cannot contain spaces.An identifier may not begin with a digit.1A and A 1 are not legitimate identifiers.C++ doesnt always limit the length of identifiers, but some implementations of C++ do restricttheir length. Its good programming practice to pick meaningful names for identifiers.For example, it is much clearer to call a variable that represents a students name sStudent ratherthan s1.char sStudent[50];char cGrade;short nPercentage;A data type defines the sort of values a variable can store.char sStudent[50];char cGrade;short nPercentage; Fundamental data types include • int • float • char int Variables of type int hold integer values, that is, whole numbers such as 7, -35, 0, and 23961. The int data type has a range which is system dependent. float You use the float data type to represent any number that might be expressed using a decimal point, that is, a real number, for example 2.7, 4.9, and -31.2. char L & T Infotech Page 29 of 87
    • OOPS Concepts using C++ Data Abstraction A character in C++ is represented by its integer character code. The char data type stores an integer character code. The char value range is from -128 to +127. char sStudent[50]; char cGrade; short nPercentage; int x; float y;You can assign a value to a variable using the assignment operator, =.In the following code the assignment statement calculates the sum of the variables integer1 andinteger2 and assigns the result to the variable "total".Most calculations are performed in assignment statements.total = integer1 + integer2;Variables declared inside functions are local to those functions.Variables declared outside functions, including main(), are global.int x=20;main() { int x; x=10; }Local variables may be referenced only by statements that are inside the block in which thevariables are declared.This feature is known as a variables scope. Statements outside the variables scope have no access to that variable, and so cant use its value.int x=20;main() { int x; x=10; }Local variables are usually stored on the stack.The stack is a region of memory that holds,among other things, local variables. L & T Infotech Page 30 of 87
    • OOPS Concepts using C++ Data Abstractionint x=20;main() { int x; x=10; }When a function begins, it allocates space on the stack to hold its local variables.This space exists only while the function is active. After the function returns, it deletes theallocated stack space, thus discarding any values stored there.int x=20;main() { int x; x=10; }Global variables exist for the duration of a program. You create global variables by declaringthem outside any function.Any function may access them, regardless of what block of code thatexpression is in.int x=20;main() { int x; x=10; }This fragment shows that x, within main(), is currently in scope. Also, the global x is not modifiedby the assignment.int x=20;main() { int x; x=10; }You can allocate space for variables at runtime.These are called dynamic variables. They arecreated on demand and stored in a block of free memory known as the heap. cout and cin stream objectsThe cout and cin stream objects facilitate interaction between the user and the computer. • cout << "Enter student name:"; • cin << sStudent;cout << "Enter student name:"; L & T Infotech Page 31 of 87
    • OOPS Concepts using C++ Data AbstractionYou use the output stream object, cout, to display on the screen the text string contained betweenthe quotation marks. The stream insertion operator (<<) indicates that the value to the right ofthe operator is inserted in the output stream.cin << sStudent;You use the input stream object, cin, and the stream extraction operator, <<, to accept input fromthe user. The cin object takes input from the standard input stream, which is usually thekeyboard, and assigns the input to the variable. L & T Infotech Page 32 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism Constructors and DestructorsConstructor functions and destructor functions are special functions available in the C++programming language.A constructor function, which is a member function of a class, caninitialize the members of that class.A destructor function is called when program execution leaves the scope in which an instance ofa class exists.The purpose of a destructor is to reclaim an objects memory space (before the system reclaimsit) so that it may hold new objects.Because constructors and destructors take care of initializationand destruction responsibilities in a program, these two member functions complement eachother.Often, it is necessary to initialize various data members with specific data values.Using a constructor function, you can initialize various variables when the object is created.UsingC++, you can pass arguments to an objects constructor function, which are known only when theobject is created.Lets say that you are defining a constructor function of an object called Time, which has datamembers x and y.Time:: Time (short x=0, short y=0){ nX=x; nY=y; };You can set default values by coding the definition header.Now, if Time is constructed with no parameters being passed to it, nX and nY will be zero. Declaring a constructor and destructorLets say that you want to add constructor and destructor functions to the program implementinga simple time object.First, you need to add the constructor and destructor functions to the class declaration.#include <iostream.h>class CClock{private: short nHours,nMinutes,nSeconds;public: CClock(); L & T Infotech Page 33 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};The constructor function name is always that of the class,class CclockIt is added to the list of member functions.Destructors also have the same name as the class to which they belong, but they have a (~) tildein front of them, for example~CClock();In this case, the destructor is also added to the list of member functions.#include <iostream.h>class CClock{private: short nHours,nMinutes,nSeconds;public: CClock(); ~CClock(); void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};It does not matter whether you declare the constructor or the destructor first.Both the constructor and the destructor are declared as public member functions. L & T Infotech Page 34 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismBecause the constructor and destructor are public, they will be called implicitly in the functionmain(),void main(void)Either the constructor or destructor could have been declared as private. If so, class objectswould be created or destroyed only from within another member function.The only instance inwhich you could create or destroy a class object from within another function is if the memberfunctions were static.Constructors and destructors are usually declared as public. Adding a constructor functionYou need to define the constructor and destructor functions.You begin the constructor definition with the definition header. In this case, you useCClock::CClock()The Cclock(); line of code specifies that the constructor function CClock is a member function ofthe class CClock.The braces, which are to contain the specifications of the definition, are below the header.public: CClock(); ~CClock(); void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};CClock::CClock(){}Next you initialize the data members nHours, nMinutes, and nSeconds within the braces.Initializing the data members in the constructor means that you do not need to initialize the datamembers in the definition of the InitTime() function. L & T Infotech Page 35 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism{ nHours=0; nMinutes=0; nSeconds=0;}Initializing the data members in the constructor definition means that they will be automaticallyinitialized in the function main() of the program. Adding a destructor functionYou begin the destructor definition by specifying that it is a member of the CClock class.In this case, the definition isCClock::~CClock()public: CClock(); ~CClock(); void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};CClock::CClock(){ nHours=0; nMinutes=0; nSeconds=0;}CClock::~CClock(){}Then, within the braces, you define the destructor to output a message onscreen.Now destroying the clock objectTo do so, you use the cout statement by enteringcout<< "Now destroying clock object"; L & T Infotech Page 36 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphismpublic: CClock(); ~CClock(); void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};CClock::CClock(){ nHours=0; nMinutes=0; nSeconds=0;}CClock::~CClock(){ cout<<"Now destroying the clock object";}Constructors and destructors, like ordinary member functions, may contain any type of code.Nevertheless, it is good practice to use them only for their intended purposes and to placeextraneous code in other functions.Data members of a class cannot be initialized in the class definition, but they can be initialized ina constructor of the class. L & T Infotech Page 37 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism InheritanceThe two primary characteristics of object-oriented programming are inheritance andpolymorphism. Inheritance allows us to group classes into families of related types, allowing forthe sharing of common operations and data.Inheritance defines a parent/child relationship. The parent defines the public interface and privateimplementation that are common to all its children. Each child adds to or overrides what it inheritsto implement its own unique behavior. An AudioBook child class, for example, in addition to thetitle and author it inherits from its parent Book class, introduces support for a speaker and a countof the number of cassettes. In addition, it overrides the inherited check_out() member functionof its parent.In C++, the parent is called the base class and the child is called the derived class. Therelationship between the parent or base class and its children is called an inheritance hierarchy.At a design review meeting, for example, we might say, "We intend to implement an AudioBookderived class. It will override the check_out() method of its Book base class. However, it willreuse the inherited Book class data members and member functions to manage its shelf location,authors name, and title."When classes are similar, it is preferable to define them in terms of their common characteristics,rather than duplicate effort by defining individual classes in full.The mechanism provided by C++ to allow classes to reuse declarations made in other classes iscalled inheritance.When coding a new class, instead of creating new data members and new member functions, aprogrammer can specify that the new class should inherit the data members and memberfunctions of a previously defined base class.This is called inheritance, and the new class created is called a derived class.Inheritance allows programmers to reuse software that has proven itself to work efficiently, and itis therefore a major addition to the C++ programming language.Every derived class can potentially be a base class to another derived class.Inheritance in computer programming works in much the same way as it does in real life.You inherit certain characteristics from your parents.Similarly, a derived class inherits certain characteristics from its base class.The two types of inheritance are • single inheritance • multiple inheritance L & T Infotech Page 38 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismWhen a derived class inherits properties of only one base class, this is called single inheritance.When a derived class inherits properties of more than one base class, this is called multipleinheritance. When a derived class is created, it generally contains data members and functionmembers additional to those in its base class.A derived class tends to be bigger in size but more specific than its base class.Derived classesusually represent a smaller group of objects than their base classes.The real advantage of inheritance is its ability to define subtle changes to the features inheritedby a derived class for one or more base classes.Every object of a derived class is also an object of that derived classs base class.A base-class object is not also an object of that base-classs derived class.The objects of aderived class are, on the other hand, objects of its base class. Base and derived classesLets say that you are examining shapes as a class, particularly a derived class called triangle.Although a triangle is a shape, not all shapes are triangles.Conversely, considering triangles as a derived class of the shape base class, a triangle is ashape, and therefore the object of the derived class is also an object of the base class. A derivedclass can access the public members of its base class.And derived classes receive favored treatment over other functions in accessing protected baseclass members. A derived class cannot access the private members of its base class.These can be accessed by the derived class through access functions provided in the baseclasss public or protected interfaces.When deriving a class, you must specify which of the base class members you want the derivedclass to inherit.In the code Class B: public A,the derived class B is set to inherit the protected members of thebase class A.The derived class will also inherit the public functions by default.A derived class can often inherit unwanted or inappropriate functions from the public interface ofits base class.This is not a major problem because the unwanted inheritance can be redefined in the derivedclass to have an appropriate implementation. Deriving a classLets say that you want to write a C++ program that implements a time object with an alarmmechanism to demonstrate how C++ derived classes inherit the characteristics of base classes.The steps involved in the program are • include the header file • declare the base class and its member functions L & T Infotech Page 39 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism • declare the derived class and its data members and member functions • define the functions of the base class • define the member functions of the derived class • code the main () function • call the functionFirst you must include the header file <iostream.h> in the program.#include<iostream.h>Next, you declare the class CClock with the private data members nHours, nMinutes, andnSeconds.class CClock{private: short nHours, nMinutes, nSeconds;Now you declare the member functions of the base class – CClock, ~CClock, InitTime(),AdvanceHours(), AdvanceMinutes(), and AdvanceSeconds() – for public access.public: CClock(); ~CClock(); void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};The base class (CClock) is placed to the right of the colon (:), whereas the derived class(CAlarmClock) is placed on the left.It means that a derived class is to inherit the public elements of the base class.#include<iostream.h>class CClock{ private: short nHours, nMinutes, nSeconds; public: CClock(); ~CClock(); L & T Infotech Page 40 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};class CAlarmClock:public CClock{A base class can be either a direct base class or an indirect base class. The direct classsname is listed in the header of the class, whereas an indirect classs name is inherited fromtwo or more levels up the class hierarchy.Now you declare the data members of the derived class.#include<iostream.h>class CClock{ private: short nHours, nMinutes, nSeconds; public: CClock(); ~CClock(); void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};class CAlarmClock:public CClock{private: short nAlarmHours, nAlarmMinutes;The data members nAlarmHours and nAlarmMinutes are declared as private.include<iostream.h>class CClock{ private: short nHours, nMinutes, nSeconds; public: L & T Infotech Page 41 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism CClock(); ~CClock(); void InitTime(void); void AdvanceHours(void); void AdvanceMinutes(void); void AdvanceSeconds(void);};class CAlarmClock:public CClock{ private: short nAlarmHours, nAlarmMinutes;Declare the member functions of the class CAlarmClock.Declare the member functions for public member access.The member function CAlarmClock is a constructor function.The member function ~CAlarmClockis a destructor function.You assign the third member function SetAlarm() the parameters qHoursand qMins.This means that the function can be called only if values are specified for qHours and qMins.public:CAlarmClock();~CAlarmClock();void SetAlarm(short qHours, short qMins);};Now you need to define the member functions that you declared.You define the constructor function CClock() to automatically initialize the functionInitTime()CClock::CClock(){ InitTime();}Define the destructor function to display "Now destroying clock object" on screen.CClock::~CClock(){ cout<<"Now destroying clock object";} L & T Infotech Page 42 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismNow you define the function InitTime() to attribute a zero value to the class CClock datamembers.void CClock::InitTime(void){ nHours=0; nMinutes=0; nSeconds=0;}In the definition of the function AdvanceHours(), you reset the value of nHours to zero when itsvalue is no longer less than 23.void CClock::AdvanceHours(void){ nHours++; if(nHours>23){ nHours=0; }}Next you define the AdvanceMinutes() member function to reset the value of nMinutes to zerowhen its value is no longer less than 59.void CClock::AdvanceMinutes(void){ nMinutes++; if(nMinutes>59){ nMinutes=0; }}Now you define the member function AdvanceSeconds() to reset the value of nSeconds to zerowhen its value is no longer less than 59.void CClock::AdvanceSeconds(void){ nSeconds++; if(nSeconds>59){ nSeconds=0; }}Having defined the functions of the CClock class, you now need to define the member functionsdeclared in the class declaration of CAlarmClock.First define the constructor function CAlarmClock().CAlarmClock::CAlarmClock(){} L & T Infotech Page 43 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismNow define the destructor function ~CAlarmClock().CAlarmClock::~CAlarmClock(){}Now define the functionSetAlarm(short qHours, short qMins)In the header of this definition, define the function SetAlarm(short qHours, shortqMins) as a member function of the class CAlarmClock.void CAlarmClock::SetAlarm(short qHours, shortqMins)Then you equate the value of nAlarmHours to that of qHours and the value ofnAlarmMinutes to that of qMins.{ nAlarmHours=qHours; nAlarmMinutes=qMins;}Now that the base clas and the derived class have been declared and defined then, codethe main() function of the program.First you name the function main() then you declare an object of classCAlarmClock[CAlarmClock] and call it clock.CAlarmClock() [CAlarmClock] is a derived class of CClock.CAlarmClock::CAlarmClock(){}CAlarmClock::~CAlarmClock(){}void CAlarmClock::SetAlarm(short nHours, short nMins){ nAlarmHours=qHours; nAlarmMinutes=qMins;}void main(void){ CAlarmClock clock;The C++ feature of inheritance has allowed CAlarmClock to inherit all the publicly defined L & T Infotech Page 44 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphismmembers of the class CClock.Therefore, the function main() has full access to the publiclydeclared members of the classCClock.CAlarmClock::CAlarmClock(){}CAlarmClock::~CAlarmClock(){}void CAlarmClock::SetAlarm(short nHours, short nMins){ nAlarmHours=qHours; nAlarmMinutes=qMins;}void main(void){ CAlarmClock clock;Next, type the code to call the SetAlarm() function.In this case, the code isClock.SetAlarm (10, 50);The qHours is assigned the value of 10, and qMinutes is assigned the value of 50 in thefunction SetAlarm() to the set function.The alarm is therefore set to sound when 10 hours and 50 minutes have elapsed.clock.SetAlarm(10, 50);The nested loop in the main method is made up of three for statements.The loop is designed to operate a 24-hour clock object with hours, minutes, and seconds.for(short h==0;h<24;h++){ clock.AdvanceHours(); for(short m==0;m<60;m++){ clock.AdvanceMinutes(); for(short s==0;s<60;s++){ clock.AdvanceSeconds(); } } } L & T Infotech Page 45 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism Multiple inheritanceSingle inheritance means that a derived class inherits elements from only one base class.Multiple inheritance, on the other hand, means that a derived class inherits elements from morethan one base class.Multiple inheritance was included in the C++ Release 2.0 languagedefinition.The addition was necessary to reflect and accommodate real-world objects, which programmerswere trying to simulate.Lets say that youre mixing different colors of paint from the three primary colors red, blue, andyellow.By mixing red and blue you get purple.purple is a derived class from the two base classes red and blue.By mixing red, blue, and yellow you get brown.In this case, brown has inherited elements of the base classes red, blue, and yellow.In the example of multiple inheritance, brown is the derived class, and red, blue, and yellow arethe multiple base classes.A specific syntax is used to indicate multiple inheritance when the derived class is called.After the colon (:) inheritance indicator in the derived class declaration, the base classes beinginherited are listed and separated by a comma.In the code, derived class D inherits the member functions and data members of base classes A,B, and C.Class D: public A, public B, public C{};As in single inheritance, classes derived from multiple base classes inherit only member functionsand data members to which they have access. Coding multiple inheritanceLets say that you want to code a program in C++ that uses multiple inheritance.First, you need to make the header file <iostream.h> available to the program. L & T Infotech Page 46 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismThe syntax for including the header file <iostream.h> in the program is#include<iostream.h>Next you declare the class A.class A{ public: int x; void make_x(int i);};Now declare the data members and member functions of class A for public member access.Next you declare class B.Also declare the data member int y and the member function void make_y(int i) for public memberaccess.class B{ public: int y; void make_y(int i);};Next, declare the class C, which is a derived class with class A and class B as its multiple baseclasses.In the syntax, class C inherits all the public members of classes A and B.Also, declare the function make_xy() for public member access.#include<iostream.h>class A{ public: int x; void make_x(int i);};class B{ public: int y; void make_y(int i);}; L & T Infotech Page 47 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphismclass C:public A, public B{ public: int make_xy(void);Next type the class A function make_x()Type void A::make_x(int i) and then press Enter.Now define the class B function make_y().Define the function make_y() to set the value of y to the value of i by coding y=i; between braces.void A::make_x(int i){ x=i;}void B::make_y(int i){ y=i;}Next define the class C function make_xy().In the definition header, you code void in the brackets.This specifies that the function takes noparameters.By coding return x*y; between the braces, you define the function make_xy() to return the valueof x multiplied by y when the function is executed.void A::make_x(int i){ x=i;}void B::make_y(int i){ y=i;}void C::make_xy(void){ return x*y;}Now you code the programs main() function.main (void){ C i; L & T Infotech Page 48 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism i.make_x(10); i.make_y(12); cout<<i.make_xy();}On the first line – C i; – in the main() function, you call the class C and give it the variable name i.On this line, you call the function i.make_x(10).You can call the make_x() function even though it was declared in the class A declaration, andnot in the class C declaration.This is because class C inherits the member functions of class A.Next type the code to call the function make_y().Type i.make_y(12); and then press Enter.The code – i.make_y(12) – calls the function make_y(), which is called from main().The parameter, and therefore the value of the function, is 12.The make_y() function is not a member function of the derived class C, but it can be calledbecause it is a member of the base class B.main (void){ C i; i.make_x(10); i.make_y(12); cout<<i.make_xy();}The last line – cout<<i.make_xy();– in the main() function employs cout to display the result onscreen.The make_xy() function was defined to return the value of x multiplied by y.main (void){ C i; i.make_x(10); i.make_y(12); cout<<i.make_xy();} L & T Infotech Page 49 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismBecause the value of x is 10, and the value of y is 12, the return is 120. Multiple inheritance issuesBecause of its very nature, programming multiple inheritance can be difficult, so a programmerneeds to pay attention to several issues.When deriving a class from one or more public base classes, public members of the base classbecome public members of the derived class.Protected members of the base classes become protected members of the derived class.Private members of a base class are never directly accessible from a derived class.When deriving from a protected base class, public and protected members of the base classbecome protected members of the derived class.When deriving from a private base class, public and protected members of the base classbecome private members of the derived class.In the base-class list, member access must be specified for each base class.In the code, class A is protected, class B is public, and class C defaults to private.class D: protected A, public B, private C{ };If member access is not specified, the base class always defaults to private.class D: private C{ };However, for clarity, it is recommended that the member access be stated explicitly.class D: protected A, public B, private C{ };A base-class name must not be included more than once in a base-class list.In the case of an indirect base class, its name is not specified in the declaration.The name of the class, which is derived from it, is used.class D: protected A, public B, private C, public B{Lets say that class A inherits properties directly from class B.In this case, class D inherits all the characteristics directly from class A and class C.Because class D inherits class A, class B is an indirect base class of class D.class D: protected A, public B, private C{ }; L & T Infotech Page 50 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism Calling issuesA class derived from multiple base classes may need to call multiple base-class constructors.A derived class inherits its base classs members.Lets say that there is an instance of an object of a derived class.The base-class members of the derived class object must be initialized by calling the base classsconstructor.A base-class initializer may be coded in the derived classs constructor to explicitly call the base-class constructor.Otherwise, the derived classs constructor will implicitly call the base classs default constructor.A derived-class constructor always calls the constructor for its base class first to initialize thederived classs base-class members.Constructors are called in the order in which their base classes are declared.However, it is generally unwise to devise classes that depend on other class objects to beconstructed in a particular order.As a rule, classes should operate as independently of each other as possible.Class D: protected A, public B, C{};Suppose that youre coding a class that depends on other classes objects to be initialized in acertain order.Instead of having one class inherit other classes properties, you might declare the dependentclasses as data members of a new class.The new class could then control when and how the dependent members are initialized. L & T Infotech Page 51 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism Virtual Functions and PolymorphismPolymorphism is an important feature of object-oriented languages. It makes it easier to reusecode. This, in turn, leads to systems that are easier to maintain and extend.// mainvoid main(void) { CEmployee *pEmployee; pEmployee = new CHourlyWorker(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee; pEmployee = new CSupervisor(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee;Polymorphism can be defined as the ability of objects of different classes, related by inheritance,to respond differently to the same member function call.It allows the use of identical interfaces with different implementations.Consider the definition of a base class CEmployee which is used to define two derived classes: • CHourlyWorker • CSupervisor#include <iostream.h>// class declarations// CEmployeeclass CEmployee{ protected: char cLastName[25]; char cFirstName[25]; float fYearlyEarnings; public: CEmployee(); virtual void Print(); float ReviseYearlyEarnings();};// CHourlyWorkerclass CHourlyWorker:public CEmployee{ private: float fWeeklyWage;// CHourlyWorker L & T Infotech Page 52 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphismclass CHourlyWorker:public CEmployee{ private: float fWeeklyWage; public: CHourlyWorker(); void Print();};// CSupervisorclass CSupervisor:public CEmployee{ private: int nMonthlySalary; public: CSupervisor void Print();};CEmployee might include a function called Print() that allows you to output the contents of anobject of that class.Each derived class of CEmployee can redefine this function to output its local contents.// CHourlyWorkerclass CHourlyWorker:public CEmployee{ private: float fWeeklyWage; public: CHourlyWorker(); void Print();};// CSupervisorclass CSupervisor:public CEmployee{ private: int nMonthlySalary; public: CSupervisor void Print();};Although the contents of each derived class will be different, polymorphism allows you to treatthem all as objects of the base class.The contents of any derived class can then be printed by simply making a generic call to Print()using a base-class pointer. L & T Infotech Page 53 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism// mainvoid main(void) { CEmployee *pEmployee; pEmployee = new CHourlyWorker(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee; pEmployee = new CSupervisor(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee;}The proper behavior of the derived-class object when treated as a base-class object ispolymorphism.// mainvoid main(void) { CEmployee *pEmployee; pEmployee = new CHourlyWorker(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee; pEmployee = new CSupervisor(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee;}The call to function Print() can be resolved only at run time, and not at compile time. This ability todelay resolution is referred to as late or dynamic binding.Late binding is the mechanism for implementing polymorphism.Resolving the function at compile time is called early or static binding. L & T Infotech Page 54 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism Virtual functionsC++ supports polymorphism using virtual functions. You use the keyword virtual in a virtualfunctions prototype declaration.#include <iostream.h>//-----------------------------// class declarations//-----------------------------// CEmployeeclass CEmployee{ protected: char cLastName[25]; char cFirstName[25]; float fYearlyEarnings; public: CEmployee(); virtual void Print(); float ReviseYearlyEarnings();};// CHourlyWorkerclass CHourlyWorker:public CEmployee{ private: float fWeeklyWage;};Unlike redefined nonvirtual functions, redefined virtual functions must have the same number ofparameters and the same parameter types and return type.When a function is declared as a virtual function, it remains virtual from that point down throughthe hierarchy.#include <iostream.h>//-----------------------------// class declarations//-----------------------------// CEmployeeclass CEmployee{ protected: char cLastName[25]; char cFirstName[25]; L & T Infotech Page 55 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism float fYearlyEarnings; public: CEmployee(); virtual void Print(); float ReviseYearlyEarnings();};// CHourlyWorkerclass CHourlyWorker:public CEmployee{ private: float fWeeklyWage;};For the "employee" example, the function Print() is declared as a virtual function in the base classCEmployee.Print() is then redefined (or overloaded) in each derived class to print the contents specific to thatclass.#include <iostream.h>// class declarations// CEmployeeclass CEmployee{ protected: char cLastName[25]; char cFirstName[25]; float fYearlyEarnings; public: CEmployee(); virtual void Print(); float ReviseYearlyEarnings();};// CHourlyWorkerclass CHourlyWorker:public CEmployee{ private: float fWeeklyWage; public: CHourlyWorker(); void Print();};// CSupervisorclass CSupervisor:public CEmployee{ private: L & T Infotech Page 56 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism int nMonthlySalary; public: CSupervisor void Print();};Base-class member functions that are too generic to be defined (implemented) within the baseclass should be declared as pure virtual functions.When a pure virtual function is included in a class, you cannot create an instance of that class.This is referred to as an abstract class as it is too generic to define a real object.// CHourlyWorkerclass CHourlyWorker:public CEmployee{ private: float fWeeklyWage; public: CHourlyWorker(); void Print();};// CSupervisorclass CSupervisor:public CEmployee{ private: int nMonthlySalary; public: CSupervisor void Print();};Although you cannot create an object from an abstract class, you can declare a pointer to thatabstract class.A virtual function that is created in a base class and redefined in one or more derived classes canact polymorphically.When the function is called as a member of the base class using a base-class pointer, C++selects the correct redefined function in the appropriate derived class associated with that object.// mainvoid main(void) { CEmployee *pEmployee; L & T Infotech Page 57 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism pEmployee = new CHourlyWorker(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee; pEmployee = new CSupervisor(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee;}A call to a virtual function using a specific object name and the dot member selection operator isresolved at compile time.The virtual function used is the one defined for the class associated with that object, in this casethe CSupervisor class.// CHourlyWorkerCHourlyWorker::CHourlyworker() {}virtual void CHourlyWorker::Print( ) { cout << cFirstName << << cLastName << endl << "Weekly wage = " << fWeeklyWage << endl << "Expected Yearly Earnings = " << fYearlyEarnings;}void main() { CSupervisor Brown; Brown.print();}When a nonvirtual member function of a base class is redefined in a derived class, a call to thatfunction using a base-class pointer always uses the base-class version of the function.If a derived-class pointer is used to call the member function, the derived-class version of thefunction is used. This represents nonpolymorphic behavior.// CHourlyWorkerCHourlyWorker::CHourlyworker() {}virtual void CHourlyWorker::Print( ) { cout << cFirstName << << cLastName << endl << "Weekly wage = " << fWeeklyWage << endl L & T Infotech Page 58 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism << "Expected Yearly Earnings = " << fYearlyEarnings;}void main() { CSupervisor Brown; Brown.print();}As an example of nonpolymorphic behavior, consider the redefinition of nonvirtual base-classmember function ReviseYearlyEarnings() within class CSupervisor.A call to this function using the base-class pointer pEmployee will always access the base-classversion of the function rather than the corresponding redefined version.// mainvoid main(void) { CEmployee *pEmployee; pEmployee = new CHourlyWorker(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee; pEmployee = new CSupervisor(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee;}void CEmployee::REviseYearlyEarnings() { fYearlyEarnings = fYearlyEarnings + fYearlyEarnings/50}// CSupervisor::CSupervisor() {}virtual void CSupervisor::Print() { cout << cFirstName << << cLastName << endl << "Monthly Salary = " << nMonthlySalary << endl << "Expected Yearly Earnings = " << fYearlyEarnings;}void CSupervisor::REviseYEarlyEarnings() { fYearlyEarnings = fYearlyEarnings + fYearlyEarnings/50To access the appropriate redefined version of the function, you need to use a pointer to thederived class (CSupervisor).// mainvoid main(void) { CEmployee *pEmployee; L & T Infotech Page 59 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism pEmployee = new CHourlyWorker(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee; pEmployee = new CSupervisor(); pEmployee->ReviseYearlyEarnings(); pEmployee->Print(); delete pEmployee;} Compiling considerationsWith polymorphism and virtual functions, a programmer can write code independently of the typesof objects to which messages are sent.Dynamic binding allows virtual functions to compile without knowing the type or class of an objectat compile time.At run time, the virtual function call is matched with the member function of the receiving object.This allows new objects to be added without the need to recompile the whole system.All you need to do is compile the objects being added and any code which manipulates thatobject type specifically. Default function parametersA useful technique associated with C++ functions is the ability to specify default values forfunction parameters.You can employ this technique when a function call needs to provide only some of the expectedarguments for that function.Consider a function that returns the product of three integer values.To call the function using only two arguments, say num1 and num2, the programmer first needsto determine and then supply the correct value to the unused parameter.This is cumbersome and can be prone to error.Default function arguments are designed to guard against incorrect function values beingassigned to function parameters.To declare a default value, you assign the parameter the appropriate value in the functionprototype.In this example, the prototype declaration indicates that Product() requires only two arguments,but can have three arguments.The default values must come last in the functions parameter list, and can only appear in thefunctions prototype. For example, you cant have a prototype like the one in this example.But you can use the following prototype:int Product(int X, int Y, int Z=1); L & T Infotech Page 60 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismIf a function call specifies no arguments for function parameters with default values, then theparameters are given the default values listed in the prototype.Polymorphism in an application1. ShapesLets consider a shape hierarchy as the classic example of how you can apply polymorphism toan application.The shape program needs to manage generic shape objects. All shapes can be drawn, buttriangles, circles, and squares are all drawn differently.To create a program that is easy to extend, you need to be able to handle generic shape pointerswhile ignoring the details of the particular shape you are dealing with.2. Coding considerationsSuppose a set of shape classes CTriangle, CCircle, and CSquare are all to be derived from baseclass CShape.Each derived class will have its own Draw() and SetColor() functions.Using polymorphism, you want to be able to draw any shape and set its color by simply invokingthe Draw() and SetColor() functions using a base-class pointer.To implement the program, you first need to define the base class, CShape, from which all othershape classes will be derived.By declaring color as a private data member, access to it is restricted to functions that aremembers or friends of the base class.#include <iostream.h> typedef enum{ Unknown, Blue, Green, Red, Yellow } Color; //-------------------------- // class declarations //-------------------------- // CShape class CShape{ private: Color color; L & T Infotech Page 61 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism public: Cshape();CShape must be set up as an abstract class because member function Draw() is too generic tobe defined for an object of this class.#include <iostream.h> typedef enum{ Unknown, Blue, Green, Red, Yellow } Color; //-------------------------- // class declarations //-------------------------- // CShape class CShape{ private: Color color; public: Cshape();You make CShape an abstract class by declaring Draw() as a pure virtual function, that is, youassign its prototype a value of zero.//-------------------------- // class declarations //-------------------------- // CShape class CShape{ private: Color color; public: Cshape(); virtual ~CShape(); virtual void Draw(void)=0; virtual void SetColor(Color c); }; // CTriangle class CTriangle : public CShape{ private: //------------------------------- L & T Infotech Page 62 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism // class definitions //------------------------------- // CShape CShape::CShape() { } CShape::~CShape() { cout << "In ~ CShape()n"; } void CShape::SetColor(Color c) { cout << "In CShape::Setcolor()n"; color=c; }; // CTriangle CTriangle::CTriangle() { m_nAngle1=m_nAngle2=m_nAngle3=0;The implementation presented here does not deal with the mechanics of how to draw eachshape.Only part definitions of constructors, destructors, and other class member functions areshown.When the program is executed, the output consists of a list of "location messages" (ordiagnostics), used to indicate the execution path through the program.Base-class member function SetColor() can be defined without knowing which shape object it isbeing applied to.Therefore, it can be declared as a virtual function, rather than a pure virtual function.You can see from its definition that it outputs a location message to the screen and assigns avalue to base-class data member color.//-------------------------- // class declarations //-------------------------- // CShape class CShape{ private: Color color; L & T Infotech Page 63 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism public: Cshape(); virtual ~CShape(); virtual void Draw(void)=0; virtual void SetColor(Color c); }; // CTriangle class CTriangle : public CShape{ private: //------------------------------- // class definitions //------------------------------- // CShape CShape::CShape() { } CShape::~CShape() { cout << "In ~ CShape()n"; } void CShape::SetColor(Color c) { cout << "In CShape::Setcolor()n"; color=c; }; // CTriangle CTriangle::CTriangle() { m_nAngle1=m_nAngle2=m_nAngle3=0;In addition to Draw() and SetColor(), CShapes public interface includes the constructor CShape()and the destructor ~CShape()The destructor ~CShape needs to be declared as a virtual function to overcome a problem whichoccurs when virtual functions and destructors are used with class hierarchies.//-------------------------- // class declarations //-------------------------- // CShape class CShape{ L & T Infotech Page 64 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism private: Color color; public: Cshape(); virtual ~CShape(); virtual void Draw(void)=0; virtual void SetColor(Color c); }; // CTriangle class CTriangle : public CShape{ private:If the destructor is not marked as virtual, then an explicit call to destroy an object using thedelete operator and a base-class pointer will always use the base-class destructor,irrespective of the object being pointed to.Once CShape has been defined, you can begin to define each of the derived classes.Each derived class will inherit from the base class CShape.//-------------------------- // class declarations //-------------------------- // CShape class CShape{ private: Color color; public: Cshape(); virtual ~CShape(); virtual void Draw(void)=0; virtual void SetColor(Color c); }; // CTriangle class CTriangle : public CShape{ private: L & T Infotech Page 65 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism3. Defining derived classesLets look at how you might define CTriangle, the derived class for triangle objects.You want the public members of CShape to be public in the derived class CTriangle. So youderive CTriangle from CShape using the keyword public.// CTriangle class CTriangle : public CShape{ private: short m_nAngle1, m_nAngle2, m_nAngle3; public: CTriangle(); ~CTriangle(); void SetAngles(short nAngle1, short nAngle2, short nAngle3) void Draw(void); void SetColor(Color c); }; // CCircle class CCircle : public CShape( private: short m_nRadius; public: CCircle(); ~CCircle();The shape of any triangle is described by the magnitude of its three angles. Therefore, CTriangleincludes three data members to hold these values.// CTriangle class CTriangle : public CShape{ private: short m_nAngle1, m_nAngle2, m_nAngle3; public: CTriangle(); ~CTriangle(); void SetAngles(short nAngle1, short nAngle2, short nAngle3) void Draw(void); void SetColor(Color c); }; // CCircle class CCircle : public CShape( private: short m_nRadius; L & T Infotech Page 66 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism public: CCircle(); ~CCircle();In addition to a constructor and destructor, CTriangle includes three other member functions in itspublic interface • SetAngles() • Draw() • SetColor()SetAngles()Draw()SetColor()// CTriangle class CTriangle : public CShape{ private: short m_nAngle1, m_nAngle2, m_nAngle3; public: CTriangle(); ~CTriangle(); void SetAngles(short nAngle1, short nAngle2, short nAngle3) void Draw(void); void SetColor(Color c); }; // CCircle class CCircle : public CShape( private: short m_nRadius; public: CCircle(); ~CCircle();SetAngles() allows you to describe a triangle object by setting the values of CTriangles datamembers m_nAngle1, m_nAngle2, and m_nAngle3.Draw() and SetColor() are overloaded versions of the corresponding base class virtual functions,which allows them to be polymorphically selected at run time.// CTriangle class CTriangle : public CShape{ private: short m_nAngle1, m_nAngle2, m_nAngle3; L & T Infotech Page 67 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism public: CTriangle(); ~CTriangle(); void SetAngles(short nAngle1, short nAngle2, short nAngle3) void Draw(void); void SetColor(Color c); }; // CCircle class CCircle : public CShape( private: short m_nRadius; public: CCircle(); ~CCircle();In this example, Draw() simply outputs the message "In CTriangle::Draw()" each time it is called.SetColor() explicitly calls the base-class version of itself to assign a value to base-class privatedata member color.CTriangle::~CTriangle() { cout << "In ~CTriangle()n"; } void CTriangle::SetAngles(short nAngle1, short nAngle2, short nAngle3) { m_nAngle1=nAngle1; m_nAngle2=nAngle2; m_nAngle3=nAngle3; } void CTriangle::Draw(void) { cout << "In CTriangle::Draw()n"; } void CTriangle::SetColor(Color c) { cout << "In CTriangle::SetColor()n" CShape::SetColor(c); } // CCircleYou dont have to derive your own versions of base-class virtual functions if implementations areprovided in the base class.For example, if SetColor() was not redefined here, the CShape version would be inherited. L & T Infotech Page 68 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismCTriangle::~CTriangle() { cout << "In ~CTriangle()n"; } void CTriangle::SetAngles(short nAngle1, short nAngle2, short nAngle3) { m_nAngle1=nAngle1; m_nAngle2=nAngle2; m_nAngle3=nAngle3; } void CTriangle::Draw(void) { cout << "In CTriangle::Draw()n"; } void CTriangle::SetColor(Color c) { cout << "In CTriangle::SetColor()n" CShape::SetColor(c); } // CCircleIf Draw() was not defined in CTriangle, then CTriangle would be an abstract class as there isno definition for Draw() in CShape.Therefore, you do need to include a definition for Draw() in CTriangle.Derived classes CCircle and CSquare are defined in a way similar to CTriangle. Each is set up topublicly inherit from the base class CShape.// CCircle class CCircle : public CShape{ private: short m_nRadius; public: CCircle(); ~CCircle(); void SetRadius(short nRadius); void Draw(void); void SetColor(Color c); };// CSquare class CSquare : public CShape( private: short m_nSideLength; public: L & T Infotech Page 69 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism CSquare(); ~CSquare();As a circle is described by its radius, an appropriate data member is declared in the privateportion of class CCircle.CCircle includes a public member function SetRadius() that you can use to describe the circleobject created.// CCircle CCircle::CCircle() { m_nRadius=0; } CCircle::~CCircle() { cout << "In ~CCircle()n"; } void CCircle::SetRadius(short nRadius) { m_nRadius=nRadius; } void CCircle::Draw(void) { cout << "In CCircle::Draw()n"; } void CCircle::SetColor(Color c) { cout << "In CCircle::SetColor()n"; CShape::SetColor(c);CSquare has a single data member, m_nSideLength, that describes the square object.CSquare::SetSideLength() allows you to manipulate this data member// CSquare class CSquare : public CShape( private: short m_nSideLength; public: CSquare(); ~CSquare(); void SetSideLength(short nSideLength); void Draw(void); void SetColor(Color c); }; //------------------------------- // class definitions L & T Infotech Page 70 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism //------------------------------- // CShape CShape::CShape() { }Base-class member functions Draw() and SetColor() are both redefined for CCircle and CSquare.The implementation of these functions is similar to those described for CTriangle.// CSquare class CSquare : public CShape( private: short m_nSideLength; public: CSquare(); ~CSquare(); void SetSideLength(short nSideLength); void Draw(void); void SetColor(Color c); }; //------------------------------- // class definitions //------------------------------- // CShape CShape::CShape() { }4. Polymorphism in main()Lets now examine main() to see how polymorphism simplifies the code.Function main() begins with the declaration of the base-class pointer pShape.Using a base class pointer allows the use of generic calls to Draw() and SetColor() without anyindication of the shape currently being referencedThe first step in drawing a triangle is to create an object of class CTriangle.One way to do this is to call CTriangles constructor using the new operator.The new operator returns the address of the object, which can be used to initialize the pointerpShape.// main function void main(void) { CShape *pShape; // create a triangle pShape=new CTriangle(); L & T Infotech Page 71 of 87
    • OOPS Concepts using C++ Virtual Function & PolymorphismThe new and delete operators handle pointers rather than the objects themselves. You usenew to return a pointer to the initialized object.The delete operator takes a pointer to an object that was previously created using new.To describe the triangle, you need to access CTriangles unique member function SetAngles().You can do this by casting the base-class pointer, pShape, as a CTriangle pointer.This allows you to call CTriangle::SetAngles() using the pointer arrow operator "->".// main function void main(void) { CShape *pShape; // create a triangle pShape=new CTriangle(); ((CTriangle*)pShape)->SetAngles(90,90,90);Once you have described the object, you can make use of polymorphism to set its color and thendraw it.Polymorphism allows you to code a generic call to SetColor() using base-class pointer pShape.C++ will polymorphically select CTriangle::SetColor() as the correct version of SetColor() in thisinstance. This is because pShape will be pointing to an object of class CTriangle when the call ismade.// main function void main(void) { CShape *pShape; // create a triangle pShape=new CTriangle(); ((CTriangle*)pShape)->SetAngles(90,90,90); pShape->SetColor(Blue);You can confirm the polymorphic behavior by observing the programs output.The first non-constructor function executed is CTriangle::SetColor(). This function calls the base-class version of SetColor() (that is, CShape::SetColor()) to set the color of the object.No location messages have been included in the definition of class constructors. Therefore,there is no indication of which constructors have been executed in the programs output.Next, pShape is used to make a generic call to base-class member function Draw().As for SetColor(), C++ polymorphically selects CTriangle::Draw() as the correct version of Draw()in this instance. L & T Infotech Page 72 of 87
    • OOPS Concepts using C++ Virtual Function & Polymorphism// main function void main(void) { CShape *pShape; // create a triangle pShape=new CTriangle(); ((CTriangle*)pShape)->SetAngles(90,90,90); pShape->SetColor(Blue); pShape->Draw(); delete pShape;The delete operator is used to destroy the object currently pointed to by pShape. It automaticallyinvokes the correct class destructor for the object.First, the derived-class destructor is called, then the destructor for the base class.Generally, destructors are called in reverse order of their constructors.Because of polymorphism, the code used to draw the circle and square objects is almost identicalto the code used to draw the triangle object.The only difference is in the casting for calls to functions specific to each class.// create a circle pShape=new CCircle(); ((CCircle*)pShape)->SetRadius(10); pShape->SetColor(Red); pShape->Draw(); delete pShape; // create a square pShape=new CSquare(); ((CSquare*)pShape)->SetSideLength(10); pShape->SetColor(Green);A glance at the output shows that the execution path for each of the three objects is quitedifferent. L & T Infotech Page 73 of 87
    • OOPS Concepts using C++ Program Using Polymorphism Program Using PolymorphismA program to calculate the area of two-dimensional objects using polymorphism.This involves the following tasks: • defining a base class • defining member functions • defining derived classes • defining member functions • handling a rectangle object • handling a circle objectSuppose you have been asked to write a program to calculate the area of two-dimensionalobjects using polymorphism. You are required to test the design by coding a main() function thatcreates a rectangle and a circle.You should calculate the area of each defined object and output the details of the calculation tothe screen.Task 1: Defining a base classYour first task is to define a base class, called C2dObject, from which all two-dimensional objectswill be derived.Step 1 of 4See if you can complete the line that begins the definition of the base class C2dObject.ResultTo complete the class definition you typeclass C2dObject{Next, you need to declare a data member within C2dObject that will hold the value calculated forarea.Lets assume that you want derived-class member functions to have access to this base-classdata member.#include <iostream.h>//------------------------------------// class declarations//------------------------------------// C2dObjectclass C2dObject{ L & T Infotech Page 74 of 87
    • OOPS Concepts using C++ Program Using PolymorphismStep 2 of 4In which portion of the class do you think you would declare this data member?Options: 1. private 2. protected 3. publicResultThe protected portion of the class declares this data member.You declare floating-point variable, m_fArea, as a protected data member of C2dObject.#include <iostream.h>//------------------------------------// class declarations//------------------------------------// C2dObjectclass C2dObject{ protected float m_fArea;Assume that C2dObjects public interface has already been partly defined with the declaration ofa constructor and destructor.You now want to declare a member function, called CalcArea(), that will be used to calculate thearea of any two-dimensional object.#include <iostream.h>//------------------------------------// class declarations//------------------------------------// C2dObjectclass C2dObject{ protected float m_fArea; public: C2dObject(); virtual ~C2dObject();C2dObject is too generic to be a real object.Step 3 of 4 L & T Infotech Page 75 of 87
    • OOPS Concepts using C++ Program Using PolymorphismSee if you can complete the declaration of the CalcArea() prototype.MISSING CODE void CalcArea(void) = 0;ResultYou declare CalcArea() as a pure virtual function by entering this statement:virtual void CalcArea(void) = 0;You are also asked to include a function PrintDetails() that will display the value calculated for thearea of an object.A generic call to PrintDetails() should also print the defining data used in the calculation of thearea of the object.#include <iostream.h>//------------------------------------// class declarations//------------------------------------// C2dObjectclass C2dObject{ protected float m_fArea; public: C2dObject(); virtual ~C2dObject(); virtual void CalcArea(void) = 0;Step 4 of 4How do you think you could declare PrintDetails() within the base class C2dObject, if adefinition for PrintDetails() is to exist for the base class as well as for derived classes?Options: 1. virtual void PrintDetails(void); 2. virtual PrintDetails(); 3. virtual PrintDetails()=0; 4. void PrintDetails(void)=0;ResultYou could declare PrintDetails() using the codevirtual void PrintDetails(void);orvirtual PrintDetails(); L & T Infotech Page 76 of 87
    • OOPS Concepts using C++ Program Using PolymorphismAs a definition for PrintDetails() is to be included within C2dObject, and polymorphic behavior isrequired, you declare PrintDetails() as a virtual function by entering this statement.#include <iostream.h>//------------------------------------// class declarations//------------------------------------// C2dObjectclass C2dObject{ protected float m_fArea; public: C2dObject(); virtual ~C2dObject(); virtual void CalcArea(void) = 0; virtual void PrintDetails(void);Finally, you enter the closing tag – }; – to complete the definition of the base class C2dObject.#include <iostream.h>//------------------------------------// class declarations//------------------------------------// C2dObjectclass C2dObject{ protected float m_fArea; public: C2dObject(); virtual ~C2dObject(); virtual void CalcArea(void) = 0; virtual void PrintDetails(void);}; L & T Infotech Page 77 of 87
    • OOPS Concepts using C++ Program Using PolymorphismTask 2: Defining member functionsNow, you need to define the member functions of class C2dObject.Assume that the class constructor and destructor have already been defined.//--------------------------// class definitions//--------------------------// C2dObjectC2dObject::C2dObject() { m_fArea = 0;}C2dObject::~C2dObject() {}Step 1 of 1See if you can complete the code required to begin the definition of PrintDetails() for baseclass C2dObject./--------------------------// class definitions//--------------------------// C2dObjectC2dObject::C2dObject() { m_fArea = 0;}C2dObject::~C2dObject() {}void MISSING CODE (void) {ResultTo begin the definition of PrintDetails() you typeC2dObject::PrintDetailsThis cout statement outputs the area of the current object.//--------------------------// class definitions//--------------------------// C2dObject L & T Infotech Page 78 of 87
    • OOPS Concepts using C++ Program Using PolymorphismC2dObject::C2dObject() { m_fArea = 0;}C2dObject::~C2dObject() {}void C2dObject::PrintDetails(void) { cout << "Area = " << m_fArea << "n";}Task 3: Defining derived classesYou are now going to define derived classes CRectangle and CCircle.// CRectangleStep 1 of 3See if you can complete the opening line in the definition of derived class CRectangle.You want public members of the base class to be inherited as public members in the derivedclass.// CRectangleclass MISSING CODE C2dObject {ResultThe code to complete the definition of derived class CRectangle is CRectangle:public.Step 2 of 3Which of the following data members would you expect to find declared in the private portionof class CRectangle?Options: 1. m_nLength 2. m_nWidth 3. m_nArea 4. m_nRadiusResultA rectangle is described by its length and width. Therefore, you need to declare m_nLengthand m_nWidth as private data members of CRectangle.Derived class CRectangle inherits base-class data member m_fArea to hold the value for thearea of a rectangle object. L & T Infotech Page 79 of 87
    • OOPS Concepts using C++ Program Using Polymorphism// CRectangleclass CRectangle:public C2dObject{ private: int m_nLength, m_nWidth;Assume that the constructor and destructor for CRectangle have already been declared anddefined.Also declared is the prototype for the function SetDimensions(). It will be used to set the valuesfor the length and width of a rectangle object.// CRectangleclass CRectangle:public C2dObject{ private: int m_nLength, m_nWidth; public: CRectangle(); ~CRectangle(); void SetDimensions(int nLength, int nWidth);The program needs to display polymorphic behavior when calls are made to • calculate the area of an object • output the details of an object to the screenStep 3 of 3For a polymorphic implementation, which of the following declarations do you think you shoulduse to complete CRectangles public interface?Options: 1. void CalcArea(void); 2. virtual void CalcArea(void)=0; 3. void PrintDetails(void); 4. void PrintDetails();ResultYou use the declarationsvoid CalcArea(void); andvoid PrintDetails(void);You include declarations for base-class virtual member functions CalcArea() and PrintDetails() inthe public portion of derived class CRectangle.// CRectangleclass CRectangle:public C2dObject{ private: int m_nLength, m_nWidth; public: L & T Infotech Page 80 of 87
    • OOPS Concepts using C++ Program Using Polymorphism CRectangle(); ~CRectangle(); void SetDimensions(int nLength, int nWidth); void CalcArea(void); void PrintDetails(void);Finally, you add the closing tag – }; – to complete the definition of derived class Crectangle.// CRectangleclass CRectangle:public C2dObject{ private: int m_nLength, m_nWidth; public: CRectangle(); ~CRectangle(); void SetDimensions(int nLength, int nWidth); void CalcArea(void); void PrintDetails(void);};Task 4: Defining a rectangle objectAssume that the definitions for CRectangles constructor and destructor functions have both beencompleted.// CRectangleCRectangle::CRectangle() { m_nLength = m_nWidth = 0;}CRectangle::~CRectangle() {}Step 1 of 2What statement do you think you would include in the definition ofCRectangle::SetDimensions() to set the value for the length of an object?// CRectangleCRectangle::CRectangle() { m_nLength = m_nWidth = 0;}CRectangle::~CRectangle() {}void CRectangle::SetDimensions(int nLength, int nWidth) { MISSING CODE ; L & T Infotech Page 81 of 87
    • OOPS Concepts using C++ Program Using PolymorphismResultThe statement that sets the value for the length ism_nLength=nLength;Step 2 of 2See if you can complete the definition CRectangle::CalcArea()// CRectangleCRectangle::CRectangle() { m_nLength = m_nWidth = 0;}CRectangle::~CRectangle() {}void CRectangle::SetDimensions(int nLength, int nWidth) { m_nLength=nLength; m_nWidth=nWidth;}void CRectangle::CalcArea(void) { MISSING CODE}ResultThe code to complete the definition ism_fArea=m_nLength*m_nWidth;You define CRectangle::PrintDetails() to output the dimensions of a rectangle object using astatement similar to this.You can simply make a call to the base-class version of PrintDetails() to output the valuecalculated for the area of the rectangle object.You have now completed the implementation of derived class CRectangle and its memberfunctions.// CRectangleCRectangle::CRectangle() { m_nLength = m_nWidth = 0;}CRectangle::~CRectangle() {}void CRectangle::SetDimensions(int nLength, int nWidth) { m_nLength=nLength; m_nWidth=nWidth;}void CRectangle::CalcArea(void) { m_fArea=m_nLength*m_nWidth; L & T Infotech Page 82 of 87
    • OOPS Concepts using C++ Program Using Polymorphism}void CRectangle::PrintDetails(void) { cout << "Rectangle of length "<< m_nLength << "n"; cout << "and width " << m_nWidth << "n"; C2dObject::PrintDetails();}You implement derived class CCircle in a similar way to CRectangle.}// CCircleCCircle::CCircle() { m_nRadius = 0;}CCircle::~CCircle() {}void CCircle::SetRadius(int nRadius) { m_nRadius = nRadius}void CCircle::CalcArea(void) { m_fArea = 3.14 * m_nRadius * m_nRadius;}void CCircle::PrintDetails(void) { cout << "Circle of radius " << m_nRadius << "n"; C2dObject::PrintDetails();}Task 5: Handling a rectangle objectYou are now ready to code a simple program for this application.// main functionvoid main(void) {Step 1 of 5See if you can enter a statement declaring p2dObject as a base-class pointer.// main functionvoid main(void) { MISSING CODEResultThe code to declare p2dObject as a base-class pointer is C2dObject*p2dObject. The base-class pointer is required for polymorphic operation. L & T Infotech Page 83 of 87
    • OOPS Concepts using C++ Program Using PolymorphismStep 2 of 5See if you can complete the statement that creates a rectangle object and assigns its addressto the base-class pointer.// main functionvoid main(void) { C2dObject*p2dObject; // create a rectangle p2dObject=MISSING CODEResultThe code used to complete the statement is newCRectangle().To set the length and width of the newly created rectangle object, you need to callCRectangle::SetDimensions().Step 3 of 5See if you can complete the function call to CRectangle::SetDimensions() using the base-class pointer.// main functionvoid main(void) { C2dObject*p2dObject; // create a rectangle p2dObject=newCRectangle(); ( MISSING CODE)>SetDimensions(4, 2)ResultThe code to complete the function call is(CRectangle*)p2dObjectStep 4 of 5See if you can enter a statement to calculate the area of the newly created rectangle object.// main functionvoid main(void) { C2dObject*p2dObject; // create a rectangle p2dObject=newCRectangle(); L & T Infotech Page 84 of 87
    • OOPS Concepts using C++ Program Using Polymorphism ((CRectangle*)p2dObject)->SetDimensions(4, 2); MISSING CODEResultThe statement to calculate the area of the newly created rectangle is the generic function callp2dObject->CalcArea.You can use another generic call to output the details of the rectangle object.// main functionvoid main(void) { C2dObject*p2dObject; // create a rectangle p2dObject=newCRectangle(); ((CRectangle*)p2dObject)->SetDimensions(4, 2); p2dObject->CalcArea(); p2dObject->PrintDetails();Step 5 of 5See if you can enter a statement to destroy the object created.// main functionvoid main(void) { C2dObject*p2dObject; // create a rectangle p2dObject=newCRectangle(); ((CRectangle*)p2dObject)->SetDimensions(4, 2); p2dObject->CalcArea(); p2dObject->PrintDetails(); MISSING CODEResultTo destroy the object you use the code delete p2dObject.Task 6: Handling a circle objectYou now want to create a circle.// main functionvoid main(void) { C2dObject*p2dObject; L & T Infotech Page 85 of 87
    • OOPS Concepts using C++ Program Using Polymorphism // create a rectangle p2dObject=newCRectangle(); ((CRectangle*)p2dObject)->SetDimensions(4, 2); p2dObject->CalcArea(); p2dObject->PrintDetails(); delete p2dObject; // create a circleStep 1 of 1See if you can complete the assignment statement that creates a new circle object andassigns its address to base-class pointer p2dObject.// main functionvoid main(void) { C2dObject*p2dObject; // create a rectangle p2dObject=newCRectangle(); ((CRectangle*)p2dObject)->SetDimensions(4, 2); p2dObject->CalcArea(); p2dObject->PrintDetails(); delete p2dObject; // create a circle p2dObject=MISSING CODEResultThe code to complete the assignment statement is newCCircle();.You can set the radius of the circle to 3 with this statement.// main functionvoid main(void) { C2dObject*p2dObject; // create a rectangle p2dObject=newCRectangle(); ((CRectangle*)p2dObject)->SetDimensions(4, 2); p2dObject->CalcArea(); p2dObject->PrintDetails(); delete p2dObject; // create a circle L & T Infotech Page 86 of 87
    • OOPS Concepts using C++ Program Using Polymorphism p2dObject=newCCircle(); ((CCircle*)p2dObject)->SetRadius(3);The remainder of the program is a repetition of the code used to handle a rectangle object. Thiscompletes the program.// main function void main(void) { C2dObject*p2dObject; // create a rectangle p2dObject=newCRectangle(); ((CRectangle*)p2dObject)->SetDimensions(4, 2); p2dObject->CalcArea(); p2dObject->PrintDetails(); delete p2dObject; // create a circle p2dObject=newCCircle(); ((CCircle*)p2dObject)->SetRadius(3); p2dObject->CalcArea(); p2dObject->PrintDetails(); delete p2dObject; }When the program is compiled and run, it produces this output. L & T Infotech Page 87 of 87