New object

Mar 20, 2014 at 10:46am
hello!
Please, copy constructor is ment to create a new object.
WHat is the name of the new object there and how to output it?
(is it a mistake to use xopy constructor without using default one in the same program?)
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
#include<iostream>
#include<cstdlib>
 
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;
};
 
point::point(const point& t) {
  x=t.x; y=t.y;
}
 
int main(){
 

  point a;
  point b;
  a.init(3, 2);
  b.init(1,1);
  a.getX();
  cout<<a.getX()<<endl;
  b.print();
  cout<<"t:"<<t.print()<<endl;

  return EXIT_SUCCESS;
}
Last edited on Mar 20, 2014 at 1:03pm
Mar 20, 2014 at 10:50am
1) Please adopt a sensible indentation style. It will be a lot easier for you - and us - to read your code, and you'll be able to spot mistakes you've made in the flow of control much more easily.

2) You're not actually calling your copy constructor anywhere that I can see. You're only using the default constructor, to create a and b. So, no, right now, your copy constructor isn't creating a new object.

Do you understand what a constructor is, and when it's called?

Do you understand what a copy constructor is, and when it's called?
Mar 20, 2014 at 1:05pm
HELLO!
ccorrected indentation. Sorry.

If I want to make a copy of the object a, what am I supposed to do?
many thanks!!!
Mar 20, 2014 at 1:28pm
Please, look at this:
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
#include<iostream>
#include<cstdlib>

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(){
  point a;
  point b;
  a.init(3, 2);
  b.init(1,1);
  a.getX();
  cout<<a.getX()<<endl;
  b.print();

  point t=a;
  cout<<"here is object 't'!!!:"<<t.getX()<<endl;
  return 0;
}


Please, is that the right way we write copy constructors, or is that just the SAME as it???
MANY THANKS!!!
Last edited on Mar 20, 2014 at 1:29pm
Mar 20, 2014 at 1:30pm
Please, is line 45 calling the copy constructor or ?
Many thanks!
Mar 20, 2014 at 1:42pm
No, line 45 is not calling a copy constructor. It is calling the function getX() which returns an int.

You have no copy constructor in your class.
A copy constructor would appear as follows:
1
2
3
4
5
6
7
8
9
10
class point
{ ...
public:
  point (const point & pt)
  { x = pt.x; y = pt.y; }
...
};

// Make b a copy of a
point b (a);



Mar 20, 2014 at 1:59pm
Hello!
Please, what is missing here?

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
#include<iostream>
#include<cstdlib>

using namespace std;

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

};

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(){
  point a;
  point b;
  a.init(3, 2);
  b.init(1,1);
  a.getX();
  cout<<a.getX()<<endl;
  b.print();


  cout<<"copy: "<<point d (a); 

  return 0;
}
Last edited on Mar 20, 2014 at 2:00pm
Mar 20, 2014 at 2:07pm
Please, what is missing here?

There's still nothing in your code that ever calls the copy constructor.

Do you understand what a constructor is, and when it's called?

Do you understand what a copy constructor is, and when it's called?
Mar 20, 2014 at 2:25pm
Please, is there any link telling exactly what and where to do?

(I suppose it is ment to make another point. Ir is an object called d, and it is a point with coordnates same as point a, is it right?)

Is line 14 copy constructor? I suppose it is.
Many thanks!!!
Last edited on Mar 20, 2014 at 2:32pm
Mar 20, 2014 at 2:53pm
Please, is there any link telling exactly what and where to do?

Surely your textbook has a section on copy constructors?

If not, I'm sure it will be easy to use Google to search for a tutorial on copy constructors - and on constructors generally, if you're unclear on them.

It's a bit much to ask users here to do Google searches that you could easily do on your own.


Is line 14 copy constructor?

Yes, lines 14 - 15 define your copy constructor. As you already know, since the person who wrote it for you to copy-and-paste told you it was.
Last edited on Mar 20, 2014 at 2:56pm
Mar 20, 2014 at 3:01pm
Hello!
Please, am I supposed to call it like this:

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
#include<iostream>
#include<cstdlib>

using namespace std;

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

};

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(){
  point a;
  point b;
  a.init(3, 2);
  b.init(1,1);
  a.getX();
  cout<<a.getX()<<endl;
  b.print();

  point d=a;
  cout<<"copy: "<<point d (a); 

  return 0;
}

That is sth similar to that I find on net.
Many thanks!!!
Last edited on Mar 20, 2014 at 3:02pm
Mar 20, 2014 at 3:07pm
You have a few problems with your code.

1) You have no default constructor. Lines 37 and 38 result in an uninitialized object. Your compiler should have warned you that you have no default constructor. Lines 37 and 38 implictly call the default constructor.

2) Line 46 makes no sense. Assuming you meant something like this:
1
2
  point d(a);  // Make d same as a
  cout<<"copy: "<< d << endl;

Only problem with that is that you have not overloaded the << operator.




Mar 20, 2014 at 3:07pm
That code should fail to compile, because at line 46, you're attempting to put a variable declaration in the middle of a statement. Even if it were legal to do that, it would still give you an error, because you've already defined d in line 45.

Line 45 invokes the copy constructor, yes. Do you understand why?
Last edited on Mar 20, 2014 at 3:08pm
Mar 20, 2014 at 5:13pm
Hello, MikeBoy!
Well, actually I don't. I suppose, because it's name is point (Method from class). But it is better to check.

It is the only oe, but should it not have ()- meaning, parameters? Only default is without parameters.
In my teyxtbook, copy konstructor DOES have parameter when invoked...???
MANY THANKS!!!
Last edited on Mar 20, 2014 at 5:15pm
Mar 20, 2014 at 5:51pm
It is the only oe, but should it not have ()- meaning, parameters? Only default is without parameters.
In my teyxtbook, copy konstructor DOES have parameter when invoked...???


When initialising, this:

point d=a;

is exactly the same as this:

point d(a);

Both of those lines invoke the copy constructor, with a as the argument.

Note that this is only true for initialisation. If you have:

1
2
point d;
d=a;


that does NOT invoke the copy constructor, it invokes the assignment operator.

In general, MyType myVar = myValue; is interpreted identically to MyType myVar(myValue);
Mar 20, 2014 at 7:55pm
But the same wouldn't go for a function, would it?
Mar 20, 2014 at 8:06pm
But the same wouldn't go for a function, would it?

Um... what? I don't understand what you're asking.
Mar 20, 2014 at 8:06pm
Please, just one mroe q: if we have constructor for one thinkg, we always have to use constructors in that program, do we?

If we have function object.print(), then we can't use cout for printing results of the constructors, can we?

Many thanks!!!
Last edited on Mar 20, 2014 at 8:07pm
Mar 21, 2014 at 7:56am
Please, just one mroe q: if we have constructor for one thinkg, we always have to use constructors in that program, do we?

You really don't understand what a constructor is, or when it's called, do you?

I'm not going to re-type a textbook for you. You need to go to your textbook and take the time to read, and learn, properly what a constructor is, and when it's called.

If we have function object.print(), then we can't use cout for printing results of the constructors, can we?

What? That makes no sense at all. Why would having a method called print() stop you from being able to use cout?

I'm starting to wonder if Catfish was right...
Mar 21, 2014 at 6:37pm
Ideone did not want to compile first one, but when I changed cout to print() then it did...

btw many online compilers were not working properly ysterday...
Topic archived. No new replies allowed.