// -- C ++ shortcut tutorial Reading Notes -- Chapter 15 -- virtual functions and polymorphism (end)
// -- Chapter 15 -- virtual functions and Polymorphism
// -- 04/15/2006 sat.
// -- Computer lab
// -- Liwei
// -- Program #1 indicates the base type pointer
# Include <iostream>
# Include <cstring>
Using namespace STD;
Class B _class {
Char author [80];
Public:
Void put_author (char * s) {strcpy (author, S );}
Void show_author () {cout <author <'/N ';}
};
Class d_class: Public B _class {
Char title [80];
Public:
Void put_title (char * num) {strcpy (title, num );}
Void show_title () {cout <"title:" <title <'/N ';}
};
Int main ()
{
B _class * P, B _ob;
D_class * DP, d_ob;
P = & B _ob;
P-> put_author ("Tom Clancy .");
P-> show_author ();
P = & d_ob;
P-> put_author ("William Shakespeare .");
P-> show_author ();
Cout <"/n ===========================/N ";
B _ob.show_author ();
D_ob.show_author ();
Cout <Endl;
Dp = & d_ob;
DP-> put_title ("The Tempest ");
P-> show_author ();
DP-> show_title ();
(D_class *) P)-> show_title ();
Return 0;
}
// ================================================ ========================================== //
// ==================================== End ======== ========================================== //
// ================================================ ========================================== //
// -- Program #2 Description of virtual functions
# Include <iostream>
Using namespace STD;
Class base {
Public:
Virtual void who () {cout <"base/N ";}
};
Class first_d: public base {
Public:
Void who () {cout <"first derivation/N ";}
};
Class second_d: public base {
Public:
Void who () {cout <"second derivation/N ";}
};
Int main ()
{
Base base_obj;
Base * P;
First_d first_obj;
Second_d second_obj;
P = & base_obj;
P-> who ();
P = & first_obj;
P-> who ();
P = & second_obj;
P-> who ();
Return 0;
}
// ================================================ ========================================== //
// ==================================== End ======== ========================================== //
// ================================================ ========================================== //
// -- Program #3 Description of virtual functions
# Include <iostream>
Using namespace STD;
Class base {
Public:
Virtual void who () {cout <"base/N ";}
};
Class first_d: public base {
Public:
Void who () {cout <"first derivation/N ";}
};
Class second_d: public base {
Public:
// Void who () {cout <"second derivation/N ";}
};
Int main ()
{
Base base_obj;
Base * P;
First_d first_obj;
Second_d second_obj;
P = & base_obj;
P-> who ();
P = & first_obj;
P-> who ();
P = & second_obj;
P-> who ();
Return 0;
}
// ================================================ ========================================== //
// ==================================== End ======== ========================================== //
// ================================================ ========================================== //
// -- Program #4 description of virtual functions
# Include <iostream>
Using namespace STD;
Class base {
Public:
Virtual void who () {cout <"base/N ";}
};
Class first_d: public base {
Public:
Void who () {cout <"first derivation/N ";}
};
Class second_d: Public first_d {
Public:
// Void who () {cout <"second derivation/N ";}
};
Int main ()
{
Base base_obj;
Base * P;
First_d first_obj;
Second_d second_obj;
P = & base_obj;
P-> who ();
P = & first_obj;
P-> who ();
P = & second_obj;
P-> who ();
Return 0;
}
// ================================================ ========================================== //
// ==================================== End ======== ========================================== //
// ================================================ ========================================== //
// -- Program #5 describe the application of virtual functions
# Include <iostream>
Using namespace STD;
Class figure {
Protected:
Double X, Y;
Public:
Void set_dim (double I, Double J) {x = I; y = J ;}
Virtual void show_area () {cout <"No area computation defined for this class./N ";}
};
Class triangle: public figure {
Public:
Void show_area ()
{Cout <"triangle with height" <x <"and base" <Y <"has an area of" <x * 0.5 * Y <". /n ";}
};
Class rectangle: public figure {
Public:
Void show_area ()
{Cout <"rectangle with dimensions" <x <"X" <Y <"has an area of" <x * Y <". /n ";}
};
Int main ()
{
Figure * P;
Triangle T;
Rectangle R;
P = & T;
P-> set_dim (10.0, 5.0 );
P-> show_area ();
P = & R;
P-> set_dim (10.0, 5.0 );
P-> show_area ();
Cout <"/n =============================/N ";
P = & T;
T. set_dim (10.0, 5.0 );
P-> show_area ();
P = & R;
R. set_dim (10.0, 5.0 );
R. show_area ();
Return 0;
}
// ================================================ ========================================== //
// ==================================== End ======== ========================================== //
// ================================================ ========================================== //
// -- Program #6 describe the application of virtual functions
# Include <iostream>
Using namespace STD;
Class figure {
Protected:
Double X, Y;
Public:
Void set_dim (double I, Double J = 0) {x = I; y = J ;}
Virtual void show_area () {cout <"No area computation defined for this class./N ";}
};
Class triangle: public figure {
Public:
Void show_area ()
{Cout <"triangle with height" <x <"and base" <Y <"has an area of" <x * 0.5 * Y <". /n ";}
};
Class rectangle: public figure {
Public:
Void show_area ()
{Cout <"rectangle with dimensions" <x <"X" <Y <"has an area of" <x * Y <". /n ";}
};
Class circle: public figure {
Public:
Void show_area ()
{Cout <"circle with radius" <x <"Han an area of" <3.14 * x <"./N ";}
};
Int main ()
{
Figure * P;
Triangle T;
Rectangle R;
Circle C;
P = & T;
P-> set_dim (10.0, 5.0 );
P-> show_area ();
P = & R;
P-> set_dim (10.0, 5.0 );
P-> show_area ();
P = & C;
P-> set_dim (9.0 );
P-> show_area ();
Cout <"/n =============================/N ";
P = & T;
T. set_dim (10.0, 5.0 );
P-> show_area ();
P = & R;
R. set_dim (10.0, 5.0 );
R. show_area ();
Return 0;
}
// ================================================ ========================================== //
// ==================================== End ======== ========================================== //
// ================================================ ========================================== //
// -- Program #7 describe the application of virtual functions
# Include <iostream>
Using namespace STD;
Class figure {
Protected:
Double X, Y;
Public:
Void set_dim (double I, Double J = 0) {x = I; y = J ;}
Virtual void show_area () = 0;
};
Class triangle: public figure {
Public:
Void show_area ()
{Cout <"triangle with height" <x <"and base" <Y <"has an area of" <x * 0.5 * Y <". /n ";}
};
Class rectangle: public figure {
Public:
Void show_area ()
{Cout <"rectangle with dimensions" <x <"X" <Y <"has an area of" <x * Y <". /n ";}
};
Class circle: public figure {
Public:
// Void show_area ()
// {Cout <"circle with radius" <x <"Han an area of" <3.14 * x <"./N ";}
};
Int main ()
{
Figure * P;
Triangle T;
Rectangle R;
Circle C;
P = & T;
P-> set_dim (10.0, 5.0 );
P-> show_area ();
P = & R;
P-> set_dim (10.0, 5.0 );
P-> show_area ();
P = & C;
P-> set_dim (9.0 );
P-> show_area ();
Cout <"/n =============================/N ";
P = & T;
T. set_dim (10.0, 5.0 );
P-> show_area ();
P = & R;
R. set_dim (10.0, 5.0 );
R. show_area ();
P = & C;
C. set_dim (9.0, 5.0 );
P-> show_area ();
Return 0;
}
// ================================================ ========================================== //
// ==================================== End ======== ========================================== //
// ================================================ ========================================== //