We've as of now perceived how to define composite datatypes utilizing classes. Presently we'll step back and consider the programming rationality basic classes, known as article situated programming (OOP).

1 The Basic Ideas of OOP

Fantastic "procedural" programming dialects before C++, (for example, C) regularly centered around the inquiry "What ought to the system do next?" The way you structure a project in these dialects is:

1. Split it up into an arrangement of errands and subtasks

2. Make capacities for the errands

3. Instruct the PC to perform them in arrangement

With a lot of information and/or extensive quantities of undertakings, this makes for mind boggling and unmaintainable projects.

Consider the errand of demonstrating the operation of an auto. Such a project would have heaps of partitioned variables putting away data on different auto parts, and there'd be no real way to bunch together all the code that identifies with, say, the wheels. It's difficult to keep every one of these variables and the associations between every one of the capacities at the top of the priority list.

To deal with this unpredictability, it's more pleasant to bundle up self-sufficient, secluded bits of code. Individuals think about the world as far as connecting articles: we'd discuss associations between the controlling wheel, the pedals, the wheels, and so forth. OOP permits software engineers to pack away points of interest into perfect, independent boxes (objects) with the goal that they can think about the questions all the more conceptually and spotlight on the collaborations between them.

There are bunches of definitions for OOP, yet 3 essential components of it are:

Encapsulation: gathering related information and capacities together as items and defining an interface to those articles

Inheritance: permitting code to be reused between related sorts

Polymorphism: permitting a quality to be one of a few sorts, and deciding at runtime which capacities to approach it in light of its sort

How about we perceive how each of these plays out in C++.

2 Encapsulation

Exemplification just alludes to bundling related stuff together. We've as of now perceived how to bundle up information and the operations it underpins in C++: with classes.

On the off chance that somebody gives us a class, we don't have to know how it really attempts to utilize it; everything we need to think about is its open strategies/information – its interface. This is frequently contrasted with working an auto: when you drive, you couldn't care less how the controlling wheel makes the wheels turn; you simply mind that the interface the auto displays (the directing wheel) permits you to finish your objective. On the off chance that you recollect the relationship from Lecture 6 about articles being boxes with catches you can push, you can likewise think about the interface of a class as the arrangement of catches every occasion of that class makes accessible. Interfaces unique away the points of interest of how every one of the operations are really performed, permitting the software engineer to concentrate on how protests will utilize one another's interfaces – how they collaborate.

This is the reason C++ makes you indicate open and private access specifiers: as a matter of course, it expect that the things you define in a class are interior points of interest which somebody utilizing your code ought not need to stress over. The act of concealing endlessly these points of interest from customer code is called "information concealing," or making your class a "black box."

