470,594 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

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.

Sep 29 '05 #1
6 10774
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

"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

"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

"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
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

"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.

Similar topics

4 posts views Thread by Murat Tasan | last post: by
14 posts views Thread by Axel Straschil | last post: by
6 posts views Thread by Jon Hyland | last post: by
1 post views Thread by NOSPAM | last post: by
1 post views Thread by - vhannak | last post: by
5 posts views Thread by Nick Flandry | last post: by
1 post views Thread by =?Utf-8?B?cmFuZHkxMjAw?= | last post: by
1 post views Thread by Ryan Krauss | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.