I'm a bit confused as to why your compiler gives you that particular error, but there is an error in your code. You can't put executable statements like i=10;
as 'free-standing' parts of your class definition; generally, any such code must be part of the body of a member function.
However, you can provide a default (initial) value for variables in their declaration. So, you could do this (I've also added an 'arbitrary' initial value for the s
variable, so you can see that the code is working):
#include<iostream>
using std::cout;
class a {
public:
int s = 42, i = 0; // You can put initial (i.e. default) values for the members in the declarations
// i = 10; // Can't have this line here
};
class b :public a {
public:
void print()
{
cout << s;
}
};
int main()
{
b o;
o.print();
}
Please also read this post: Why is "using namespace std;" considered bad practice?
Feel free to ask for further explanation and/or clarification.