《C++捷径教程》读书笔记--Chapter 15--虚函数与多态(完结)

//--《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================================//
//=================================================================//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值