class A { public: int a = 100; void func() {cout << "A 成员函数"} }; class B { public: int b = 200; void func() { cout << "B 成员函数" } }; A *a1; B b1; a1 = &b1; a1->func(); b1.func();
时间: 2024-01-13 18:03:31 浏览: 37
这段代码中存在一些语法错误,应该修改为以下代码:
```
#include <iostream>
using namespace std;
class A {
public:
int a = 100;
void func() { cout << "A 成员函数" << endl; }
};
class B {
public:
int b = 200;
void func() { cout << "B 成员函数" << endl; }
};
int main() {
A* a1;
B b1;
a1 = &b1;
a1->func();
b1.func();
return 0;
}
```
此时的输出结果应该为:
```
A 成员函数
B 成员函数
```
因为 `a1` 是一个指向 `A` 类型的指针,但是却指向了 `B` 类型的对象 `b1`。当调用 `a1->func()` 时,由于 `a1` 的静态类型是 `A*`,因此编译器会将 `a1->func()` 解释为调用 `A` 类型的 `func()` 函数,但是实际上 `a1` 指向的是 `B` 类型的对象 `b1`,因此会调用 `B` 类型的 `func()` 函数,输出字符串 `"B 成员函数"`。而当调用 `b1.func()` 时,直接调用 `B` 类型的 `func()` 函数,输出字符串 `"B 成员函数"`。
相关问题
#include <iostream> using namespace std; class Base { public: virtual void Func1(uint32_t p = 100) { cout << p << endl; } void Func2(uint32_t p = 100) { cout << p << endl; } }; class Derive : public Base { public: void Func1(uint32_t p = 200) { cout << p << endl; } void Func2(uint32_t p = 200) { cout << p << endl; }; int main(void) { Base *d = new Derive(); d->Func1(); //因为基类和派生类函数参数不一样,所以不构成覆盖 d->Func2(); //基类中Func2没有virtual关键字,不构成覆盖 return 0; }
在上述代码中,有两个函数`Func1()`和`Func2()`,分别位于基类`Base`和派生类`Derive`中。我们来逐个分析它们的特点和调用情况:
1. `Func1()`函数:
- 基类`Base`中的`Func1()`函数是虚函数,可以被派生类覆盖。
- 派生类`Derive`中的`Func1()`函数与基类中的函数具有相同的名称和参数类型,但有一个默认参数值不同。
- 在`main()`函数中,使用基类指针`d`指向派生类对象,并调用`d->Func1()`。
- 由于基类和派生类的函数参数不一样(派生类中有一个默认参数值为200),所以这里不构成覆盖。实际上调用的是基类中的`Func1()`函数,默认参数值为100。
2. `Func2()`函数:
- 基类`Base`中的`Func2()`函数没有使用虚函数关键字,不是虚函数,无法被派生类覆盖。
- 派生类`Derive`中的`Func2()`函数与基类中的函数具有相同的名称和参数类型,但有一个默认参数值不同。
- 在`main()`函数中,使用基类指针`d`指向派生类对象,并调用`d->Func2()`。
- 由于基类中的`Func2()`函数不是虚函数,不构成覆盖,实际上调用的是基类中的`Func2()`函数,默认参数值为100。
因此,执行上述代码的输出结果是:
```
100
100
```
需要注意的是,在派生类中重新定义虚函数时,最好避免改变函数的参数列表,以确保能正确覆盖基类的虚函数。默认参数值的不同并不影响函数的覆盖关系。
#include<iostream> using namespace std; class student; class teacher { public: int a = 1; student p; teacher(int a); ~teacher(); }; teacher::teacher(int a) { cout << "teacher构造函数调用" << endl; this->a = a; // p = new student(100); } teacher::~teacher() { cout << "teacher析构函数调用" << endl; // delete p; } class student { public: //友元函数可以访问类中的公有和私有成员,不可以访问保护成员 friend void func2(student& s); friend class teacher; int a = 10; student(int x); ~student(); void func3(); private: int b = 20; }; student::student(int x) :a(x) { cout << "student 构造函数调用1" << endl; } student::~student() { cout << "student 析构函数调用!" << endl; } void student::func3() { cout << b << endl; } void func1() { student s1(100); cout << s1.a << endl; s1.func3(); func2(s1); cout << s1.a << endl; } //友元函数,全局函数做友元 //在形参中加入const防止传入的参数被改变,不加const则可以改变 void func2( student &s) { //传入普通类对象不可以改变类中的值,只能改变形参类的值 /*s.a = 123; cout<<s.a << endl; cout << s.b << endl;*/ ////传入指针可以改变类的值 /*s->a = 1000; s->b = 123; cout << s->a << endl; cout << s->b << endl;*/ ///传入引用也可以改变类中的值 //s.a = 12345; //s.b = 123456; cout << s.a << endl; cout << s.b << endl; } //类做友元 void func3() { student s1(10); cout << s1.a << endl; teacher t1(100); cout << t1.a << endl; //t1.p->a = 123456; cout << t1.p.a<< endl; } int main() { //成员函数做友元 //func1(); //类做友元 func3(); system("pause"); return 0; }你再看一遍我没有申明吗·1
非常抱歉,我之前的回答还是有误。在你提供的代码中,确实没有在`teacher`类之前声明`student`类的存在。
如果你想在`teacher`类中使用`student`类,你需要在`teacher`类的定义之前声明`student`类的存在,例如:
```cpp
// 声明 student 类的存在
class student;
// 定义 teacher 类
class teacher
{
public:
int a = 1;
student p;
teacher(int a);
~teacher();
};
class student
{
public:
int a = 10;
student(int x);
~student();
void func3();
};
teacher::teacher(int a)
{
cout << "teacher构造函数调用" << endl;
this->a = a;
}
teacher::~teacher()
{
cout << "teacher析构函数调用" << endl;
}
student::student(int x) :a(x)
{
cout << "student 构造函数调用1" << endl;
}
student::~student()
{
cout << "student 析构函数调用!" << endl;
}
void student::func3()
{
cout << b << endl;
}
void func1()
{
student s1(100);
cout << s1.a << endl;
s1.func3();
func2(s1);
cout << s1.a << endl;
}
void func2(student& s)
{
s.a = 12345;
s.b = 123456;
cout << s.a << endl;
cout << s.b << endl;
}
void func3()
{
student s1(10);
cout << s1.a << endl;
teacher t1(100);
cout << t1.a << endl;
//t1.p->a = 123456;
cout << t1.p.a << endl;
}
int main()
{
func3();
system("pause");
return 0;
}
```
在这个代码中,我们在`teacher`类之前声明了`student`类的存在,这样就可以在`teacher`类中使用`student`类了。需要注意的是,在使用`student`类之前,你还需要在代码中包含`student.h`头文件,或者将`student`类的定义放在当前文件中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)