#include <iostream>
using namespace std;
class Base {
public:
virtual Base& fun() { cout << "base fun" << endl; return *this; }
virtual Base& foo() { cout << "base foo" << endl; return *this; }
};
class Derived: public Base {
public:
Base& fun() { cout << "derived fun" << endl; return *this; }
Base& foo() { cout << "derived foo" << endl; return *this; }
};
Base& foo();
Base& fun();
// 在此处补充你的代码
int main() {
foo().fun().foo();
fun().foo().fun();
return 0;
}
输入:
无
输出:
derived foo
derived fun
derived foo
base fun
base foo
base fun
求解答补充代码,谢谢
// Q758999.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base {
public:
virtual Base& fun() { cout << "base fun" << endl; return *this; }
virtual Base& foo() { cout << "base foo" << endl; return *this; }
};
class Derived: public Base {
public:
Base& fun() { cout << "derived fun" << endl; return *this; }
Base& foo() { cout << "derived foo" << endl; return *this; }
};
Base& foo();
Base& fun();
// 在此处补充你的代码
Base& foo()
{
Derived * d = new Derived();
d->foo();
return *d;
}
Base& fun()
{
Base * b = new Base();
b->fun();
return *b;
}
int main() {
foo().fun().foo();
fun().foo().fun();
return 0;
}