 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
jalkadir Guest
|
Posted: Thu Sep 29, 2005 2:38 pm Post subject: calling the method from the parent class |
|
|
Let's say that I have this class:
class Parent{
private: char* str;
public: const char* getStr(){return str;}
};
And then I create a child class
class Child{
private: std::string str;
public: std::string& getStr(){return str;}
};
How do I call the Parent class method in a program, i.e.
int main(){
Child obj;
std::cout << obj.getStr() << std::endl;
return 0;
}
In the above example the method access would be the Child::getStr(),
but I would like to access the Parent::getStr(), how do I do that?
TIA.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Sep 29, 2005 2:45 pm Post subject: Re: calling the method from the parent class |
|
|
jalkadir wrote:
| Quote: | Let's say that I have this class:
class Parent{
private: char* str;
public: const char* getStr(){return str;}
|
Should be:
const char* getStr() const {return str;}
| Quote: | };
And then I create a child class
class Child{
private: std::string str;
public: std::string& getStr(){return str;}
};
How do I call the Parent class method in a program, i.e.
int main(){
Child obj;
std::cout << obj.getStr() << std::endl;
return 0;
}
In the above example the method access would be the Child::getStr(),
but I would like to access the Parent::getStr(), how do I do that?
|
obj.Parent::getStr()
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Thu Sep 29, 2005 2:45 pm Post subject: Re: calling the method from the parent class |
|
|
"jalkadir" <jalkadir (AT) gosonic (DOT) ca> wrote
| Quote: | Let's say that I have this class:
class Parent{
private: char* str;
public: const char* getStr(){return str;}
};
And then I create a child class
class Child{
private: std::string str;
public: std::string& getStr(){return str;}
};
How do I call the Parent class method in a program, i.e.
int main(){
Child obj;
std::cout << obj.getStr() << std::endl;
return 0;
}
In the above example the method access would be the Child::getStr(),
but I would like to access the Parent::getStr(), how do I do that?
|
What problem are you having accessing memebrs of a Parent object? You
created a Child object and called a function from it. Why can't you just
create a Parent object and call a function on it, too? Did you try? If so,
what's that code, and what error or problem do you see?
(And by the way, is there some reason you're calling those Parent and Child?
Nothing in the code you've shown makes any connection between those two
classes. Did you intend for Child to be a derived class of Parent? Or is
the Parent class supposed to point to a list of its children (and
vice-versa)? Or were those just arbitrary names?)
-Howard
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Thu Sep 29, 2005 2:48 pm Post subject: Re: calling the method from the parent class |
|
|
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote
| Quote: | jalkadir wrote:
Let's say that I have this class:
class Parent{
private: char* str;
public: const char* getStr(){return str;}
Should be:
const char* getStr() const {return str;}
};
And then I create a child class
class Child{
private: std::string str;
public: std::string& getStr(){return str;}
};
How do I call the Parent class method in a program, i.e.
int main(){
Child obj;
std::cout << obj.getStr() << std::endl;
return 0;
}
In the above example the method access would be the Child::getStr(),
but I would like to access the Parent::getStr(), how do I do that?
obj.Parent::getStr()
|
Eh? I see no member called "Parent" in the Child class. (And if there were,
"Parent" would not be a very good name for it, since that's the class name.)
-Howard
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Thu Sep 29, 2005 2:52 pm Post subject: Re: calling the method from the parent class |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote
| Quote: |
obj.Parent::getStr()
Eh? I see no member called "Parent" in the Child class. (And if there
were, "Parent" would not be a very good name for it, since that's the
class name.)
|
Oh, I see the :: there now. I was thinking "obj.Parent.getStr()". Still,
Parent is not a class defined _in_ Child, so that's not going to work, is
it?
-Howard
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Sep 29, 2005 3:13 pm Post subject: Re: calling the method from the parent class |
|
|
Howard wrote:
| Quote: | obj.Parent::getStr()
Eh? I see no member called "Parent" in the Child class. (And if there
were, "Parent" would not be a very good name for it, since that's the
class name.)
Oh, I see the :: there now. I was thinking "obj.Parent.getStr()". Still,
Parent is not a class defined _in_ Child, so that's not going to work, is
it?
|
Well, assuming that Child was supposed to be derived from Parent, yes, it
will work.
|
|
| Back to top |
|
 |
Peter_Julian Guest
|
Posted: Fri Sep 30, 2005 1:41 am Post subject: Re: calling the method from the parent class |
|
|
"jalkadir" <jalkadir (AT) gosonic (DOT) ca> wrote
| Quote: | Let's say that I have this class:
class Parent{
private: char* str;
public: const char* getStr(){return str;}
};
And then I create a child class
class Child{
private: std::string str;
public: std::string& getStr(){return str;}
};
How do I call the Parent class method in a program, i.e.
int main(){
Child obj;
std::cout << obj.getStr() << std::endl;
return 0;
}
In the above example the method access would be the Child::getStr(),
but I would like to access the Parent::getStr(), how do I do that?
|
The Parent class and the Child class in your project are completely
unrelated to each other. There is no relationship between the two types.
Its the programmer's responsability to specify any relationship, if any.
#include
class Parent
{
public:
Parent() { }
virtual ~Parent() { }
virtual void foo() { std::cout << "Parent::foo()n"; }
};
class Child : public Parent
{
public:
Child() { }
~Child() { }
void foo() { std::cout << "Child::foo()n"; }
};
int main()
{
Child child;
child.foo();
Parent parent;
parent.foo();
Parent *p = &child;
p->foo(); // upcast
return 0;
}
/*
Child::foo()
Parent::foo()
Child::foo()
*/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|