【C++代码调试】【继承习题】新编C++习题册p210例7.26调试

新编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
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值