473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 10945
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
14
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
3
by: scott | last post by:
hi all, hope some one can help me. Ill try and explain what im trying to do as best i can. i have a parent class that has a vertual function, lets call it virtual int A(). That vertual function...
6
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make...
1
by: NOSPAM | last post by:
I have a question on calling parent class... What I want to do is: when calling the derived class, it automatically executes the parent's method, is it possible? // main code: Derived d = new...
1
by: - vhannak | last post by:
I have a class (sharedClass) that is instantiated (not derived) by two other classes (guiClassA and guiClassB). The sharedClass needs to be able to call a method that is defined in its parent...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
1
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
1
by: Ryan Krauss | last post by:
I am trying to call a parent's __init__ method from the child's: class ArbitraryBlock(InnerBlock): def __init__(self, codelist, noout=False, **kwargs): InnerBlock.__init__(self, codelist,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.