calling the method from the parent class

J

jalkadir

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

Rolf Magnus

jalkadir said:
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()
 
H

Howard

jalkadir said:
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
 
H

Howard

Rolf Magnus said:
Should be:

const char* getStr() const {return str;}


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
 
H

Howard

Howard said:
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
 
R

Rolf Magnus

Howard said:
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.
 
P

Peter_Julian

| 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()
*/
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top