• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Accessing a classes functions from a second class and initialising the second classes data members with the first classes

I have 2 separate classes and want to assign the value of the private data members of one class to the values of the private data members of the second class. How do I go about accessing them through the public functions of the first class? I have 2 functions in the first class, one that reads in the 2 values from file and another that displays the two values on screen.

Thanks
0
blah_raah
Asked:
blah_raah
4 Solutions
 
vb_jonasCommented:
If they are private? You dont want to declare them as public / friend?
0
 
fridomCEO/ProgrammerCommented:
Provide accessors. All other stuff is hacking.

Regards
Friedrich
0
 
vb_jonasCommented:
Set up public properties and access the private data through them:


    Private newPropertyValue As Integer

    Public Property NewProperty() As Integer
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As Integer)
            newPropertyValue = value
        End Set
    End Property


0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
blah_raahAuthor Commented:
Here's an example of what im talking about:

class one
{
private:
             int minute;
             string label;
public:
            void read(ifstream& in)
            void write(ifstream& out)
}

class two
{
private:
             int timeMinute;
             string nameMinute;
public:
             void display()
}


For class one, the read() function reads in the values from a file, and the write function displays them to screen. For the second class I want to access class one and set the data members of class two to the values in class one and display them to screen (all within the display() function). I don't know how to go about doing this..

           
0
 
vb_jonasCommented:
From where do you start your program, where do you instantiate your classes?

You could for example instantiate class one from within class two, and access the data that way (via a public variable or property).
0
 
gstiegerCommented:
Try declaring class two as a friend of class one. This will allow class two to access the private members of class one:

class two;
class one
{
      friend two;

private:
             int minute;
             string label;
public:
            void read(ifstream& in);
            void write(ifstream& out);
};

class two
{
private:
             int timeMinute;
             string nameMinute;
public:
             void display();
};
0
 
fffej78Commented:
If you want two classes to interact, then consider an inheritance / aggregation relationship.  In this example, it looks like class two is duplicated the responsibility of class one.  You should strive to eliminate duplicity.

Aggregation ( http://en.wikipedia.org/wiki/Aggregation_%28object-oriented_programming%29 ) is when an object is composed of other objects.  For example, a Car object might have a number of Wheel objects.

Inheritance (  http://en.wikipedia.org/wiki/Inheritance_%28computer_science%29 ) is captures an "is a" relationship.  For example, a Car "is a" Vehicle, therefore it makes sense for a Car to be a derived from a Vehicle.  The Vehicle can describe the basics of vehicles, and the Car can specialise as required.

The aggregation approach would mean class two contains class one.

class two
{
private:
  one minuteLabel
public:
  void display();
};

Class two has it's own instance of class one which stores the minute and string label.  As "fridom" said, if you need to provide access do so via a method accessor.  So you would change class one to have extra information such as:

class one
{
private:
  int minute;
  string label;
public:
  void read(ifstream& in)
  void write(ifstream& out)
  public int minute () { return minute; }
  public string label() { return label; }
}

An alternative to aggregation would be to use inheritance.  You'd use this is class two "IS A" class one.  (without knowing the names of the class and what they do, it's difficult to judge which is right).  In this case class two extends from class one.

class one
{
protected: // made protected so subclass can access, alternatively use accessors as above
             int minute;
             string label;
public:
            void read(ifstream& in);
            void write(ifstream& out);
};

class one : public two
{
 // you can access minute and label inside here no problems
public:
             void display();
}

Friend classes seem an overcomplicated solution to a problem - you need to consider the domain you are working in to determine the appropriate relationship between the classes.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now