A *p=new B;p->A::show();p->show();结果是
时间: 2024-02-19 16:01:16 浏览: 48
在这段代码中,我们首先创建了一个指向 B 类对象的 A 类指针 p,即 p = new B。由于类 B 是公开继承自类 A 的,因此 B 类对象可以被视为 A 类对象。
接着,我们通过 p 调用了 A 类中的虚函数 show(),即 p->A::show()。由于我们使用了作用域解析符号指定了调用的是 A 类中的 show() 函数,因此最终输出结果是 "A"。
最后,我们通过 p 调用了虚函数 show(),即 p->show()。由于 p 指向的是 B 类对象,B 类重写了虚函数 show(),因此最终输出结果是 "B"。
因此,最终的输出结果是 "A" 和 "B"。
相关问题
#include<iostream> #include<algorithm> #include<string> #include<cmath> using namespace std; struct node { char n = 0; node* next = nullptr; }; node* shuru(string a) { node* head = new node{ 0,NULL}; node* p; int m; m= a.length(); p = head; for(int i=m-1;i>=0;i--) { node* q = new node; p->next = q; q->n = a[i]; p = q; q->next = nullptr; } return head; } void show(node* m) { m = m->next;//head是默认值,不show while (m!=nullptr) { cout << m->n; m = m->next; } } node* add(node* a, node* b) { node* add, * head = new node{ 0,NULL }, * tail; node* p, * q; p = a; q = b; tail = head; if ((a->n == '-' && b->n != '-') || (b->n == '-' && a->n != '-')) { } else if (a->n != '-' && b->n != '-') { int carry = 0; while (1) { int val = 0; if (p) { val += p->n - '0'; p = p->next; } if (q) { val += q->n - '0'; q = q->next; } /*if (p && q == nullptr) { val += p->n - '0'; p = p->next; } if (q && p == nullptr) { val += q->n - '0'; q = q->next; }*/ val = val + carry; if (val == 0) break; add = new node{ val % 10+'0' ,NULL}; carry = val / 10; tail->next = add; tail = add; } return head; } else { add = new node{ '-',NULL }; tail->next = add; } } int main() { int a; cin >> a; for (int i = 0; i <a; i++) { string m, n; cin >> m >> n; show(add(shuru(m), shuru(n))); } }为什么输出很奇怪
这段代码的输出很奇怪是因为在输出结果时,没有换行符。可以在 `show` 函数中添加一个换行符,如下所示:
```c++
void show(node* m) {
m = m->next;
while (m != nullptr) {
cout << m->n;
m = m->next;
}
cout << endl; // 添加换行符
}
```
这样就可以在输出结果后换行,使输出结果更加清晰易读。
如何将以下代码转成pyside6的 #include <QtGui> class GripLabel: public QLabel { Q_OBJECT public: GripLabel(QString const& title, QWidget* parent = 0) : QLabel(title, parent), resizing(false), gripSize(10, 10) { // Prevent the widget from disappearing altogether // Bare minimum would be gripSize setMinimumSize(100, 30); } QSize sizeHint() const { return minimumSize(); } protected: bool mouseInGrip(QPoint mousePos) { // "handle" is in the lower right hand corner return ((mousePos.x() > (width() - gripSize.width())) && (mousePos.y() > (height() - gripSize.height()))); } void mousePressEvent(QMouseEvent *e) { // Check if we hit the grip handle if (mouseInGrip(e->pos())) { oldPos = e->pos(); resizing = true; } else { resizing = false; } } void mouseMoveEvent(QMouseEvent *e) { if (resizing) { // adapt the widget size based on mouse movement QPoint delta = e->pos() - oldPos; oldPos = e->pos(); setMinimumSize(width()+delta.x(), height()+delta.y()); updateGeometry(); } } void paintEvent(QPaintEvent *e) { QLabel::paintEvent(e); QPainter p(this); p.setPen(Qt::red); p.drawRect(width()-gripSize.width(), height()-gripSize.height(), gripSize.width(), gripSize.height()); } private: bool resizing; QSize gripSize; QPoint oldPos; }; sample 主要: #include "griplabel.h" int main(int argc, char **argv) { QApplication app(argc, argv); QWidget *w = new QWidget; QPushButton *b = new QPushButton("button"); GripLabel *l = new GripLabel("Hello"); QHBoxLayout *y = new QHBoxLayout; y->addWidget(b); y->addWidget(l); y->setSizeConstraint(QLayout::SetFixedSize); w->setLayout(y); w->show(); return app.exec(); }
以下是将C++代码转换为使用Pyside6的Python代码:
```python
from PySide6.QtCore import QPoint, QSize
from PySide6.QtGui import QPainter
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QLabel
class GripLabel(QLabel):
def __init__(self, title: str, parent=None):
super().__init__(title, parent)
self.resizing = False
self.gripSize = QSize(10, 10)
self.setMinimumSize(QSize(100, 30))
def sizeHint(self):
return self.minimumSize()
def mouseInGrip(self, mousePos: QPoint):
return mousePos.x() > (self.width() - self.gripSize.width()) and mousePos.y() > (self.height() - self.gripSize.height())
def mousePressEvent(self, e):
if self.mouseInGrip(e.pos()):
self.oldPos = e.pos()
self.resizing = True
else:
self.resizing = False
def mouseMoveEvent(self, e):
if self.resizing:
delta = e.pos() - self.oldPos
self.oldPos = e.pos()
self.setMinimumSize(self.width() + delta.x(), self.height() + delta.y())
self.updateGeometry()
def paintEvent(self, e):
super().paintEvent(e)
p = QPainter(self)
p.setPen(Qt.red)
p.drawRect(self.width() - self.gripSize.width(), self.height() - self.gripSize.height(), self.gripSize.width(), self.gripSize.height())
if __name__ == '__main__':
app = QApplication([])
w = QWidget()
b = QPushButton("button")
l = GripLabel("Hello")
y = QHBoxLayout()
y.addWidget(b)
y.addWidget(l)
y.setSizeConstraint(QLayout.SetFixedSize)
w.setLayout(y)
w.show()
app.exec()
```
需要注意的是,由于Pyside6和PyQt5在某些方面具有不同的语法和实现,因此在转换代码时可能需要进行一些调整。在本例中,我对`QLayout`类的引用进行了一些调整,并将`QPoint`和`QSize`类的头文件引用替换为`PySide6.QtCore`中的模块。
阅读全文