一个c++的小程序,帮我看下运行结果

来源:百度知道 编辑:UC知道 时间:2024/06/08 06:40:35
如下:
#include<iostream.h>
using namespace std;
class Base
{
int a;
public:
Base(int x) {a=x;}
void show() {cout<<a;}
};
class Derived :public Base
{
int b;
public:
Derived(int x):Base(i+1),b(i){}
void show(){cout<<b;}
};
int main ()
{
Base b(5),*pb;
Derived d(1);
pb=&d;
pb->show();
return 0;
}
解释一下,呵呵,我运行时有一个错误。

输出:
2

Derived(int x):Base(i+1),b(i){}//这一句的x改成i就好

1.<iostream>包含了一系列模板化的I/O类,相反地<iostream.h>只仅仅是支持字符
流。另外,输入输出流的C++标准规范接口在一些微妙的细节上都已改进,因此,
<iostream>和<iostream.h>在接口和执行上都是不同的。最后,<iostream>的各组
成都是以STL的形式声明的,然而<iostream.h>的各组成都是声明成全局型的。

因为这些实质上的不同,你不能在一个程序中混淆使用这两个库。做为一种习
惯,在新的代码中一般使用<iostream>,但如果你处理的是过去编写的代码,为了
继承可以用继续用<iostream.h>旧保持代码的一致性。

2. Derived(int x):Base(i+1),b(i){} i没有声明

Derived(int x,in i):Base(i+1),b(i){}

3.修改后的代码
#include<iostream>
using namespace std;
class Base
{
int a;
public:
Base(int x) {a=x;}
void show() {cout<<a;}
};
class Derived :public Base
{
int b;
public:
Derived(int x,int i):Base(i+1),b(i){}
void show(){cout<<b;}
};
int main ()
{
Base b(5),*pb;
Derived d(1,7);
pb=&d;
pb-&