Solved

5. How is called the usage of an object of a class as a field of another class?

Posted on 2015-01-23
5
77 Views
Last Modified: 2015-01-24
How is called the usage of an object of a class as a field of another class?
0
Comment
Question by:Nusrat Nuriyev
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40567467
You basically call that a class member, nothing else. E.g.

class A {

// ...
};

class B {


// ...

public:

  A myA; // here, an instance of 'class A' is a member of every instace of 'class B'
};

Open in new window

0
 

Author Comment

by:Nusrat Nuriyev
ID: 40567613
Sorry, I mean this question:
5. How the usage of an object of a class as a field of another class is named?

Probably, this question deals with keyword friend, I have read about that but, don't have enough knowledges.
0
 
LVL 86

Expert Comment

by:jkr
ID: 40567626
Ahh, I see. The C++ 'friend' relationship allos the classes that are declared as 'friend' to access all data in the declaring class. There's the catch phrase "In C++, your friends have access to your privates" which humorously illustrates that. You can also declare single functions or operators as friends, which sometimes  is neccessary when you e.g. want to sort a sequence based on a protected or private value. Wikipedia outlines that nicely: http://en.wikipedia.org/wiki/Friend_class
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40568086
#include <iostream>

using namespace std;

class Temperature
{
    private:
        double kelvin;
        double celcius;
        double fahrenheit;
    public:
        // Required constructors
        Temperature()
        {
            kelvin = 273.15;
            celcius = 0;
            fahrenheit = 32;
        }
        Temperature(double k, double c, double f)
        {
            kelvin = k;
            celcius = c;
            fahrenheit = f;
        }

        friend ostream& operator<<(ostream &output, const Temperature &T)
        {
            output << "K: " << T.kelvin << " C: " << T.celcius << " F: " << T.fahrenheit;
            return output;

        }

        friend istream& operator>>(istream &input, Temperature &T)
        {
            input >> T.kelvin >> T.celcius >> T.fahrenheit;
            return input;
        }

};

int main()
{
    Temperature t1;
    cin >> t1;
    cout << t1;
    return 0;
}

Open in new window


Could you explaing why it will won't work without friend keyword even if Temperature class data members are public?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40568180
That's because this example is badly grouped, making the impresion that these operators are in fact class members, whereas they are in fact global operators that aren't members of the class. Took me a while to spot that, You can see that both operators take a 'Temperature&' argument, which would not be necessary if they were members. That example should better be written like

#include <iostream>

using namespace std;

class Temperature
{
        friend ostream& operator<<(ostream&, const Temperature&);
        friend istream& operator>>(istream&, Temperature&);
    private:
        double kelvin;
        double celcius;
        double fahrenheit;
    public:
        // Required constructors
        Temperature()
        {
            kelvin = 273.15;
            celcius = 0;
            fahrenheit = 32;
        }
        Temperature(double k, double c, double f)
        {
            kelvin = k;
            celcius = c;
            fahrenheit = f;
        }
};

ostream& operator<<(ostream &output, const Temperature &T)
{
    output << "K: " << T.kelvin << " C: " << T.celcius << " F: " << T.fahrenheit;
    return output;
}

istream& operator>>(istream &input, Temperature &T)
{
    input >> T.kelvin >> T.celcius >> T.fahrenheit;
    return input;
}

int main()
{
    Temperature t1;
    cin >> t1;
    cout << t1;
    return 0;
}
                                          

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

932 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now