#include <iostream> using namespace std; class Parent { public: virtual void f() { cout << "invoke f from Parent" << endl; } }; class Child : public Parent { public: void f() { cout << "invoke f from Child" << endl; } }; void p(Parent& a) { a.f(); } int main() { Parent a; p(a); Child b; b.f(); p(b); return 0; }输出结果是什么
时间: 2023-07-16 07:17:01 浏览: 81
输出结果为:
```
invoke f from Parent
invoke f from Child
invoke f from Child
```
首先定义了一个`Parent`类和一个`Child`类,`Child`类继承自`Parent`类并重写了`f()`方法。
然后定义了一个函数`p()`,参数为`Parent`的引用,函数中调用了`a.f()`方法。
在`main()`函数中,先创建了一个`Parent`类对象`a`,调用`p(a)`方法,输出`invoke f from Parent`。
然后创建了一个`Child`类对象`b`,调用`b.f()`方法,输出`invoke f from Child`。
最后再次调用`p(b)`方法,由于参数是`Parent`类型的引用,而`b`是`Child`类型的对象,因此会调用`Parent`类中的`f()`方法,输出`invoke f from Child`。这就是多态性的表现。
相关问题
1.#include<iostream> using namespace std; class Parent [] public: virtual void f() [ cout<<"invoke f from Parent"<<endl; 了 }; class Child:public Parent [ public:void f() { cout <<"invoke f from Child"<<endl; 了 }; void p(Parent &a) [ a. f(); } int main() [[] Parent a; p(a); Child b; b. f( ); p(b); return 0; 了
这段代码存在语法错误,需要进行修正,具体修改如下:
```
#include <iostream>
using namespace std;
class Parent {
public:
virtual void f() {
cout << "invoke f from Parent" << endl;
}
};
class Child : public Parent {
public:
void f() {
cout << "invoke f from Child" << endl;
}
};
void p(Parent& a) {
a.f();
}
int main() {
Parent a;
p(a);
Child b;
b.f();
p(b);
return 0;
}
```
这段代码定义了一个父类 `Parent` 和一个继承自父类的子类 `Child`,并且在 `Parent` 类中定义了一个虚函数 `f()`。在 `p()` 函数中,以 `Parent` 类型的引用作为参数,调用了 `a.f()`,在 `main()` 函数中,实例化了 `Parent` 对象 `a` 和 `Child` 对象 `b`,并分别调用了它们的 `f()` 函数,观察输出结果可以看到虚函数的多态性,即 `p()` 函数调用时,根据实参的实际类型调用了不同的函数。
请完成Complex类的定义,使得下面的主函数能正常运行。 int main( ) { Complex c1(1,2); cout<<"c1="<<c1; //operator<<(cout,c1); Complex c2(2); // default argument is i=0 cout<<"c2="<<c2<<endl; Complex c3(c1); //invoke the copy constructor cout<<"c3="<<c3; c1=c1+c2+c3; // c1=(c1.operator+(c2)).operator+(c3); cout<<"c1=c1+c2+c3="<<c1; c2=-c3; // c2=c3.operator-(); cout<<"c2=-c3="<<c2; c3=c2-c1; // c3=c2.operator-(c1); cout<<"c3=c2-c1="<< c3; Complex r2=2+c2; // calls operator+ (2, c2) cout<<"r2=2+c2="<<r2; Complex c4; cin>>c4; cout<<"c4="<<c4; cout<<"the module of c4 is: "<<c4.GetMagnitude()<<endl;//计算复数的模 return 0; }
```
#include <iostream>
#include <cmath>
using namespace std;
class Complex {
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex(const Complex& c) : real(c.real), imag(c.imag) {}
double GetMagnitude() const {
return sqrt(real * real + imag * imag);
}
Complex operator+(const Complex& c) const {
return Complex(real + c.real, imag + c.imag);
}
Complex operator-(const Complex& c) const {
return Complex(real - c.real, imag - c.imag);
}
Complex operator-() const {
return Complex(-real, -imag);
}
Complex operator*(const Complex& c) const {
return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
Complex operator/(const Complex& c) const {
double r = c.real * c.real + c.imag * c.imag;
return Complex((real * c.real + imag * c.imag) / r, (imag * c.real - real * c.imag) / r);
}
Complex operator+=(const Complex& c) {
real += c.real;
imag += c.imag;
return *this;
}
Complex operator-=(const Complex& c) {
real -= c.real;
imag -= c.imag;
return *this;
}
Complex operator*=(const Complex& c) {
*this = *this * c;
return *this;
}
Complex operator/=(const Complex& c) {
*this = *this / c;
return *this;
}
Complex operator+(double d) const {
return Complex(real + d, imag);
}
Complex operator-(double d) const {
return Complex(real - d, imag);
}
Complex operator*(double d) const {
return Complex(real * d, imag * d);
}
Complex operator/(double d) const {
return Complex(real / d, imag / d);
}
Complex operator+=(double d) {
real += d;
return *this;
}
Complex operator-=(double d) {
real -= d;
return *this;
}
Complex operator*=(double d) {
real *= d;
imag *= d;
return *this;
}
Complex operator/=(double d) {
real /= d;
imag /= d;
return *this;
}
Complex& operator=(const Complex& c) {
real = c.real;
imag = c.imag;
return *this;
}
bool operator==(const Complex& c) const {
return real == c.real && imag == c.imag;
}
bool operator!=(const Complex& c) const {
return !(*this == c);
}
friend ostream& operator<<(ostream& out, const Complex& c) {
out << c.real << "+" << c.imag << "i";
return out;
}
friend istream& operator>>(istream& in, Complex& c) {
in >> c.real >> c.imag;
return in;
}
private:
double real, imag;
};
int main() {
Complex c1(1, 2);
cout << "c1=" << c1 << endl;
Complex c2(2);
cout << "c2=" << c2 << endl;
Complex c3(c1);
cout << "c3=" << c3 << endl;
c1 = c1 + c2 + c3;
cout << "c1=c1+c2+c3=" << c1 << endl;
c2 = -c3;
cout << "c2=-c3=" << c2 << endl;
c3 = c2 - c1;
cout << "c3=c2-c1=" << c3 << endl;
Complex r2 = 2 + c2;
cout << "r2=2+c2=" << r2 << endl;
Complex c4;
cin >> c4;
cout << "c4=" << c4 << endl;
cout << "the module of c4 is: " << c4.GetMagnitude() << endl;
return 0;
}
```
阅读全文