Why is this program not printing (2,5)?

Hello!
Please, why is result this function is ment to print not (2,5)???

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


class point {
  private:
    int x, y;
  public:
    void init(int x1, int y1);
    int getX();
    int getY();
    void print();
};



void point::init(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(){
int x1=2;
int x3=5;

point pt;
pt.print();

return 0;
}
Last edited on
And even this did not help:

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


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



void point::init(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(){
int x1=2;
int x3=5;

point pt;
pt.point::print();

return 0;
}
p.s. Please, is indentation ok, or is there also sth to change and how?
Many thanks!
you're not calling print::init() so there will be no value assigned to x and y.

and yeah, the indentation needs to be fixed.. (generally in main() function )
1
2
3
4
5
6
7
8
9
10
11
12
13
// ...

int main() {
    int x1=2; // indent 4 spaces after a  ' { '
    int x3=5; // do you mean x2 = 5 ?

    point pt;

    pt.init( x1, x2 ); // call init so values can be assigned to members x and y
    pt.print(); // not point::print()

    return 0;
}
Last edited on
int main() {
int x1=2; // indent 4 spaces after a ' { '

you mean to leave four spaces after a '{' ? Why?

Sorry i dont know what indent means :D .
you mean to leave four spaces after a '{' ? Why?

Yes, between every { and } because it will make your code structured and clear :

http://www.cprogramming.com/tutorial/style.html
May I asked why you are not using a constructor instead of your initialization function? That way you can simply do point pt(2, 5);
http://www.cplusplus.com/doc/tutorial/classes/
http://www.learncpp.com/cpp-tutorial/85-constructors/
Topic archived. No new replies allowed.