Inheritance problem

N

Nasim

Suppose I have a code like following

////////////
class A {
int a;
public:
A() : a(1) {}
int get() const { return a; }
}

class B : public A {
int b;
public:
B() : b(2) {}
int get() const { return a+b; }
}

int main() {
B b;
cout << b.get(); //3
}
//////////////

Can I call get() function of the class A from the instantiated variable
b of class B? If not, how to achieve this without using pointers!
 
M

matke

First of all, you have to declare the variable a in class A as
protected, if you want to use it in class B.
If you want to use method Get from class A, but with the object of
class B, you should write this:
b.A::get();

Your program will do what you want if you write this code:
#include <iostream.h>
class A {
protected:
int a;
public:
A() : a(1) {}
virtual int get() const { return a; }\\it can be without virtual
} ;

class B : public A {
int b;
public:
B() : b(2) {}
int get() const { return (a+b); }
};


int main() {
B b;
cout << b.get()<<endl; //3
cout<<b.A::get();
cin.get();

}
 
L

Lionel B

Nasim said:
Suppose I have a code like following

Which certainly won't compile...
////////////


class A {
int a;
public:
A() : a(1) {}
int get() const { return a; }
}

}; <--- inserted missing semicolon
class B : public A {
int b;
public:
B() : b(2) {}
int get() const { return a+b; }
}

}; <--- inserted missing semicolon
int main() {
B b;
cout << b.get(); //3

std::cout << b.get(); //3

return 0; // or something
}
//////////////

Can I call get() function of the class A from the
instantiated variable b of class B? If not, how to
achieve this without using pointers!

What were you thinking of doing with pointers?

Try compiling this (since you obviously haven't yet) after the
indicated corrections. My compiler gives me a very informative error
message.
Hint: find out about the `protected' keyword.

Regards,
 
J

Jonathan Mcdougall

Lionel said:
Which certainly won't compile...
protected:



}; <--- inserted missing semicolon




std::cout << b.get(); //3

return 0; // or something

That is not necessary.
What were you thinking of doing with pointers?

Calling get() from a A* would yield the intended results.
Try compiling this (since you obviously haven't yet) after the
indicated corrections. My compiler gives me a very informative error
message.

What message?
Hint: find out about the `protected' keyword.

What's the point? The problem was with qualifying the function call,
not with the protected keyword (since that was obviously a typo).


Jonathan
 
L

Lionel B

Jonathan said:
protected:

Thanks. I was trying to get the OP to figure this out on their own.
That is not necessary.

Not strictly, but (a) it seems ugly to have a function declared with a
return value not return anything and (b) some other
script/program/whatever might want to test the return result of your
program. IMHO it is good style to have main() return a value.
Calling get() from a A* would yield the intended results.

Ok, it's not entirely clear (to me) what the "intended results" are. If
all the OP wants is to call A::get() then yes, I agree with you. On the
other hand, the comment // 3 on the line containing the cout implied to
me that what the OP really wants is to call B::get(). Hence I'm not
sure if the omission of `protected' really was a typo, or the entire
point of the post.
What message?

g++ test.cpp -o test.exe
test.cpp: In member function `int B::get() const':
test.cpp:7: error: `int A::a' is private <-------------
test.cpp:17: error: within this context
What's the point? The problem was with qualifying the function call,
not with the protected keyword (since that was obviously a typo).
Maybe... my mind-reading is a little rusty.

Lionel
 
R

Rolf Magnus

Lionel said:
Not strictly, but (a) it seems ugly to have a function declared with a
return value not return anything

That's right.
and (b) some other script/program/whatever might want to test the return
result of your program.

And? Leaving out the return statement in main() is the same as returning 0.
IMHO it is good style to have main() return a value.

IMHO, it's a braindead idea to allow the return from main (and only main) to
be the same as a "return 0". What is that exception good for?
 
J

Jonathan Mcdougall

Lionel said:
Not strictly, but (a) it seems ugly to have a function declared with a
return value not return anything and

Not when you know it does return a value.
(b) some other
script/program/whatever might want to test the return result of your
program. IMHO it is good style to have main() return a value.

main() *does* return 0 if you don't explictly write one and 0 is the
standard way of signaling success. When there's an error, it is
obviously right to return something else (such as 1). But when no error
happens, returning explictly is not necessary.
Ok, it's not entirely clear (to me) what the "intended results" are.

Umm.. calling A::get(), which was the only question the OP ever asked.
If
all the OP wants is to call A::get() then yes, I agree with you.

Good :)
On the
other hand, the comment // 3 on the line containing the cout implied to
me that what the OP really wants is to call B::get().

No, he's showing what is the current output, which is pretty kind since
I am not forced to compile his code.
Hence I'm not
sure if the omission of `protected' really was a typo, or the entire
point of the post.

You should definitely read the original post again.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,616
Latest member
gijoji4272

Latest Threads

Top