新编C++习题册p210例7.26调试
#include "bits/stdc++.h"
#include<iostream>
using namespace std;
class A{
public:
A(int i,int j){
cout<<"执行A的双参构造函数"<<endl;
a=i;b=j;
cout<<"a是"<<a<<",b是"<<b<<endl<<endl;
}
void move(int x,int y){
cout<<"执行move"<<endl;
cout<<"x是"<<x<<",y是"<<y<<endl; //x是3,y是5
cout<<"a为"<<a<<",b为"<<b<<endl; //a为3,b为4
a+=x;b+=y;
cout<<endl;
}
void show(){
cout<<"执行show"<<endl;
cout<<"a="<<a<<","<<"b="<<b<<endl<<endl; //a为6,b为9
}
private:
int a,b;
};
class B:public A{
public:
B(int i,int j,int k,int l):A(i,j),x(k),y(l){}
void show(){
cout<<"x="<<x<<","<<"y="<<y<<endl<<endl;
}
void fun(){
move(3,5);
}
void f1(){
A::show();
}
private:
int x,y;
};
int main(){
// A e(1,2);
// e.show(); //输出a=1,b=2
B d(3,4,5,6); //i=3,j=4,x=5,y=6
d.fun(); //执行move(3,5),a=3+5=8,b=4+6=10
d.A::show();//输出a=6,b=9【核心】搞懂原因?!
// d.B::show();//输出x=5,y=6
// d.f1();//输出a=6,b=9
return 0;
}
//输出a=6,b=9
//x=5,y=6
//a=6,b=9