//--《C++捷径教程》读书笔记--Chapter 15--虚函数与多态(完结)
//--Chapter 15--虚函数与多态
//--04/15/2006 Sat.
//--Computer Lab
//--Liwei
//--程序#1 说明基类型指针
#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================================//
//=================================================================//
//--程序#2 说明虚函数
#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================================//
//=================================================================//
//--程序#3 说明虚函数
#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================================//
//=================================================================//
//--程序#4 说明虚函数
#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================================//
//=================================================================//
//--程序#5 说明虚函数的应用
#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================================//
//=================================================================//
//--程序#6 说明虚函数的应用
#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*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================================//
//=================================================================//
//--程序#7 说明虚函数的应用
#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*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================================//
//=================================================================//