C 习题和解析(继承和派生-03)
2008-02-23 05:34:26来源:互联网 阅读 ()
#include<iostream.h>
class base
{
int n;
public:
base(){};
base (int a)
{
cout << "constructing base class" << endl;
n=a;
cout << "n=" << n << endl;
}
~base() { cout << "destructing base class" << endl; }
};
class subs : public base
{
int m;
public:
subs(int a, int b) : base(a)
{
cout << "constructing sub class" << endl;
m=b;
cout << "m=" << m << endl;
}
subs() { cout << "destructing sub class" << endl; }
};
void main ()
{
subs s(1,2);
}
解:
这里base 是基类,subs为派生类,subs类的构造函数中含有调用基本类的构造函数。
所以输出为:
constructing base class
n=1
constructing sub class
n=2
destructing base class
destructing sub class
-----------------------------------------------------
题 2.分析以下程式的执行结果:
#include <iostream.h>
class Sample
{
protected:
int x;
public:
Sample() { x=0; }
Sample(int val) { x=val; }
void operator () { x ; }
};
class Derived:public Sample
{
int y;
public:
Derived():Sample(){ y=0; }
Derived(int val1,int val2):Sample(val1){ y=val2; }
void operator--(){ x--;y--;}
void disp()
{
cout<<"x="<< x <<" y=" << y << endl; f
}
};
void main ()
{
Derived d(3,5);
d.disp();
d ;
d.disp ();
d--;
d--;
d.disp();
}
解:
任何运算都是对x而言的。
所以输出为:
x=3, y=5
x=4,y=5
x=2,y=5
--------------------------------------------------------------
题 3 分析以下程式的执行结果:
#include <iostream.h>
class A
{
int a;
public:
A(int i) { a=i;cout << "constructing class A" << endl; }
void print() { cout << a << endl; }
~A() { cout << "destructing class A" << endl; }
};
class Bi:public A
{
int bl;
public:
Bl(int i,int j):A(i)
{
bl=j;cout << "constructing class BI" << endl;
void print()
{
A::print ();
cout << bl << endl;
}
~BI(){ cout << "destructing class BI" << endl; }
};
class B2:public A
{
int b2;
public:
B2(int i,int j):A(i);
{
b2=j;cout << "constructing class B2" << endl;
}
void print()
{
A::print ();
cout << b2 << endl;
}
~B2() { cout << "destructing class B2" << endl; }
};
class C:public B1,public B2
{
int c;
public:
C(int i,int j,int k, int 1,int m) :Bl(i,j),B2(k,1),c(m)
{
cout << "constructing class C" << endl;
}
void print()
{
Bl::print();
B2::print();
cout << c << endl;
}
~C( ){ cout << "destructing class C" << endl; }
};
void main()
{
C c1(1,2,3,4,5);
cl.print();
}
解:
C类是从B1类和B2类派生的,而B1和B2类又都是从A类派生,但各有自己的副本,任何这些成员函数均有print()成员函数。所以,在C的成员函数实现中,调用print()时,要加上类作用域运算符“::”。
所以输出为:
constructing class A
constructing class B1
constructing class A
constructing class B2
constructing class C
1
2
3
4
5
destructing class C
destructing class B2
destructing class A
destructing class B1
destructing class A
-------------------------------------------------------
题 4 分析以下程式执行结果:
#include <iostream.h>
class A
{
public:
A(char *s) { cout << s << endl; }
~A() {}
};
class B:public A
{
public:
B(char *sl,char *s2) :A(sl)
{
cout << s2 << endl;
}
};
class C:public A
{
public:
C(char *sl,char *s2) :A(sl)
{
cout << s2 << endl;
}
};
class D:public B,public C
{
public:
D(char *sl,char *s2,char *s3,char *s4) :B(sl,s2),C(sl,s3)
{
cout << s4 << endl;
}
void main ()
{
D d("class A","class B","class C","class D");
}
解:
D类是从B和C类派生的,而B和C类又都是从A类派生,但各有自己的副本。
所以输出为:
class A
class B
class A
class C
class D
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇: C 习题和解析(继承和派生-02)
下一篇: 六大代码问题检验您的Java知识
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash