写出程序运行结果:_________#include class shape {public: virtual int area() {return 0; }};class rectangle:public shape {public: int a, b; void setLength (int x, int y) {a=x;b=y; } int area() {return a*b; }}; void main() { rectangle r; r.setLength(10,5); shape s1; shape &s2=r; cout <
查看答案
写出程序运行结果:_________#include class A{ int i; public: virtual void fun()=0; A(int a) {i=a; }};class B:public A { int j; public: void fun() {cout<<"B::fun()"<fun();}
写出程序运行结果:_________#include using namespace std;class Base {public: virtual ~Base();};Base::~Base() { cout<< "~Base()" << endl;}class Derived: public Base {public: Derived(); ~Derived();private: int *p;};Derived::Derived() { p = new int(0);}Derived::~Derived(){ cout << "~Derived()" << endl; delete p;}void fun(Base* b) { delete b;}int main() { Base *b = new Derived(); fun(b); return 0;}
写出程序运行结果:_________#include using namespace std;class Base1{public: virtual void display() const = 0;};class Base2: public Base1{public: void display() const;};void Base2::display() const { cout << "Base2::display()" << endl;}class Derived: public Base2{ public: void display() const;};void Derived::display() const { cout << "Derived::display()" << endl;}void fun(Base1 *ptr){ ptr->display(); }int main(){ Base2 base2; Derived derived; fun(&base2); fun(&derived); return 0;}
写出程序运行结果:_________#include //using namespace std;class Complex{ public: Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { } friend Complex operator + (const Complex &c1, const Complex &c2); friend Complex operator - (const Complex &c1, const Complex &c2); friend ostream & operator << (ostream &out, const Complex &c);private: double real; double imag;}; Complex operator + (const Complex &c1, const Complex &c2){ return Complex(c1.real + c2.real, c1.imag + c2.imag);}Complex operator - (const Complex &c1, const Complex &c2) { return Complex(c1.real - c2.real, c1.imag - c2.imag);}ostream & operator << (ostream &out, const Complex &c) { out << "(" << c.real << "," << c.imag <<")"; return out;}int main() { Complex c1(5, 4), c2(2, 10), c3; cout << "c1=" << c1 << endl; cout << "c2=" << c2 << endl; c3 = c1 - c2; cout << "c3=c1-c2=" << c3 << endl; c3 = c1 + c2; cout << "c3=c1+c2=" << c3 << endl; return 0;}