?
Solved

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

Posted on 2006-05-17
9
Medium Priority
?
216 Views
Last Modified: 2013-11-23
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
Comment
Question by:blah_raah
7 Comments
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16705956
If they are private? You dont want to declare them as public / friend?
0
 
LVL 24

Accepted Solution

by:
fridom earned 500 total points
ID: 16705970
Provide accessors. All other stuff is hacking.

Regards
Friedrich
0
 
LVL 12

Assisted Solution

by:vb_jonas
vb_jonas earned 500 total points
ID: 16706062
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
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.

 

Author Comment

by:blah_raah
ID: 16706260
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
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16706865
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
 
LVL 2

Assisted Solution

by:gstieger
gstieger earned 500 total points
ID: 16713515
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
 
LVL 4

Assisted Solution

by:fffej78
fffej78 earned 500 total points
ID: 16735896
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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Starting up a Project
Screencast - Getting to Know the Pipeline

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question