《C++捷径教程》读书笔记--Chapter 14--继承(完结)
//--《C++捷径教程》读书笔记--Chapter 14--继承(完结)
//--Chapter 14--继承
//--04/14/2006 Friday
//--Computer Lab
//--Liwei
//--程序#1 说明继承的使用
#include <iostream>
using namespace std;
class road_vehicle{
int wheels;
int passengers;
public:
void set_wheels(int num) { wheels=num; }
int get_wheels() { return wheels; }
void set_pass(int num) { passengers=num; }
int get_pass() { return passengers;}
};
class truck: public road_vehicle{
int cargo;
public:
void set_cargo(int size) { cargo=size; }
int get_cargo() { return cargo; }
void show();
};
//-----
enum type { car, van, wagon};
class automobile:public road_vehicle{
enum type car_type;
public:
void set_type( type t) { car_type=t; }// this
enum type get_type() { return car_type; }
void show();
};
void truck::show()
{
cout<<"Wheels: "<<get_wheels()<<'/n';
cout<<"Passengers: "<<get_pass()<<'/n';
cout<<"Cargo capacity in cubic feet: "<<cargo<<'/n';
//---
}
void automobile::show()
{
cout<<"Wheels: "<<get_wheels()<<'/n';
cout<<"Passengers: "<<get_pass()<<'/n';
cout<<"type: ";
switch( get_type() ){
case van: cout<<"van./n";
break;
case car: cout<<"car./n";
break;
case wagon: cout<<"wagon./n";
}
}
int main()
{
truck t1,t2;
automobile c;
t1.set_wheels(18);
t1.set_pass(2);
t1.set_cargo(3200);
t2.set_wheels(6);
t2.set_pass(3);
t2.set_cargo(1200);
t1.show();
cout<<endl;
t2.show();
cout<<endl;
c.set_wheels(4);
c.set_pass(6);
c.set_type(van);
c.show();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#2
#include <iostream>
using namespace std;
class base{
int i,j;
public:
void set(int a, int b) { i=a; j=b;}
void show() { cout<<i<<' '<<j<<'/n'; }
};
class derived:public base{
int k;
public:
derived(int x) { k=x; }
void showk() { cout<<k<<'/n'; }
};
int main()
{
derived ob(3);
ob.set(1,2);
ob.show();
ob.showk();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#3
#include <iostream>
using namespace std;
class base{
int i,j;
public:
void set(int a, int b) { i=a; j=b;}
void show() { cout<<i<<' '<<j<<'/n'; }
};
class derived:private base{
int k;
public:
derived(int x) { k=x; }
void showk() { cout<<k<<'/n'; }
};
int main()
{
derived ob(3);
// ob.set(1,2);
// ob.show();
ob.showk();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#4
#include <iostream>
using namespace std;
class base{
protected:
int i,j;
public:
void set(int a, int b) { i=a; j=b;}
void show() { cout<<i<<' '<<j<<'/n'; }
};
class derived:public base{
int k;
public:
void setk() { k=i*j; }
void showk() { cout<<k<<'/n'; }
};
int main()
{
derived ob;
ob.set(2,3);
ob.show();
ob.setk();
ob.showk();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#5
#include <iostream>
using namespace std;
class base{
protected:
int i,j;
public:
void set(int a, int b) { i=a; j=b;}
void show() { cout<<i<<' '<<j<<'/n'; }
};
class derived1:public base{
int k;
public:
void setk() { k=i*j; }
void showk() { cout<<k<<'/n'; }
};
class derived2:public derived1{
int m;
public:
void setm() { m=i-j; }
void showm() { cout<<m<<'/n'; }
};
int main()
{
derived1 ob1;
derived2 ob2;
ob1.set(2,3);
ob1.show();
ob1.setk();
ob1.showk();
ob2.set(3,4);
ob2.show();
ob2.setk();
ob2.showk();
ob2.setm();
ob2.showm();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#6
#include <iostream>
using namespace std;
class base{
protected:
int i,j;
public:
void set(int a, int b) { i=a; j=b;}
void show() { cout<<i<<' '<<j<<'/n'; }
};
class derived1:private base{
int k;
public:
void setk() { k=i*j; }
void showk() { cout<<k<<'/n'; }
};
class derived2:public derived1{
int m;
public:
// void setm() { m=i-j; }
void showm() { cout<<m<<'/n'; }
};
int main()
{
derived1 ob1;
derived2 ob2;
ob1.set(1,2);
ob1.show();
ob2.set(3,4);
ob2.show();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#7 说明以保护方式继承
#include <iostream>
using namespace std;
class base{
int i;
protected:
int j;
public:
int k;
void seti(int a) { i=a; }
int geti() { return i; }
};
class derived: protected base{
public:
void setj(int a) { j=a; }
void setk(int a) { k=a; }
int getj() { return j;}
int getk() { return k; }
};
int main()
{
derived ob;
// ob.seti(10);
// cout<<ob.geti();
// ob.k=11;
ob.setk(10);
cout<<ob.getk()<<' ';
ob.setj(12);
cout<<ob.getj()<<' ';
cout<<endl;
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#8 说明多继承
#include <iostream>
using namespace std;
class base1{
protected:
int x;
public:
void showx() { cout<<x<<endl;}
};
class base2{
protected:
int y;
public:
void showy() { cout<<y<<endl; }
};
class derived: public base1,public base2{
public:
void set(int i, int j) { x=i; y=j; }
};
int main()
{
derived ob;
ob.set(11,12);
ob.showx();
ob.showy();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#9 说明构造和析构
#include <iostream>
using namespace std;
class base{
//public:
protected:
base() { cout<<"Constructing base./n"; }
~base() { cout<<"Destructing base./n"; }
};
class derived: public base {
public:
derived() { cout<<"Constructing derived./n"; }
~derived() { cout<<"Destructing derived./n"; }
};
int main()
{
derived ob;
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#10 说明构造和析构
#include <iostream>
using namespace std;
class base{
public:
//protected:
base() { cout<<"Constructing base./n"; }
~base() { cout<<"Destructing base./n"; }
};
class derived1: public base {
public:
derived1() { cout<<"Constructing derived1./n"; }
~derived1() { cout<<"Destructing derived1./n"; }
};
class derived2: public derived1 {
public:
derived2() { cout<<"Constructing derived2./n"; }
~derived2() { cout<<"Destructing derived2./n"; }
};
int main()
{
derived2 ob;
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#11 说明构造和析构
#include <iostream>
using namespace std;
class base1{
public:
//protected:
base1() { cout<<"Constructing base1./n"; }
~base1() { cout<<"Destructing base1./n"; }
};
class base2{
public:
//protected:
base2() { cout<<"Constructing base2./n"; }
~base2() { cout<<"Destructing base2./n"; }
};
class derived: public base1,public base2 {
public:
derived() { cout<<"Constructing derived./n"; }
~derived() { cout<<"Destructing derived./n"; }
};
int main()
{
derived ob;
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#12 说明传递参数给基类
#include <iostream>
using namespace std;
class base{
protected:
int i;
public:
//protected:
base(int x) { i=x; cout<<"Constructing base./n"; }
~base() { cout<<"Destructing base./n"; }
};
class derived:public base{
int j;
public:
derived(int x, int y): base(y)
{ j=x; cout<<"Constructing derived./n"; }
~derived() { cout<<"Destructing derived./n"; }
void show() { cout<<i<<' '<<j<<endl; }
};
int main()
{
derived ob(3,4);
ob.show();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#13 说明传递参数给基类
#include <iostream>
using namespace std;
class base1{
protected:
int i;
public:
base1(int x) { i=x; cout<<"Constructing base1./n"; }
~base1() { cout<<"Destructing base1./n"; }
};
//----
class base2{
protected:
int k;
public:
base2(int x) { k=x; cout<<"Constructing base2./n"; }
~base2() { cout<<"Destructing base2./n"; }
};
//----
class derived:public base1, public base2{
int j;
public:
derived(int x, int y, int z): base1(y),base2(z)
{ j=x; cout<<"Constructing derived./n"; }
~derived() { cout<<"Destructing derived./n";}
void show() { cout<<" i:"<<i<<"/n j:"<<j<<"/n k:"<<k<<endl;}
};
int main()
{
derived ob(3,4,5);
ob.show();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#14 说明传递参数给基类
#include <iostream>
using namespace std;
class base1{
protected:
int i;
public:
base1(int x) { i=x; cout<<"Constructing base1./n"; }
~base1() { cout<<"Destructing base1./n"; }
};
//----
class base2{
protected:
int k;
public:
base2(int x) { k=x; cout<<"Constructing base2./n"; }
~base2() { cout<<"Destructing base2./n"; }
};
//----
class derived: public base1, public base2{
int j;
public:
derived(int y, int z): base1(y),base2(z)
{ j=y*z ; cout<<"Constructing derived./n"; }
~derived() { cout<<"Destructing derived./n";}
void show() { cout<<" i:"<<i<<"/n j:"<<j<<"/n k:"<<k<<endl;}
};
int main()
{
derived ob(3,4);
ob.show();
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#15 说明访问声明
#include <iostream>
using namespace std;
class base{
int i;
public:
int j,k;
void seti(int x) { i=x; }
int geti() { return i; }
};
class derived: private base{
public:
base::j;
base::seti;
base::geti;
// base::i;
int a;
};
int main()
{
derived ob;
//ob.i=10;
ob.j=20;
ob.a=40;
ob.seti(10);
cout<<ob.geti()<<' '<<ob.j<<' '<<ob.a<<endl;
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#16 说明虚基类
#include <iostream>
using namespace std;
class base{
public:
int i;
};
class derived1: public base{
public:
int j;
};
class derived2: public base{
public:
int k;
};
class derived3: public derived1, public derived2{
public:
int sum;
};
int main()
{
derived3 ob;
// ob.i=10;
ob.j=20;
ob.k=30;
// ob.sum=ob.i+ob.j+ob.k;
// cout<<ob.i<<endl;
cout<<ob.j<<' '<<ob.k<<endl;
// cout<<ob.sum<<endl;
}
//=================================================================//
// END //
//=================================================================//
//--程序#17 说明虚基类
#include <iostream>
using namespace std;
class base{
public:
int i;
};
class derived1: public base{
public:
int j;
};
class derived2: public base{
public:
int k;
};
class derived3: public derived1, public derived2{
public:
int sum;
};
int main()
{
derived3 ob;
ob.derived1::i=10;
ob.j=20;
ob.k=30;
ob.sum=ob.derived1::i+ob.j+ob.k;
cout<<ob.derived1::i<<endl;
cout<<ob.j<<' '<<ob.k<<endl;
cout<<ob.sum<<endl;
cout<<"====================/n";
ob.derived2::i=100;
ob.sum=ob.derived2::i+ob.j+ob.k;
cout<<ob.derived2::i<<endl;
cout<<ob.j<<' '<<ob.k<<endl;
cout<<ob.sum<<endl;
return 0;
}
//=================================================================//
// END //
//=================================================================//
//--程序#18 说明虚基类
#include <iostream>
using namespace std;
class base{
public:
int i;
};
class derived1: virtual public base{
public:
int j;
};
class derived2: virtual public base{
public:
int k;
};
class derived3: public derived1, public derived2{
public:
int sum;
};
int main()
{
derived3 ob;
ob.i=10;
ob.j=20;
ob.k=30;
ob.sum=ob.i+ob.j+ob.k;
cout<<ob.i<<endl;
cout<<ob.j<<' '<<ob.k<<endl;
cout<<ob.sum<<endl;
cout<<"====================/n";
derived1 lw;
lw.i=999;
cout<<lw.i<<endl;
cout<<ob.i<<endl;
return 0;
}
//=================================================================//
// END //
//=================================================================//
《C++捷径教程》读书笔记--Chapter 14--继承(完结)相关推荐
- 《C++捷径教程》读书笔记--Chapter 16--模板(完结)
//--<C++捷径教程>读书笔记--Chapter 16--模板(完结) //--Chapter 16--模板 //--04/16/2006 Sun. //--Computer Lab ...
- 《C++捷径教程》读书笔记--Chapter 10--结构与联合
//--<C++捷径教程>读书笔记--Chapter 10--结构与联合 //--Chapter 10--结构与联合 //--11/24/2005 Thurs. //--Computer ...
- Oracle PL/SQL 程序设计读书笔记 - 第14章 DML和事务管理
Oracle PL/SQL 程序设计读书笔记 - 第14章 DML和事务管理 Oracle PL/SQL 程序设计读书笔记 - 第14章 DML和事务管理 ACID原则:即一个事务具有原子性.一致性. ...
- 电磁兼容工程(Electromagnetic compatibility engineering Herry Ott )读书笔记-- 章14 抗射频和瞬态信号干扰能力
1, 继续对Henry W Ott 写的<电磁兼容工程>这本书进行读书笔记记录. 强烈推荐英文原版,原版可能更容易读懂. 2,本博客是这本书的读书笔记,它不是对书的直接翻译,主要记录阅读这 ...
- 嵌入式Linux基础教程-读书笔记
waiting to be fixed. coming soon. +读书笔记: +linux kernel <Linux内核完全剖析基于0.12内核>.pdf 嵌入式Linux基础教程第 ...
- 廖雪峰Git教程读书笔记
因为多人协作导致的不确定因素太多,git需要处理各种各样的情况,除了在多人协作开发过程中用到的常用git命令之外,其它一些不常见的命令的原理和命令都能够在廖老师的教程中给予解答,光通过度娘看解决方法是 ...
- [The Path to QUANT] 《Volatility Trading》by Euan Sinclair 读书笔记 Chapter 3
<Volatility Trading> by Euan Sinclair Chapter 3 收益率和波动率的典型事实 典型事实列表 波动率并非常数 收益率分布 成交量和波动率 波动率分 ...
- 《C#本质论》读书笔记(14)支持标准查询操作符的集合接口
14.2.集合初始化器 使用集合初始化器,程序员可以采用和数组相似的方式,在集合的实例化期间用一套初始的成员来构造这个集合.如果没有集合初始化器,就只有在集合实例化后才能显示添加到集合中--例如使用 ...
- Objective-C基础教程读书笔记(8)
第8章 Foundation Kit介绍 Objective-C是一门非常精巧实用的语言,目前我们还没有研究完它提供的全部功能.不过现在,我们先探索另一个方向,快速了解一下Cocoa中的Foundat ...
最新文章
- ICML 2020论文贡献榜排名出炉:Google单挑斯坦福、MIT、伯克利;清华进TOP 20
- jQuery UI Widget(1.8.1)工作原理--转载
- 学习笔记Hadoop(十三)—— MapReduce开发入门(1)—— MapReduce开发环境搭建、MapReduce单词计数源码分析
- idea java8_太赞了,Intellij IDEA 竟然把 Java8 的数据流问题这么完美的解决掉了!...
- python gdb coredump_Linux段错误及GDB Coredump调试方法
- POJ - 3415 Common Substrings(长度不小于K的公共子串个数)
- Spring Tools 4 for Eclipse 下载
- 上一次系统的关闭是意外的_教你如何一键极速重装系统
- 数据结构java学生成绩排序_数据结构学习--Java简单排序
- JavaWeb之HTTP协议
- mac android 手机连接打印机,线上就能解决苹果手机怎么连接打印机问题
- csgo 机器人模式_csgo怎么加机器人
- java泡泡屏保,js 模拟气泡屏保效果代码
- js调用exe程序,bs调用cs客户端
- 【蓝凌表单】流程表单JS汇总
- 网课搜题API接口搭建教程
- 小米笔记本 air 12.5寸 支持硬盘参数
- html网页生成动态地图
- 如是使用JS实现页面内容随机显示
- win10如何添加或禁用开机自启动项