[ Pobierz całość w formacie PDF ]

initialise them in the normal constructor code.
Finally, after the member initialisers are all executed in the proper order,
the main body of the constructor is executed in the normal manner.
Using the new class
The next example program [USEDTTM.CPP] uses the datetime class
we just built, and like our previous examples, the main program is kept
very simple and straight forward. You will note that the default
constructor is used for the object named now, and the constructor with
the member initialisers is used with the objects named birthday and
special.
#include
#include "datetime.h"
datetime now, birthday(10, 18, 1938, 1, 30, 17);
datetime special( 2, 19, 1950, 13, 30, 0);
void main()
{
cout
cout
cout
}
Exercise 6
Describe how potential name conflicts for variables and methods are
handled in classes employing multiple inheritance.
Page 819-35
Module 819 INHERITANCE AND VIRTUAL FUNCTIONS IN C++
Objective 7 After working through this section you should be able to define virtual
functions in C++
Objects are polymorphic (exhibit polymorphism) if they have some
similarities but are still somewhat different. We have already studied
operator overloading and function overloading in this and earlier
Modules, and they are a subtle form of polymorphism since in both
cases, a single entity is used to refer to two or more things. The use of
virtual functions can be a great aid in programming some kinds of
projects as you will see in these two sections.
A simple program with inheritance
Examine the first example program [VIRTUAL1.CPP] for the basic
program outline we will use for all discussion in this section. Since this
program has nothing to do with virtual functions, the name may be
somewhat misleading; it is so named because it is part of a series of
programs intended to illustrate the use of virtual functions. The last
program in this section will illustrate the proper use of virtual functions.
#include
class vehicle
{
int wheels;
float weight;
public:
void message(void)
{
cout
}
};
class car : public vehicle
{
int passenger_load;
public:
void message(void)
{
cout
}
};
class truck : public vehicle
{
int passenger_load;
float payload;
public:
int passengers(void)
{
return passenger_load;
}
};
class boat : public vehicle
{
int passenger_load;
public:
int passengers(void)
{
return passenger_load;
}
void message(void)
{
Page 819-36
Module 819 INHERITANCE AND VIRTUAL FUNCTIONS IN C++
cout
}
};
void main()
{
vehicle unicycle;
car sedan;
truck semi;
boat sailboat;
unicycle.message();
sedan.message();
semi.message();
sailboat.message();
}
This first program is very simple and you will recognise it as being
somewhat similar to the programs studied in the last section except that
this program is greatly simplified in order to focus on the virtual
function. You will notice that many of the methods from the last section
have been dropped from this example for simplicity, and a new method
has been added to the parent class, the method named message( ) in line
7.
Throughout this section we will be studying the operation of the
method named message( ) in the base class and the derived classes. For
that reason, there is a method named message( ) in the car class as well
as in the new class named boat in lines 31 to 43. You will also notice
that there is a lack of a method named message( ) in the truck class.
This has been done on purpose to illustrate the use of the virtual
method, or if you prefer, you can refer to it as a virtual function. You
will recall that the method named message( ) from the base class is
available in the truck class because the method from the base class is
inherited with the keyword public included in line 15. You will also
notice that the use of the keyword public in lines 12 and 21 actually do
nothing because the only method available in the base class is also
available in the derived classes. There are no methods actually inherited.
Leaving the keyword in the header poses no problem however, so it will
be left there for your study.
The method named message( ) in the base class and in the derived
classes has been kept very simple on purpose. Once again, we are
interested in the technique of the virtual method rather than a long
complicated example.
The main program is as simple as the classes, one object of each of the
classes is declared in lines 46 to 49 and the method named message( ) is
called once for each object. The result of executing the program
indicates that the method for each is called except for the object named [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • imuzyka.prv.pl
  •