conversion constructor

Hello!
I do not have in my textbook any chapter nor idex item "conversion constructor".

Pleasse, can someone help me with changig a and y in object a?
Can someone just give me a hint, where to look?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include<iostream>
  #include<cstdlib>

using namespace std;

class point {
  private:
   int x, y;
  public: 
   point(int x1, int y1);
   int getX();
   int getY();
   void print();
   point (const point & pt)
  { x = pt.x; y = pt.y; }

};

 point::point(int x1, int y1) {
  x = x1; 
  y = y1;
}

int point::getX() {
  return x; 
}

int point::getY() {
  return y; }
  void point::print() {
  cout << "(" << x << ", " << y << ") " << endl;
}



int main(){
  
  point a(3, 2);
  point b(1,1);
  a.getX();
  a.getY();
  b.getX();
  b.getY();
  
  cout<<a.getX()<<endl<<a.getY()<<endl<<b.getX()<<endl<<b.getY()<<endl;
  b.print();
  a.print();
  
  cout<<endl;
  point d(a);
  d.print(); 

  return 0;
}


Do I HAVE to declare a new object with new values or is it possible just to change values in the same object?
Many thanks!
Last edited on
you can declare a public member function so called mutator which essentially provides way to change private members of a class ( can also be used in validation )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class point
{
private :
  int x, y

public :
  //...
  void setX( int x ) { this->x = x; }

  // ...
};

int main()
{
  point p;
  p.setX( 100 );
}
Last edited on
Topic archived. No new replies allowed.