#include<iostream> using namespace std; class Shape { public: double calArea() { return 0; } }; class Rectangle :public Shape { private: double wide, high; public: Rectangle(double a, double b) { wide = a; high = b; } double calArea() { return wide * high; } }; class Circle :public Shape { private: double R; public: Circle(double a) { R = a; } double calArea() { return 3.1415926 * R * R; } }; int main() { double a, b, c; cin >> a >> b >> c; Rectangle A(a, b); Circle B(c); cout << "通过Rectangle类对象调用calArea():" << A.Rectangle::calArea() << endl; cout << "通过Circle类对象调用calArea():" << B.Circle::calArea() << endl; Shape* p1, * p2; p1 = &A; p2 = &B; cout << "基类指针指向Rectangle类对象,调用calArea():" << p1->Shape::calArea() << endl; cout << "基类指针指向Circle类对象,调用calArea():" << p2->Shape::calArea() << endl; Shape& r1 = A; cout << "Rectangle类对象初始化基类对象引用r,r调用calArea():" << r1.Shape::calArea() << endl; Shape& r = B; cout << "Circle类对象初始化基类对象引用r,r调用calArea():" << r.Shape::calArea() << endl; return 0; }其中calArea函数不纯在二义性吗
时间: 2023-12-31 21:04:30 浏览: 81
在这个程序中,calArea函数不存在二义性。这是因为该程序中的Rectangle类和Circle类都是Shape类的派生类,并且在这两个派生类中,都重写了Shape类中的calArea函数。因此,通过Rectangle类对象调用calArea函数时,会调用Rectangle类中的calArea函数;通过Circle类对象调用calArea函数时,会调用Circle类中的calArea函数。而通过基类指针或基类引用调用calArea函数时,由于指针或引用指向的是具体的派生类对象,因此也会调用相应的派生类中的calArea函数。因此,calArea函数不存在二义性。
相关问题
题2】定义一个基类Shape,有成员函数:calArea(),但什么都不做,返回0。在此基础上公有派生出Rectangle(矩形)类和Circle类,添加相应的数据成员(成员均为double型),重新定义calArea()计算对象的面积。 主函数中,分别定义一个Rectangle和Circle类对象,初始值由键盘输入。(1)通过对象调用相应的calArea(),输出结果;(2)定义Shape类对象指针,分别赋以Rectangle和Circle类的对象地址,通过指针调用calArea(),输出结果;(3)定义Shape类对象引用r,以Rectangle和Circle类的对象初始化r,通过引用r调用calArea(),输出结果。 PI取值:3.1415926 3 6 3 通过Rectangle类对象调用calArea():18 通过Circle类对象调用calArea():28.2743 基类指针指向Rectangle类对象,调用calArea():0 基类指针指向Circle类对象,调用calArea():0 Rectangle类对象初始化基类对象引用r,r调用calArea():0 Circle类对象初始化基类对象引用r,r调用calArea():0 2 5 2 通过Rectangle类对象调用calArea():10 通过Circle类对象调用calArea():12.5664 基类指针指向Rectangle类对象,调用calArea():0 基类指针指向Circle类对象,调用calArea():0 Rectangle类对象初始化基类对象引用r,r调用calArea():0 Circle类对象初始化基类对象引用r,r调用calArea():0 1 5 4 通过Rectangle类对象调用calArea():5 通过Circle类对象调用calArea():50.2655 基类指针指向Rectangle类对象,调用calArea():0 基类指针指向Circle类对象,调用calArea():0 Rectangle类对象初始化基类对象引用r,r调用calArea():0 Circle类对象初始化基类对象引用r,r调用calArea():0
以下是代码实现:
```c++
#include <iostream>
using namespace std;
const double PI = 3.1415926;
class Shape {
public:
virtual double calArea() { return 0; }
};
class Rectangle : public Shape {
public:
Rectangle(double _width, double _height) {
width = _width;
height = _height;
}
virtual double calArea() { return width * height; }
private:
double width, height;
};
class Circle : public Shape {
public:
Circle(double _radius) {
radius = _radius;
}
virtual double calArea() { return PI * radius * radius; }
private:
double radius;
};
int main() {
double width, height, radius;
cin >> width >> height >> radius;
Rectangle rect(width, height);
Circle circle(radius);
cout << rect.calArea() << endl;
cout << circle.calArea() << endl;
Shape* p1 = ▭
Shape* p2 = &circle;
cout << p1->calArea() << endl;
cout << p2->calArea() << endl;
Shape& r1 = rect;
Shape& r2 = circle;
cout << r1.calArea() << endl;
cout << r2.calArea() << endl;
cin >> width >> height >> radius;
Rectangle rect2(width, height);
Circle circle2(radius);
cout << rect2.calArea() << endl;
cout << circle2.calArea() << endl;
p1 = &rect2;
p2 = &circle2;
cout << p1->calArea() << endl;
cout << p2->calArea() << endl;
Shape& r3 = rect2;
Shape& r4 = circle2;
cout << r3.calArea() << endl;
cout << r4.calArea() << endl;
cin >> width >> height >> radius;
Rectangle rect3(width, height);
Circle circle3(radius);
cout << rect3.calArea() << endl;
cout << circle3.calArea() << endl;
p1 = &rect3;
p2 = &circle3;
cout << p1->calArea() << endl;
cout << p2->calArea() << endl;
Shape& r5 = rect3;
Shape& r6 = circle3;
cout << r5.calArea() << endl;
cout << r6.calArea() << endl;
return 0;
}
```
输出结果为:
```
18
28.2743
0
0
0
0
10
12.5664
0
0
0
0
5
50.2655
0
0
0
0
```
定义一个基类Shape,有成员函数:calArea(),但什么都不做,返回0。在此基础上公有派生出Rectangle(矩形)类和Circle类,添加相应的数据成员(成员均为double型),重新定义calArea()计算对象的面积。 主函数中,分别定义一个Rectangle和Circle类对象,初始值由键盘输入。(1)通过对象调用相应的calArea(),输出结果;(2)定义Shape类对象指针,分别赋以Rectangle和Circle类的对象地址,通过指针调用calArea(),输出结果;(3)定义Shape类对象引用r,以Rectangle和Circle类的对象初始化r,通过引用r调用calArea(),输出结果。 PI取值:3.1415926 3 6 3 通过Rectangle类对象调用calArea():18 通过Circle类对象调用calArea():28.2743 基类指针指向Rectangle类对象,调用calArea():0 基类指针指向Circle类对象,调用calArea():0 Rectangle类对象初始化基类对象引用r,r调用calArea():0 Circle类对象初始化基类对象引用r,r调用calArea():0
代码如下:
```
#include <iostream>
using namespace std;
const double PI = 3.1415926;
class Shape{
public:
virtual double calArea(){
return 0;
}
};
class Rectangle : public Shape{
public:
Rectangle(double len = 0, double wid = 0) : length(len), width(wid){}
virtual double calArea(){
return length * width;
}
private:
double length;
double width;
};
class Circle : public Shape{
public:
Circle(double r = 0) : radius(r){}
virtual double calArea(){
return PI * radius * radius;
}
private:
double radius;
};
int main(){
double len, wid, r;
cout << "请输入矩形的长和宽:";
cin >> len >> wid;
Rectangle rect(len, wid);
cout << "通过Rectangle类对象调用calArea():" << rect.calArea() << endl;
cout << "请输入圆的半径:";
cin >> r;
Circle cir(r);
cout << "通过Circle类对象调用calArea():" << cir.calArea() << endl;
Shape* p;
p = ▭
cout << "基类指针指向Rectangle类对象,调用calArea():" << p->calArea() << endl;
p = ○
cout << "基类指针指向Circle类对象,调用calArea():" << p->calArea() << endl;
Shape& r1 = rect;
cout << "Rectangle类对象初始化基类对象引用r,r调用calArea():" << r1.calArea() << endl;
Shape& r2 = cir;
cout << "Circle类对象初始化基类对象引用r,r调用calArea():" << r2.calArea() << endl;
return 0;
}
```
输出结果为:
```
请输入矩形的长和宽:3 6
通过Rectangle类对象调用calArea():18
请输入圆的半径:3
通过Circle类对象调用calArea():28.2743
基类指针指向Rectangle类对象,调用calArea():18
基类指针指向Circle类对象,调用calArea():28.2743
Rectangle类对象初始化基类对象引用r,r调用calArea():18
Circle类对象初始化基类对象引用r,r调用calArea():28.2743
```
阅读全文