Visual C++ Programming – Penn Wu, PhD 251 Lecture #9 Inheritance, Composition, and Polymorphism Introduction In terms of object-oriented programming, “inheritance” is the ability that enables new classes to use the properties and methods of existing classes through the declaration of inheritance. For example, the word “car” is a general term that can describe many kinds of vehicles including “sedan”, “truck”, “pickup”, “SUV”, and more. All these vehicles are specialized cars. A “car” must have an engine, tires, and a driver’s seat no matter what kind of car it is. Therefore, if a “sedan” is a kind of “car”, then a “sedan” must have an engine, tires, and a driver’s seat. Fig 1 “Inheritance” is the act that applies the properties and methods of the “Car” class to the “Sedan” class; thus, an instance of “Sedan” can use members of “Car” without the need to re-define these “Car” members in “Sedan”. A class that is inherited by other classes is called a base class (or parent class). The class that does the inheriting is called a derived class (or child class). In a sense, a derived class is a specialized subset of a base class. Polymorphism is the concept that allows multiple forms of a given method to exist in a class, each of the form is designed for handling a special situation. Polymorphism also allows a child class to override the definition of methods of the parent class. C++ inheritance In C++, programmers create a child class (officially known as “derived class”) with the following syntax to declare the inheritance from a parent class (aka “base class”), where AccessSpecifier is public, private, or protected. By the way, if no AccessModifier is given, private is the default. class childClassName: AccessModifier parentClassName { childClass members; }; The following illustrates how to declare a new class named “Cellphone” as child (derived) class of an existing class named “Telephone”. It is necessary to note that the “Telephone” class which is the parent (based) class must be declared before the child can be declared. class cellphone: public Telephone { } After the declaration, the child class can inherit all of the “shared” members of the base class and create its own members. In other words, “inheritance” is for a child class to gain access to all the public or protected members (such as properties and methods) of the parent class and use them like its own members. Both public and protected members of a parent class are shareable members to the child class, while private members are not shareable. A later section will discuss how these access modifier work in “inheritance”. The advantage of “inheritance” in object-oriented programming is the “sharing of codes”. In a given program, you only have to define what a “car” is ONCE by creating a “Car” class. All the specialized cars, such as “sedan”, “truck”, “pickup”, and “SUV”, can reference to the Car class Car Sedan Truck Pickup SUV
29
Embed
Car Sedan Truck Pickup SUV - Cypress Collegestudents.cypresscollege.edu/cis223/lc09.pdf · Visual C++ Programming – Penn Wu, PhD 251 Lecture #9 Inheritance, Composition, and Polymorphism
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Visual C++ Programming – Penn Wu, PhD 251
Lecture #9 Inheritance, Composition, and Polymorphism
Introduction In terms of object-oriented programming, “inheritance” is the ability that enables new classes to
use the properties and methods of existing classes through the declaration of inheritance. For
example, the word “car” is a general term that can describe many kinds of vehicles including
“sedan”, “truck”, “pickup”, “SUV”, and more. All these vehicles are specialized cars. A “car”
must have an engine, tires, and a driver’s seat no matter what kind of car it is. Therefore, if a
“sedan” is a kind of “car”, then a “sedan” must have an engine, tires, and a driver’s seat.
Fig 1
“Inheritance” is the act that applies the properties and methods of the “Car” class to the “Sedan”
class; thus, an instance of “Sedan” can use members of “Car” without the need to re-define these
“Car” members in “Sedan”.
A class that is inherited by other classes is called a base class (or parent class). The class that
does the inheriting is called a derived class (or child class). In a sense, a derived class is a
specialized subset of a base class.
Polymorphism is the concept that allows multiple forms of a given method to exist in a class,
each of the form is designed for handling a special situation. Polymorphism also allows a child
class to override the definition of methods of the parent class.
C++ inheritance In C++, programmers create a child class (officially known as “derived class”) with the following
syntax to declare the inheritance from a parent class (aka “base class”), where AccessSpecifier is
public, private, or protected. By the way, if no AccessModifier is given, private is the default.
class childClassName: AccessModifier parentClassName
{
childClass members;
};
The following illustrates how to declare a new class named “Cellphone” as child (derived) class
of an existing class named “Telephone”. It is necessary to note that the “Telephone” class which
is the parent (based) class must be declared before the child can be declared.
class cellphone: public Telephone { }
After the declaration, the child class can inherit all of the “shared” members of the base class
and create its own members. In other words, “inheritance” is for a child class to gain access to all
the public or protected members (such as properties and methods) of the parent class and use
them like its own members. Both public and protected members of a parent class are shareable
members to the child class, while private members are not shareable. A later section will discuss
how these access modifier work in “inheritance”.
The advantage of “inheritance” in object-oriented programming is the “sharing of codes”. In a
given program, you only have to define what a “car” is ONCE by creating a “Car” class. All the
specialized cars, such as “sedan”, “truck”, “pickup”, and “SUV”, can reference to the Car class
Car
Sedan Truck Pickup SUV
Visual C++ Programming – Penn Wu, PhD 252
and share the codes defined in the Car class. It is just like saying “a sedan is a car”, “a truck is a
car”, and “a pickup is a car” without the need to define what a car is over and over again.
Class
inheritance
The following is a class named “Car” containing two public instance variables: “mpg” and
“tankSize”. It also has a protected method named mileage() to calculate the mileage, and a
private variable named “securityCode”. The instructor creates this “Car” class to be inherited by
others.
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
ref class Car // base class
{
public: // open to the entire program
int mpg;
int tankSize;
protected:
int mileage()
{
return mpg * tankSize;
}
private: // not open to child class
int securityCode;
};
The following is an example that create the Sedan class as child to the Car class. The Sedan
class also has two public variables of int type: “tireNumber” and “ml”. The default constructor,
Sedan(), defines all the default values of the Sedan class. All the shareable members of the “Car”
class, mpg, tankSize, and mileage(), are used by the Sedan as its own members. In the words,
Sedan “inherits” all sharable members of “Car”. Shareable members of Car are also members of
the Sedan class.
ref class Sedan : public Car // declare Sedan a child of Car
{
public:
int tireNumber;
int ml;
Sedan()
{
tireNumber = 4;
mpg = 34;
tankSize = 16;
ml = mileage();
}
};
The declaration on inheritance forces the Car class to share all the public properties and public
methods with the Sedan class. Programmers do not need to declare variables “mpg” and
“tankSize” and the mileage() method again when they create the Sedan class. In other words, the
“Sedan” class inherits all the public members of the “Car” class.
A child class, even as a child, is still an individual class. The way to declare, create, or add
members of a child class is exactly the same as that of any C++ class. In the following example,
the instructor adds a public method named “TurboIt()”.
Visual C++ Programming – Penn Wu, PhD 253
ref class Sedan : public Car // declare Sedan a child of Car
{
public:
int tireNumber;
int ml;
Sedan()
{
tireNumber = 4;
mpg = 34;
tankSize = 16;
ml = mileage();
}
void TurboIt()
{
mpg = mpg * 1.5;
}
};
Some programmers prefer declaring a class method inside the class body, and then use the scope
resolution operator (::) to define details of the member method outside the class body, as
illustrated below. The scope resolution operator (::), which, in C++, can be used to define the
already declared member functions.
ref class Sedan : public Car // declare Sedan a child of Car
{
public:
int tireNumber;
int ml;
Sedan()
{
tireNumber = 4;
mpg = 34;
tankSize = 16;
ml = mileage();
}
void TurboIt();
};
void Sedan::TurboIt()
{
mpg = mpg * 1.5;
}
To use members in both base (parent) and derived (child) classes, you can create an instance of
Sedan in the main() method, such as “s1” in the following code. You can certainly create an
instance of Car (s2). Interestingly, you can also declare an object as Car (such as “s3”) but create
the object as Sedan.
int main()
{
Sedan^ s1 = gcnew Sedan;
Car^ s2 = gcnew Car;
Car^ s3 = gcnew Sedan;
}
Visual C++ Programming – Penn Wu, PhD 254
The s1 object is instantiated as Sedan, s2 object is instantiated as a Car, and s3 is declared as Car
but is instantiated as Sedan. The difference between s1 and s3 is that s1 can uses all shared
members of the “Car” class through inheritance and every member of the “Sedan” class, while s3
can use all shared members of Car and the default values defined in the Sedan() constructor.
Instance of the “Car” class can only use any member of the “Car” class and cannot access the
Sedan() default constructor.
Object Car Sedan
mpg tankSize mileage() securityCode tireNumber() ml Sedan()
s1 yes yes yes no yes yes yes
s2 yes yes yes no no no no
s3 yes yes yes no no no yes
Once Sedan is declared as a derived class (the child class) of the Car class, you can directly use
properties and methods of the Car class with the “member-of” (->) operator. Technically, all the
public members in the Car class are treated as members of the Sedan class. For example, to the
use mpg property of the Car class from a Sedan object, use:
s1->mpg
To assign a value to the mpg property, use:
s1->mpg = 72;
Similarly, to use a method of the Car class, use:
s1->mileage();
The following is the complete code.
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
ref class Car // base class
{
public: // open to the entire program
int mpg;
int tankSize;
protected:
int mileage()
{
return mpg * tankSize;
}
private: // not open to child class
int securityCode;
};
ref class Sedan : public Car // declare Sedan a child of Car