#include<iostream> using namespace std; class Base public: virtual void Show( )cout<<'B';; ,; class Derived:public Base 1 public: void Show( ) cout< <'D';- ,; int main( ) 1 Base *pl=new Derived; Derived *p2=new Derived; p1->Show0; p2->Show0; delete p1; delete p2; return 0; } 运行这个程序的输出结果是_
时间: 2024-01-18 10:05:07 浏览: 89
这个程序存在语法错误,无法通过编译。具体地说,class Base的定义缺少了一个左花括号,应该修改为class Base{。同时,class Derived的定义也缺少了一个左花括号,应该修改为class Derived:public Base{。修改后的程序如下:
```c++
#include<iostream>
using namespace std;
class Base {
public:
virtual void Show() {
cout << 'B';
}
};
class Derived:public Base {
public:
void Show() {
cout << 'D';
}
};
int main() {
Base *p1 = new Derived;
Derived *p2 = new Derived;
p1->Show();
p2->Show();
delete p1;
delete p2;
return 0;
}
```
运行这个程序的输出结果是:
```
D
D
```
相关问题
#include<iostream> using namespace std; , class Base 1 public: virtual void Show( ) cout<< <'B';; }; class Derived:public Base 1 public: void Show( ) cout< <'D'; ,; int main( ) 1 Base *pl =new Derived; Derived *p2=new Derived; p1->Show0; p2-> ShowO; delete pl; delete p2; return 0; }运行这个程序的输出结果是
程序存在语法错误,正确的程序应该是这样的:
```
#include<iostream>
using namespace std;
class Base1 {
public:
virtual void Show() { cout << 'B'; }
};
class Derived: public Base1 {
public:
void Show() { cout << 'D'; }
};
int main() {
Base1* p1 = new Derived;
Derived* p2 = new Derived;
p1->Show();
p2->Show();
delete p1;
delete p2;
return 0;
}
```
程序的输出结果是:
```
DD
```
解释如下:
1. 首先定义了一个名为 Base1 的基类,其中定义了一个虚函数 Show(),输出字符 'B'。
2. 接着定义了一个名为 Derived 的派生类,继承了 Base1,同时重写了 Show() 函数,输出字符 'D'。
3. 在主函数中,分别用基类指针和派生类指针分别指向一个 Derived 对象。
4. 调用基类指针的 Show() 函数,由于是虚函数,会调用 Derived 类中的函数,输出字符 'D'。
5. 调用派生类指针的 Show() 函数,直接调用派生类中的函数,输出字符 'D'。
6. 最后释放内存。
using namespace std; class Base1 { public: //下列语句需要声明纯虚函数Show ▁▁▁(3分); }; class Base2 { protected: char * _p; Base2(const char *s) { _p = new char[strlen(s) + 1]; //下列语句将形参指向的字符串常量复制到该类的字符数组中 ▁▁▁(3分); } ~Base2() { delete [] _p; } }; //Derived类公有继承Base1,私有继承Base2类 class Derived :▁▁▁(3分) { public://以下构造函数调用Base2类构造函数 Derived(const char *s) :▁▁▁(3分) { } void Show() { cout << _p << endl; } }; int main() { Base1 *pb = new Derived("I'm a derived class."); pb->Show(); delete pb; return 0; }
完整代码如下:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class Base1 {
public:
virtual void Show() = 0;
};
class Base2 {
protected:
char *_p;
Base2(const char *s) {
_p = new char[strlen(s) + 1];
strcpy(_p, s);
}
~Base2() { delete [] _p; }
};
class Derived : public Base1, private Base2 {
public:
Derived(const char *s) : Base2(s) {}
void Show() { cout << _p << endl; }
};
int main() {
Base1 *pb = new Derived("I'm a derived class.");
pb->Show();
delete pb;
return 0;
}
```
输出结果为:
```
I'm a derived class.
```
在此程序中,类Base1中声明了纯虚函数Show(),因此该类为抽象基类。类Base2中定义了一个字符数组_p,并在构造函数中对其进行了初始化。类Derived公有继承自Base1和私有继承自Base2。在类Derived的构造函数中调用了Base2的构造函数,将形参指向的字符串常量复制到_p所指向的字符数组中。在类Derived中重写了Show()函数,输出_p所指向的字符串常量。在主函数中,我们首先创建了一个Derived类的对象,并将其转化为Base1类的指针。由于Base1类中声明了纯虚函数Show(),因此Derived类必须实现该函数。在调用pb->Show()时,输出"I'm a derived class."。最后,我们释放了pb所指向的内存空间。
阅读全文