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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

816 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

8 Experts available now in Live!

Get 1:1 Help Now