下面程序输出结果为: Display1() in BaseClass is called! Display1() in DerivedClass is called! Display2() in BaseClass is called! Display2() in BaseClass is called! 请将程序填写完整 #include <iostream> using namespace std; class BaseClass { public: (1) {cout<<"Display1() in BaseClass is called!"<<endl;} (2) {cout<<"Display2() in BaseClass is called!"<<endl;} }; class DerivedClass:public BaseClass { public: void Display1(){cout<<"Display1() in DerivedClass is called!"<<endl;} void Display2(){cout<<"Display2() in DerivedClass is called!"<<endl;} }; void fun1( (3) ) { p->Display1(); } void fun2(BaseClass &rb) { rb.Display2(); } int main() { BaseClass b; DerivedClass d; fun1(&b); fun1(&d); fun2(b); fun2(d); return 0; }
时间: 2024-04-01 08:33:38 浏览: 16
#include <iostream>
using namespace std;
class BaseClass {
public:
void Display1() {cout<<"Display1() in BaseClass is called!"<<endl;}
void Display2() {cout<<"Display2() in BaseClass is called!"<<endl;}
};
class DerivedClass:public BaseClass {
public:
void Display1() {cout<<"Display1() in DerivedClass is called!"<<endl;}
void Display2() {cout<<"Display2() in DerivedClass is called!"<<endl;}
};
void fun1(BaseClass *p) {
p->Display1();
}
void fun2(BaseClass &rb) {
rb.Display2();
}
int main() {
BaseClass b;
DerivedClass d;
fun1(&b);
fun1(&d);
fun2(b);
fun2(d);
return 0;
}
相关问题
下面程序中Base是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序 的输出为: #include <iostream> class Base {public: _______________; }; class Der1:public Base {public: void display(){cout<<"Der1 called!"<<endl;} }; class Der2:public Base {public: void display(){cout<<"Der2 called!"<<endl;} }; void fun(______________) { p->display();} void main() { Der1 b1; Der2 b2; Base * p=&b1; fun(p); p=&b2; fun(p); }
下面程序中Base是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序 的输出为:
```
#include <iostream>
class Base {
public:
virtual void display() = 0;
};
class Der1:public Base {
public:
void display(){std::cout<<"Der1 called!"<<std::endl;}
};
class Der2:public Base {
public:
void display(){std::cout<<"Der2 called!"<<std::endl;}
};
void fun(Base *p) {
p->display();
}
void main() {
Der1 b1;
Der2 b2;
Base * p=&b1;
fun(p);
p=&b2;
fun(p);
}
```
程序输出:
```
Der1 called!
Der2 called!
```
在 `Base` 类中,我们需要定义一个纯虚函数 `display()`,使得 `Base` 成为一个抽象类。在 `fun()` 函数中,需要使用 `Base` 类型的指针作为参数,这样才能保证在调用 `p->display()` 时,能够调用到派生类中实现的 `display()` 函数。因此,在 `fun()` 函数的参数列表中,我们需要使用 `Base *` 类型的指针。
在 `main()` 函数中,我们定义了两个派生类对象 `b1` 和 `b2`,并分别将它们的地址赋值给 `Base *` 类型的指针 `p`。然后分别调用了 `fun(p)` 函数,程序输出了 `Der1 called!` 和 `Der2 called!`。
垂直方向设置display:flex;结果flex:1不生效
垂直方向设置`display:flex;`后,`flex:1`不生效的原因可能是因为缺少了`flex-direction: column;`属性。`flex-direction: column;`用于指定弹性容器内的元素按照垂直方向排列。
下面是一个示例代码:
```html
<style>
.container {
display: flex;
flex-direction: column;
height: 200px;
}
.item {
flex: 1;
background-color: lightblue;
}
</style>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
```
在上述代码中,`.container`是一个弹性容器,设置了`display: flex;`和`flex-direction: column;`。`.item`是弹性容器内的子元素,设置了`flex: 1;`,表示在垂直方向上均分剩余空间。