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