0

Well, just new in classes and trying to create a simple program, that has 2 classes. Class A and Class B. Well I'm trying to initialize my variable in constructor in class A, and then make some action and pass it to class B, where I'm also can make some action. So class A is a base class. However, when I compile the program I got two mistake -

error: ‘i’ was not declared in this scope

For class A and class B. So I have two question 1) why constructor doesn't declare variables (according to the books constructor called first)? 2) what ways I can transfer the variable from class A to use in class B?

#include <iostream>
#include <cstdio>
using namespace std;
class A {
    public:
        A(){
            int i =1;           
        }
        ~A(){}
        int foo () {
            int p = i+1;
            i++;
            return p;
        }

};

class B : public A {
    public:
        int showme() {
            return i;
        }
};

int main() {
    A j;
    B k;
    cout<< k.showme()<<endl;
    cout<< j.foo()<<endl;
return 0;
}
2
  • So what then I must to declare in constructor? I thought constructor is to declare variables? Commented Sep 23, 2016 at 3:12
  • 1
    I might be wrong, but it sounds like this also might be helpful to point out. In this code, you declare a variable of class A and a separate variable of class B. When you add int i; as a member variable of A, this means that there will be an integer j.i and a different integer k.i. I wouldn't call this quite transference of the member A::i. Commented Sep 23, 2016 at 3:16

1 Answer 1

2

First off, get a good book:

The Definitive C++ Book Guide and List

and familiarize yourself with the basics. Your question indicates there is a complete mess right now in your understanding of C++.

Now on to the actual Q's.

1) C-tor does not declare a class member variable, it can only declare a local variable since a c-tor is also a function. You need to declare the member variable explicitly like so:

class A {
    int i;
public:
    A(int _i): i(_i) {  }
};

2) The term "transfer" is incorrect per se. In this case, the two classes are in an inheritance hierarchy, so the variable i is inherited by class B and if you declare it as protected, then it will be accessible in the way you do it. The function B::showme() is defined correctly. Fix the first part and the example is going to work.

3) Do not confuse classes and objects. When there is an object of class A declared, it is in no way related to another object of class A or B. j and k share their own private instances of int i (provided that you fix (a)) and if you want to have k's instance of i to be equal to that of j, you can e.g. implement a copy constructor or a pair of getter/setter functions.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I'm still a little confused with constructor and A(int _i): i(_i) { } ; But it worked , and I'll find a better book to understand it better. Thanks.
@alex_mike It's a shortcut to saying A(int _i) { i =_i; }.
@iksemyonov Almost a shortcut. For objects that are "expensive", initialization lists are the way to go, as otherwise you default-construct plus assign. Also, initialization lists are mandatory for reference class members.
@vsoftco great, thank you, didn't know! Constructors are indeed tricky.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.