We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

class question

strmtrpr
strmtrpr asked
on
Medium Priority
209 Views
Last Modified: 2010-04-02
I have two classes the first has some functions.
the second class needs to use those functions
how can I do this.
HELP!!!
Comment
Watch Question

Commented:
Declare the functions as public or make the 2nd class a descendent of the 1st class.

Author

Commented:
In the second class I have to write a function that uses the first classes functions.
I have declared the functions as public already.
the second class has private member ---      flower** ptr   // flower is the name of the first class.


Author

Commented:
In the second class I have to write a function that uses the first classes functions.
I have declared the functions as public already.
the second class has private member ---      flower** ptr   // flower is the name of the first class.


Commented:
to be able to call public functions of another class

class flower{
 public:
    func1()
}

class two{
 private:
    flower* ptr;
 voif func(){
    ptr = new flower; // <-- creating a flower object
    ptr->func1(); // <-- accessing the flower's class's public
                         function
 }

}

however, if you want to use flower** ptr instead
then: (*ptr) = new flower;
      (*ptr)->func1();


Commented:
jbalagop,

This time you are not guessing.  (Well you almost are because strmtrpr is not providing any details, but it seems we have just enough details.)  It would not have been inappropriate to answer on the last comment.  

There is a fine line between beign to greedy and answering before you know you have really provided the answer and being to honerable and not answering until yoiu get confirmation.  The problem is that if you don't answer there is a good chance someone will come along and submit your idea as an answer.  This is unethical, but a few do it.  In many cases, the client realizes that they don't deserve the points, and rejects their answer so you can submit.  But not always.  So if you can answer, do.

todd
Well said, both of you!

Author

Commented:
jbalagop,  
 answer the question and I will give the points,
Thanks for the help. I still don't know why I over look the simple stuff.
THANKS.
Without going into debates on ethics, I have a suggestion. Why don't you make the first class a friend of the second one.

Commented:
I have suggestion to your problem; make the second class a sub-class of the first one as follows:

class two: public flower
{
   private:
      ......//all your data members
   public:
      ......//overides or additional methods to the first class.
}

By doing this, the second class has access to both data members and functions of the first class and can still have other methods not known to the first class. You can also read more on any C++ book about Inheritance.

Hope this helps.

Commented:
Billh, please read the question history.  That solution was already proposed by jbalagop (as well as others).  strmtrpr, has a working solution from what jbalagop proposed and has asked that jbalagop submit an answer.

Author

Commented:
I am waiting for  jbalagop to answer so he can get the points
sorry billyh's

Commented:
Maybe you can try declaring the second class as the friend of the first class.

Commented:
xu, please read the question history.  That idea was proposed twice already.  strmfrpr, has a working solution.  he had opened the question so that jbalagop can answer.  Now jbalagop cannot answer.

Author

Commented:
the question has already been answered
Thanks anyway

Commented:
There are a few ways to do this :

 - If you want, you can inherit the second one from the first
 - You can declare the second one as a friend class to the first
 - You can declare them as static functions.
   ( Note : this may violate your class structure)
 

Commented:
This is kind of funny.

Commented:
I think this has become an expert IQ test--most are failing.

canoquz, read the question history from the beginning to the end before you answer.

Commented:
I think jbalagop may have forgotten to mark the e-mail notification box for this question and that is why he is not responding.    I asked that customer service try e-mailing him directly concerning this.   In the meant time.

THIS QUESTION HAS BEEN ANSWERED.  WE ARE WAITING FOR JBALAGOP TO SUBMIT ANSWER.   NO ONE ELSE SHOULD ANSWER IT!!!!!!!!!!!!!!!!!!!!

Author

Commented:
I wish there was a way just to give jbalagop the points.

I sent an email to jbalagop to post an answe to this question.  It is amazing how many experts don't read through the question before posting an answer.

Linda Gardner
Customer Service @ Experts Exchange

Author

Commented:
That is the truth
Thanks Linda
haha. This was pretty funny. :-)
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks Again for the help
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.