a.side ??? Is a an object???

Hello!
Please, can someone make me clear the construction a.side in next example? Does a not look like an object? But it is NOT an object, it is just an argument? Many thanks for any short&clear explanation!!!

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
  #include <iostream>
using namespace std;

class Square;

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

class Square {
  friend class Rectangle;
  private:
    int side;
  public:
    Square (int a) : side(a) {}
};

void Rectangle::convert (Square a) {
  width = a.side;      ///THAT HERE!!!
  height = a.side;   /// AND THAT HERE!!!
}
  
int main () {
  Rectangle rect;
  Square sqr (4);
  rect.convert(sqr);
  cout << rect.area();
  return 0;
}
it's an object that gets passed in as an argument.
a is an object of class Square.
side is a variable in class Square.
a.side is the variable side in the object a.

In the example given, sqr is an object of class Square that is passed as an argument to rect.convert().

A class is the definition(recipe) of an object.
Last edited on
Hello!
a and a mixed me up!
This ok?

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
#include <iostream>
using namespace std;

class Square;

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

class Square {
  friend class Rectangle;
  private:
    int side;
  public:
    Square (int a) : side(a) {}
};

void Rectangle::convert (Square b) {
  width = b.side;
  height = b.side;
}
  
int main () {
  Rectangle rect;
  Square sqr (4);
  rect.convert(sqr);
  cout << rect.area();
  return 0;
}
Oops, sorry about that.
I have edited my answer to be a little more clear.
Hello!
Many thanks, think I got it! But still obvviouly not completely, cause I would need help to "establish" the wikipedia example:

http://en.wikipedia.org/wiki/Friend_class

Class A obviously "sees" into the class B, and gives to all objects of the class B the i (lets, say index) value the same: 10.

The program stopps at the line 19, when I try to create objects of the calss B, with attribute index. Pleas,e does someone see where is the error?
Many thanks!!!

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
class B {
    friend class A; // A sees into the B class
 
private:
    int i;
};
 
class A {
public: 
   void a(B b);
};

void A::a(B b){
b.i=10;
}


int main(){
B obj1(2);
B obj2(3);
B obj3(4);


int z;
z=obj1.i;
cout<<z<<endl;
return 0;
}
Last edited on
Topic archived. No new replies allowed.