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
90 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 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