One approach to consider what happens in an article situated system is that we define what items exist and what every one knows, and after that the items send messages to one another( (by calling one another's techniques) to trade data and let each know other what to do.

3 Inheritance

Legacy permits us to define progressive systems of related classes.

Envision we're composing a stock project for vehicles, including autos and trucks. We could compose one class for speaking to autos and an inconsequential one for speaking to trucks, yet we'd need to copy the usefulness that all vehicles have in like manner. Rather, C++ permits us to determine the basic code in a Vehicle class, and after that indicate that the Car and Truck classes share this code.

The Vehicle class will be much the same as what we've seen some time recently:

1 2 3 4

class Vehicle { secured : string permit ; int year ;

2

5

6 open :

7 Vehicle( const string &myLicense , const int myYear)

8 : license(myLicense), year(myYear) {} 9 const string getDesc() const 10 {return permit + " from " + stringify(year);} 11 const string &getLicense() const {return license;} 12 const int getYear() const {return year;} 13 };

A couple notes on this code, by line:

2. The standard string class is portrayed in Section 1 of PS3; see there for subtle elements. Review that strings can be attached to one another with the + administrator.

3. ensured is to a great extent equal to private. We'll examine the differences without further ado.

8. This line exhibits part initializer sentence structure. While defining a constructor, you in some cases need to instate certain individuals, especially const individuals, even before the constructor body. You essentially put a colon before the capacity body, trailed by a comma-isolated rundown of things of the structure dataMember(initialValue).

10. This line accept the presence of some capacity stringify for changing over numbers to strings.

Presently we need to determine that Car will acquire the Vehicle code, yet with a few augmentations. This is refined in line 1 beneath:

1 class Car : open Vehicle {/Makes Car acquire from Vehicle 2 string style; 3 4 open : 5 Car( const string &myLicense , const int myYear , const string &myStyle)

6 : Vehicle(myLicense , myYear), style(myStyle) {}

7 const string &getStyle() {return style;}

8 };

Presently class Car has all the information individuals and systems for Vehicle, and a style information part and a getStyle strategy.

Class Car acquires from class Vehicle. This is proportionate to stating that Car is an inferred class, while Vehicle is its base class. You might likewise hear the terms subclass and superclass.

Notes on the code:

3

1. Try not to stress for the present over why we put people in general watchword in there.

6. Note how we utilize part initializer sentence structure to call the base-class constructor. We need a complete Vehicle article built before we develop the parts included the Car. On the off chance that you don't expressly call a base-class constructor utilizing this sentence structure, the default base-class constructor will be called.

Essentially, we could make a Truck class that acquires from Vehicle and shares its code. This would give a class pecking order like the accompanying:

Vehicle

Truck Car

Class chains of command are by and large drawn with bolts indicating from inferred classes base classes.

3.1 Is-a versus Has-a

There are two ways we could depict some class An as relying upon some different class B:

1. Each An item has a B object. Case in point, each Vehicle has a string item (called permit).

2. Each occasion of A will be a B case. Case in point, each Car is a Vehicle, also.

Legacy permits us to define "is-a" connections, yet it ought not be utilized to execute "has-a" connections. It would be a configuration blunder to make Vehicle acquire from string on the grounds that each Vehicle has a permit; a Vehicle is not a string. "Has-a" connections ought to be actualized by proclaiming information individuals, not by legacy.

3.2 Overriding Methods

We might need to create the depiction for Cars in a different route from bland Vehicles. To perform this, we can essentially redefine the getDesc technique in Car, as beneath. At that point, when we call getDesc on a Car object, it will utilize the redefined capacity. Redefining in this way is called overriding the capacity.

1 2 3

class Car : open string style ;

Vehicle {/Makes Car acquire from Vehicle

4

4 open : 5 Car( const string &myLicense , const int myYear , const string &myStyle) 6 : Vehicle(myLicense , myYear), style(myStyle) {} 7 const string getDesc()/Overriding this part work 8 {return stringify(year) + " + style + ": " + permit ;} 9 const string &getStyle() {return style;} 10 };

3.2.1 Programming by Difference

In defining inferred classes, we just need to indicate what's different about them from their base classes. This effective procedure is called programming by difference.

Legacy permits just overriding routines and including new individuals and systems. We can't uproot usefulness that was available in the base class.

3.3 Access Modifiers and Inheritance

In the event that we'd pronounced year and permit as private in Vehicle, we wouldn't have the capacity to get to them even from an inferred class like Car. To permit determined classes however not outside code to get to information individuals and part works, we must proclaim them as ensured.

The general population watchword utilized as a part of indicating a base class (e.g., class Car : open Vehicle {...}) gives a point of confinement for the perceivability of the acquired systems in the determined class. Regularly you ought to simply utilize open here, which implies that acquired routines announced as open are still open in the inferred class. Indicating secured would make acquired systems, even those announced open, have at most ensured perceivability. For a full table of the effects of different legacy access specifiers, see http://en.wikibooks.org/wiki/C++ Programming/Classes/Inheritance.

4 Polymorphism

Polymorphism signifies "numerous shapes." It alludes to the capacity of one article to have numerous sorts. In the event that we have a capacity that expects a Vehicle object, we can securely pass it a Car object, on the grounds that each Car is additionally a Vehicle. Similarly for references and pointers: anyplace you can utilize a Vehicle *, you can utilize a Car *.

5

4.1 virtual Functions

There is still an issue. Take the accompanying case:

1 Car c(" VANITY " , 2003) ; 2 Vehicle * vPtr = &c; 3 cout << vPtr - > getDesc () ;

(The - > documentation on line 3 just dereferences and gets a part. ptr->member is identical to (*ptr).member.)

Since vPtr is pronounced as a Vehicle *, this will call the Vehicle form of getDesc, despite the fact that the item indicated is really a Car. Normally we'd need the project to choo
 
Top