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
75 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 clear a vector as well as how to detect empty vectors in C++.

708 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

14 Experts available now in Live!

Get 1:1 Help Now