很少编类,今晚试了试手,从下面的代码得到了几点经验:
1、成员函数如果不是构造函数则需要指定返回值(或者void)
2、类的成员变量在继承的时候,并不是单独的覆盖,而是有点像局部变量,和父类的同名变量可共存,从代码编译结果可以知道,两个变量的存储地址是不一样的。
#include<iostream.h>
class Base
{
public:
int a;
Base()
{
a=0;
cout<<"I`m base Begin"<<endl;
}
int Print()
{
cout<<"The base a is "<<a<<endl;
cout<<&a<<endl;
return 1;
}
~Base()
{
cout<<"I`m base End"<<endl;
}
};
class Simple: public Base
{
public:
int a;
Simple()
{
a=1;
cout<<"I`m Simple Begin"<<endl;
}
~Simple()
{
cout<<"I`m Simple End"<<endl;
}
void Printnew()
{
cout<<"The simple a is "<<a<<endl;
cout<<&a<<endl;
}
};
main()
{
Simple b;
b.Print();
b.Printnew();
}
![](https://img-my.csdn.net/uploads/201208/06/1344264601_2766.png)