#include<iostream> #include<string> using namespace std; class A { protected: string m; double p; public: A(const string& a = "", double b = 0) { m = a; p = b; } string getM() { return m; } double getP() { return p; } virtual void Show() { cout << m << "," << p << endl; } }; class B :public A{ int s; public: B(const string& a, double b, int c = 3) { s = c; } void Show() { cout << m << "," << p << "," << s<<endl; } friend int operator+(B&, B&); }; int operator+(B& b1, B& b2) { return b1.s + b2.s; } int main() { B s1("hhh",300), s2("yyy", 2400, 2); s1.Show(); A& f = s1; f.Show(); cout << f.getM() << "," << f.getP() << endl; cout << s2.getM() << "," << s2.getP() << endl; cout << s1 + s1 << endl; }s1.Show();运行结果是多少?这种语法现象叫什么?
时间: 2024-03-28 21:39:58 浏览: 42
运行结果是 "hhh,0,0",因为在 B 类的构造函数中,虽然继承了 A 类的构造函数,但是并没有对 A 类的数据成员 m 和 p 进行初始化,所以默认值为 "" 和 0。在 s1 对象的 Show() 函数中,输出了 m、p 和 s,因为 s1 对象的 s 值为默认值 3,所以输出为 "hhh,0,3"。
这种语法现象叫做隐藏(hidden),即在派生类中定义了一个和基类同名的成员函数或变量,那么基类的同名成员就被隐藏了,派生类对象调用同名函数或变量时只能访问到自己定义的那个。在本例中,B 类的 Show() 函数隐藏了 A 类的同名函数,所以在调用 s1 对象的 Show() 函数时,只能访问到 B 类中定义的 m、p 和 s。
相关问题
#include<iostream> #include<string> using namespace std; class A { protected: string m; double p; public: A(const string& a = "", double b = 0) { m = a; p = b; } string getM() { return m; } double getP() { return p; } virtual void Show() { cout << m << "," << p << endl; } }; class B :public A{ int s; public: B(const string& a, double b, int c = 3) { s = c; } void Show() { cout << m << "," << p << "," << s<<endl; } friend int operator+(B&, B&); }; int operator+(B& b1, B& b2) { return b1.s + b2.s; } int main() { B s1("hhh",300), s2("yyy", 2400, 2); s1.Show(); A& f = s1; f.Show(); cout << f.getM() << "," << f.getP() << endl; cout << s2.getM() << "," << s2.getP() << endl; cout << s1 + s1 << endl; }
这段代码定义了两个类 A 和 B,其中 B 继承自 A,A 中有两个数据成员 m 和 p,表示名称和价格,有一个虚函数 Show(),用于输出对象的名称和价格;B 中有一个数据成员 s,表示数量,也有一个 Show() 函数,输出对象的名称、价格和数量。B 中还重载了一个 + 运算符,用于计算两个 B 对象的数量之和。
在 main 函数中,定义了两个 B 对象 s1 和 s2,分别传入不同的参数进行初始化。然后分别调用它们的 Show() 函数进行输出。接着定义了一个 A 类型的引用 f,指向 s1 对象,并调用 f 的 Show() 函数进行输出。然后分别输出 s2 对象的名称和价格。最后调用了 s1 + s1,输出两个对象数量之和。
#include <iostream> #include <string> using namespace std; class Point { protected: string type; double x, y; public: Point() : type("Point"), x(0), y(0) {} Point(double x, double y) : type("Point"), x(x), y(y) {} virtual ~Point() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Point& p) { os << "Type: " << p.type << endl; os << "Coordinates: (" << p.x << ", " << p.y << ")" << endl; return os; } virtual void PrintName() { cout << "Type: " << type << endl; } }; class Circle : public Point { protected: double r; public: Circle() : Point(), r(0) { type = "Circle"; } Circle(double x, double y, double r) : Point(x, y), r(r) { type = "Circle"; } virtual ~Circle() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Circle& c) { os << static_cast<const Point&>(c); os << "Radius: " << c.r << endl; return os; } virtual void PrintName() { cout << "Type: " << type << endl; } }; class Cylinder : public Circle { protected: double h; public: Cylinder() : Circle(), h(0) { type = "Cylinder"; } Cylinder(double x, double y, double r, double h) : Circle(x, y, r), h(h) { type = "Cylinder"; } virtual ~Cylinder() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Cylinder& cy) { os << static_cast<const Circle&>(cy); os << "Height: " << cy.h << endl; return os; } virtual void PrintName() { cout << "Type: " << type << endl; } }; int main() { Point p1(1, 2); cout << p1 << endl; Circle c1(1, 2, 3); cout << c1 << endl; Cylinder cy1(1, 2, 3, 4); cout << cy1 << endl; Point* p; p = &p1; p->PrintName(); p = &c1; p->PrintName(); p = &cy1; p->PrintName(); return 0; } 写出该程序的大致流程
1. 引入iostream和string头文件。
2. 声明Point类,包含类型(type)和坐标(x和y)属性,有默认构造函数和带参数构造函数,定义了一个友元函数用于输出对象信息,定义了一个虚函数PrintName用于输出对象类型。
3. 声明Circle类,继承自Point类,增加了半径(r)属性,有默认构造函数和带参数构造函数,定义了一个友元函数用于输出对象信息,重写了虚函数PrintName用于输出对象类型。
4. 声明Cylinder类,继承自Circle类,增加了高度(h)属性,有默认构造函数和带参数构造函数,定义了一个友元函数用于输出对象信息,重写了虚函数PrintName用于输出对象类型。
5. 在main函数中,分别创建一个Point对象、一个Circle对象和一个Cylinder对象,输出它们的信息。
6. 声明一个指向Point对象的指针p,在分别将p指向Point对象、Circle对象和Cylinder对象后,分别调用p的PrintName函数,输出对象类型。
7. 程序结束,析构Point、Circle和Cylinder对象。
阅读全文
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)