?
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
Medium Priority
?
93 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
Suggested Courses

764 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