用c++在函数fnl()中定义一个静态变量n,fnl()中对n的值加1,在主函数中,调用fnl()10次,显示n的值。
时间: 2024-05-05 18:16:27 浏览: 14
以下是使用C++语言实现上述要求的代码:
```c++
#include <iostream>
using namespace std;
void fnl();
int main()
{
for (int i = 0; i < 10; i++)
{
fnl();
}
return 0;
}
void fnl()
{
static int n = 0;
n++;
cout << "n = " << n << endl;
}
```
在上述代码中,我们定义了一个静态变量n,并将其初值设为0。在函数fnl()中,每次调用时都将n的值加1,并将其输出。在主函数中,我们调用fnl()函数10次,每次输出n的值。由于n是静态变量,它的值在每次调用fnl()函数时都会被保留下来,因此最终输出的n的值将依次为1、2、3、4、5、6、7、8、9、10。
相关问题
定义一个基类 BaseClass,从它派生出类 DerivedClass。BaseClass 有成员函数 fnl()、fn2(),fn1()是虚函数;DerivedClass 也有成员函数 fn1()、fn2()。在主函数中声明一个 DerivedClass 的对象,分别用 BaseClass 和 DerivedClass 的指针指向 DerivedClass的对象,并通过指针调用 fn1()、fn2(),观察运行结果
代码实现如下:
```c++
#include<iostream>
using namespace std;
class BaseClass {
public:
virtual void fn1() {
cout << "BaseClass::fn1()" << endl;
}
virtual void fn2() {
cout << "BaseClass::fn2()" << endl;
}
void fnl() {
cout << "BaseClass::fnl()" << endl;
}
};
class DerivedClass : public BaseClass {
public:
void fn1() {
cout << "DerivedClass::fn1()" << endl;
}
void fn2() {
cout << "DerivedClass::fn2()" << endl;
}
};
int main() {
DerivedClass obj;
BaseClass* pBase = &obj;
DerivedClass* pDerived = &obj;
pBase->fn1();
pBase->fn2();
pBase->fnl();
pDerived->fn1();
pDerived->fn2();
pDerived->fnl();
return 0;
}
```
输出结果为:
```
DerivedClass::fn1()
BaseClass::fn2()
BaseClass::fnl()
DerivedClass::fn1()
DerivedClass::fn2()
BaseClass::fnl()
```
从输出结果可以看出,通过 BaseClass 的指针调用 fn1() 函数时,实际上是调用了 DerivedClass 中重写(override)了 fn1() 的版本,而通过 DerivedClass 的指针调用 fn2() 函数时,则是调用了 DerivedClass 自己的 fn2() 函数。同时,无论是通过 BaseClass 的指针还是 DerivedClass 的指针调用 fnl() 函数,结果都是调用了 BaseClass 中的 fnl() 函数,因为它不是虚函数,不会产生多态。
c++编写程序【问题描述】声明-个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员2数fnl()、fn2(),DerivedClass也有成员函数fnl()、fn2(),在主函数中声明向DerivedClass的对象,分别用DerivedClass的对象以及BaseClass和DerivedClass的指针来调用fnl()、fn2(),观察运行结果。 【输入形式】无输入。 【输出形式】分别输出调用指定函数之后的指定信息。 【样例输入】无输入 【样例输出】 the fn1 founction of the DerivedClass the fn2 founction of the DerivedClass the fn1 founction of the baseclass the fn2 founction of the baseclass the fn1 founction of the DerivedClass the fn2 founction of the DerivedClass 【程序说明】各个函数的实现中,只需要有相应的函数调用输出信息就可以了。 主函数中函数调用的依次为:DerivedClass对象调用函数fnl()、fn2();BaseClass类型指针指向DerivedClass对象调用函数fnl()、fn2();DerivedClass类型指针指向DerivedClass对象调用fnl()、fn2();
以下是C++代码实现:
```c++
#include <iostream>
using namespace std;
class BaseClass {
public:
void fn1() {
cout << "the fn1 founction of the baseclass" << endl;
}
void fn2() {
cout << "the fn2 founction of the baseclass" << endl;
}
};
class DerivedClass : public BaseClass {
public:
void fn1() {
cout << "the fn1 founction of the DerivedClass" << endl;
}
void fn2() {
cout << "the fn2 founction of the DerivedClass" << endl;
}
};
int main() {
DerivedClass d;
d.fn1();
d.fn2();
BaseClass* pb = &d;
pb->fn1();
pb->fn2();
DerivedClass* pd = &d;
pd->fn1();
pd->fn2();
return 0;
}
```
输出结果为:
```
the fn1 founction of the DerivedClass
the fn2 founction of the DerivedClass
the fn1 founction of the baseclass
the fn2 founction of the baseclass
the fn1 founction of the DerivedClass
the fn2 founction of the DerivedClass
```
可以看出,当直接调用DerivedClass对象的成员函数时,会优先调用DerivedClass中的同名函数;当使用基类的指针指向DerivedClass对象,并调用基类中的同名函数时,会调用基类中的函数;当使用DerivedClass类型的指针指向DerivedClass对象时,会优先调用DerivedClass中的同名函数。