1

I was working with inheritance in C++ but I found the following error: 's' does not name a type.

Can someone please explain what is the problem?

#include<iostream>
using namespace std;
class a
{
public:
    int s,i;
    i=10;
};
class b:public a
{
public:
void print()
{
    cout<<s;
}
};
int main()
{
    b o;
    o.print();
}
Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25
  • please help i need to finish this soon – naman singh Apr 10 '20 at 14:36
  • 1
    You mean `'i' does name a type`. You cannot assign variable in the class body like that; You should do it in the constructor (or during the declaration like this `int i = 10;`, but don't do it). [Some examples](https://en.cppreference.com/w/cpp/language/initializer_list); And don't post "please help" - it never makes people willing to help you. – pptaszni Apr 10 '20 at 14:36
  • but why is that – naman singh Apr 10 '20 at 14:37
  • Does this answer your question? [What's the reason for initializing fields inside class?](https://stackoverflow.com/questions/35582045/whats-the-reason-for-initializing-fields-inside-class) – pptaszni Apr 10 '20 at 14:40

2 Answers2

2

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • i want to ask one more thing when i print value of s in base class i dont get the value assinged to it – naman singh Apr 10 '20 at 14:42
  • I'm not sure what you mean: `s` in the base class is the same as `s` in the derived class, and that shows the value `42`, does it not? – Adrian Mole Apr 10 '20 at 14:44
  • no i mean when i initialize it through object creation and not like what i asked(that was a diffrent problem) now i ask if i assign a value to s through object of class a why cant i directly acess that value in class b – naman singh Apr 10 '20 at 14:47
  • Again, I'm not *entirely* sure what you mean. Although you can (sometimes) access a derived class 'as though it were' its base class, trying to do the other way round will generally be problematical. Maybe post as a new question? – Adrian Mole Apr 10 '20 at 14:50
0

For in your snipe of code. You are trying to initialize class variable within class whereas you can initialize the variable in constructor or setter function like following

class a
{
public:
    int s, i;
    a()
    {
        i = 10;
        s = 20;
    };
    void setS(int a)
    {
        s = a;
    }
};
class b :public a
{
public:
    void print()
    {
        cout << s;
    }
};
int main()
{
    b o;
    o.print();
}
Ahsan
  • 11
  • 2