求教:下面的代码的结果和我预计的结果不符,问题出在哪里?谢谢
#include<iostream>
using namespace std;
class First {
int val1;
public:
int SetVal1(int v) {
int val1=v;
cout<<"传入修改后的val1值是"<<val1<<endl;
show_First();
return val1;
}
void show_First(void) {
cout<<"val1="<<val1<<endl;
}
};
//派生类Second
class Second:private First {
int val2;
public:
void SetVal2(int v1,int v2) {
SetVal1(v1);
val2=v2;
}
void show_Second(void) {
// cout<<"val1="<<val1<<endl; 不能访问First私有成员
show_First();
cout<<"val2="<<val2<<endl;
}
};
void main() {
Second s1;
s1.SetVal2(20,3);
s1.show_Second();
system("pause");
}
[解决办法]
也许楼主是这个意思:
#include <iostream>
using namespace std;
class First {
int val1;
public:
int SetVal1(int v) {
val1=v;//int val1=v;
cout<<"传入修改后的val1值是"<<val1<<endl;
show_First();
return val1;
}
void show_First(void) {
cout<<"val1="<<val1<<endl;
}
};
//派生类Second
class Second:private First {
int val2;
public:
void SetVal2(int v1,int v2) {
SetVal1(v1);
val2=v2;
}
void show_Second(void) {
// cout<<"val1="<<val1<<endl; 不能访问First私有成员
show_First();
cout<<"val2="<<val2<<endl;
}
};
void main() {
Second s1;
s1.SetVal2(20,3);
s1.show_Second();
system("pause");
}
int val1=v;
val1=v;