以下不正确的程序段是int main() { class A { public:int v; }; A *p=new A; p->v=4; delete p; return 0; } B. int main() { class A { public:int v;A *p; }; A a; a.p=&a; return 0; } C. int main() { class A { int v; }; A a; a.v = 3; return 0; } D. int main() { class A { public:int v; A *p; }; A a; a.p=new A; delete a.p; return 0; }
时间: 2023-07-14 19:13:50 浏览: 52
以下不正确的程序段是B. int main() { class A { public:int v;A *p; }; A a; a.p=&a; return 0; }。因为这里将一个类的实例的指针赋值给了该实例的指针成员,这样会导致循环引用和内存泄漏。因为当这个实例被销毁时,它指向自己的指针成员不会被自动销毁,所以会造成内存泄漏。
相关问题
用c++代码写出下列程序的输出结果为二请将程序补充完整 class Base Leublis void Basefun class Derived: public Base Leublie int main l ¿Derived *p= new Derived: return 0;1
以下是对程序进行补充后的完整代码,并解释输出结果:
```c++
#include <iostream>
using namespace std;
class Base {
public:
void Basefun() { cout << "Basefun" << endl; }
virtual void Basevir() { cout << "Basevir" << endl; }
};
class Derived: public Base {
public:
void Derivedfun() { cout << "Derivedfun" << endl; }
virtual void Derivedvir() { cout << "Derivedvir" << endl; }
};
int main() {
Derived *p = new Derived;
Base *pb = p;
pb->Basefun(); // 输出 Basefun
pb->Basevir(); // 输出 Derivedvir
// pb->Derivedfun(); // 编译错误,Base 类型的指针无法调用 Derived 类的成员函数
// pb->Derivedvir(); // 编译错误,Base 类型的指针无法调用 Derived 类的成员函数
return 0;
}
```
程序定义了一个 `Base` 类和一个 `Derived` 类,`Derived` 类继承自 `Base` 类。在 `main()` 函数中,创建了一个 `Derived` 类型的指针 `p`,并将其赋值给一个 `Base` 类型的指针 `pb`。然后通过 `pb` 指针调用了 `Basefun()` 和 `Basevir()` 两个成员函数。
由于 `Basevir()` 函数在 `Base` 类中被声明为虚函数,因此在运行时,将根据实际对象的类型来调用相应类型的函数。虽然 `pb` 指针的类型是 `Base*`,但它指向的实际对象是 `Derived` 类型的,因此调用 `Basevir()` 函数时将会调用 `Derivedvir()` 函数。因此程序的输出结果为:
```
Basefun
Basevir
```
#include <iostream> using namespace std; class MyClass { int* p; const int N; public: // ERROR **********found********** MyClass(int val) : N=1 { p = new int; *p = val; } // ERROR **********found********** ~MyClass() { delete *p; } friend void print(MyClass& obj); }; // ERROR **********found********** void MyClass::print(MyClass& obj) { cout << "value = " << *(obj.p) << endl; cout << "number = " << obj.N << endl; } int main() { MyClass obj(63); print(obj); return 0; }
在这段代码中,有三个错误:
1. 在 MyClass 类的构造函数中,使用了 const 成员初始化列表,但是 N 是一个非静态成员变量,不能在初始化列表中初始化。正确的做法是在构造函数的函数体中给 N 赋值。
2. 在 MyClass 类的析构函数中,使用了 delete *p,但是 p 是一个指向 int 的指针,应该使用 delete p。
3. 在 MyClass 类的友元函数 print() 中,应该将其定义为 MyClass 类的成员函数,而不是独立的函数。
下面是修改后的代码:
```cpp
#include <iostream>
using namespace std;
class MyClass {
int* p;
const int N;
public:
MyClass(int val) : N(1) {
p = new int;
*p = val;
}
~MyClass() { delete p; }
void print() {
cout << "value = " << *p << endl;
cout << "number = " << N << endl;
}
};
int main() {
MyClass obj(63);
obj.print();
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)