class in c++

hi
i see this code example about Friendship and inheritance in c++ tut but i don't understand this line of code

class CSquare;

what does it do and why we declared it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// friend class
#include <iostream>
using namespace std;

class CSquare;

class CRectangle {
    int width, height;
  public:
    int area ()
      {return (width * height);}
    void convert (CSquare a);
};

class CSquare {
  private:
    int side;
  public:
    void set_side (int a)
      {side=a;}
    friend class CRectangle;
};

void CRectangle::convert (CSquare a) {
  width = a.side;
  height = a.side;
}
  
int main () {
  CSquare sqr;
  CRectangle rect;
  sqr.set_side(4);
  rect.convert(sqr);
  cout << rect.area();
  return 0;}



CRectangle is declared as friend of CSquare because it is designed to directly access private variables of CSquare. Without being a friend CRectangle::convert(...) couldn't "calculate" width and height.

This way is dangerous because it violates a design principle to hide an objects attributes against direct mutual access. A getter like f.e.
int CSquare::get_side() const would be more appropriate.
In class CRectangle: void convert (CSquare a);
Class CSquare declared below, so CRectangle doesn't know anything about it existence. So we made a forward declaration to tell, that such class exist (and it is really a class, not some typedef of something)
thanks for your answers
i have another question
why some function have const keyword
for example in this code

1
2
3
4
     double sin() const
      {
          return std::sin( degree() * DEG2RAD );
      }

what does it do?
and
i don't understand this one
1
2
3
4
5
 const
    double & degree() const
      {
          return M_degree;
      }

what & do ? why write 2 const s?
Last edited on
why some function have const keyword

If a member function is marked const, it is not allowed to change any member variables.

what & do ?

The & (ampersand) means that the function will return a reference to a variable, to M_degree in this case. Here references are explained fully: http://www.learncpp.com/cpp-tutorial/611-references/

why write 2 const s?

The first const means that the returned variable will be a const one, so that it can't be changed. The second const I explained above.

Additonally in your example the first const is mandatory because otherwise you couldn't return a reference to an attribute (C++ member variable) out of a const declared method.
If the returned reference wouldn't be declared const anybody could modify the attributes value which in fact would be a modifucation of the object - but this is forbidden by the second const.
1
2
3
4
const int &example()
{
    return var;
}
In this case, returning by const reference is pointless; it is less efficient than this identical code:
1
2
3
4
int example()
{
    return var;
}
Always pass and return primitive types by value, or by reference if you need to modify them. Passing or returning by const reference is useless and less efficient.
tnx for your answer
in this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Size2D {
private:
    //! x range
    double M_length;

    //! y range
    double M_width;

public:

    /*!
      \brief default constructor.
    */
    Size2D()
        : M_length( 0.0 )
        , M_width( 0.0 )
      { }

how set value with constrcure? m_width and M_lenght are not function but in this example passing value to them? how does it possible?
It is called direct initialization
compare:
1
2
int x(5);
std::string y("Hello");

http://en.cppreference.com/w/cpp/language/direct_initialization
thanks so much
in example of this page
http://www.cplusplus.com/reference/set/set/insert/
in line 8 what is
set<int>::iterator

and in line 9
std::pair<std::set<int>::iterator,bool> ret;

what does pair do?
please explain for me
i don't understand it.
Pair basically is a templated struct with two members. Another interpretation that it is pair of variables types. In your case it is iterator of set<int> and bool.
Consider following: std::pair<int, double> students[10];
you can make first (int) be their grade and second (double) mean of their marks.
And if you sort array, you will not lose relation between grade and marks of specific individual.
thanks
i have another problem
when i run the example in this page
http://www.cplusplus.com/reference/map/map/at/
these errors occur

hanie.cpp:13:3: error: ‘cout’ is not a member of ‘std’
hanie.cpp:14:13: error: ‘it’ does not name a type
hanie.cpp:14:34: error: expected ‘;’ before ‘it’
hanie.cpp:14:46: error: ‘class std::map<char, int>’ has no member named ‘cend’
hanie.cpp:15:5: error: ‘cout’ is not a member of ‘std’
hanie.cpp:16:3: error: ‘cout’ is not a member of ‘std’
You need to use a C++11 compiler.

http://liveworkspace.org/code/2zqy73$0
thanks
in this example what does pointer do? how does it work?
ArmAction * clone() const



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Arm_Off
    : public ArmAction {
public:
    /*!
      \brief constructor
     */
    Arm_Off()
      { }
    bool execute( PlayerAgent * agent );
    
    ArmAction * clone() const
      {
          return new Arm_Off();
      }
};

}
Last edited on
It returns pointer to new instance of class Arm_Off
Topic archived. No new replies allowed.