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) 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?
#include<iostream>
#include<cstdlib>
usingnamespace 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;
}
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.
#include<iostream>
#include<cstdlib>
usingnamespace 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!!!
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.
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?
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!!!
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);
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?