By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

calling the method from the parent class

P: n/a
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.

Sep 29 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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()
Sep 29 '05 #2

P: n/a

"jalkadir" <ja******@gosonic.ca> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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


Sep 29 '05 #3

P: n/a

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:dh*************@news.t-online.com...
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
Sep 29 '05 #4

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:b9*******************@bgtnsc04-news.ops.worldnet.att.net...

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
Sep 29 '05 #5

P: n/a
Howard wrote:
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.

Sep 29 '05 #6

P: n/a

"jalkadir" <ja******@gosonic.ca> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
| 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 <iostream>

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()
*/
Sep 30 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.