I am trying to learn the basics of C++ classes and have written the code below
#include <iostream>
#include <string>
using namespace std;
// defines a 'point'
class Point {
public:
double x;
double y;
// can also double x,y;
// Constructors. Can have multiple constructors. This is possbile thanks to overloading feature
// Constructors are all configured using the method with same name as class name.
Point() {
x = 0.0;
y = 0.0;
cout << "A point was created with default setting! Its address is " << this << endl;
} // default constructor places point at origin
Point(double nx, double ny) {
x = nx;
y = ny;
cout << "A point was created! Its address is " << this << endl;
// Point.x = nx, Point.y = ny
}
// Copy Constructors. Do not need to define explicitly if you don't want some special features
// a point class is passed by reference (&o)
// Why define it? maybe you just want x coordinate of point to be copied or print some messages when copied.
Point(Point& o) {
x = o.x;
y = o.y;
cout << "A point was copied from address " << &o << " and its points had addresses " << &o.x << " and " << &o.y << endl;
cout << "Copied point has address " << this << " and its points has addresses " << &x << " and " << &y << endl;
}
string coordiate() {
return "(" + to_string(x) + "," + to_string(y) + ")";
}
};
// Vector is composed of two points, start and end
class Vector {
public:
Point start;
Point end;
Vector() {
}
Vector(Point x, Point y) {
start = x;
end = y;
}
};
class VectorByRef {
public:
Point start;
Point end;
VectorByRef() {
}
// if you specify arguments to constructor, then vector is created by reference to the points.
// if the points in question changes, vector also changes.
VectorByRef(Point &x, Point &y) {
start = x;
end = y;
}
};
int class_example() {
Point start; // put a point at origin
cout << "Its coordinates are (" << start.x << "," << start.y << ")n";
Point end(1.0, 1.0); // put a point at 1,1
cout << "Its coordinates are (" << end.x << "," << end.y << ")n";
Point foot(1.0, 0.0); // put a point a 1,0
cout << "Its coordinates are (" << foot.x << "," << foot.y << ")" << endl << endl;
// Copy a point
cout << "Copying a point..." << endl;
Point copy_end = end;
cout << endl << endl << "Creating a new vector..." << endl;
Vector uVec(start, end);
cout << endl << endl;
cout << "a vector with start and end points specified has been created " << uVec.start.coordiate() << " " << uVec.end.coordiate() << endl;
cout << "its points has addresses " << &uVec.start << " and " << &uVec.end << endl;
return 0;
}
When run, following results are printed
and my concern is about what’s happening to the “copied” points.
According to the printed results,
- Original points have addresses 0000003F9EDAF328 (end point) and 0000003F9EDAF2F8 (start point)
- Copied instances of above points are created at 0000003F9EDAF578 and 0000003F9EDAF5C8 respectively
- Default points are created when vector class is initiated at 0000003F9EDAF3B8 and 0000003F9EDAF3C8
- The values at the initiated points then change to that of copied points
But what happens to the copied points that are created in the middle, 0000003F9EDAF578 and 0000003F9EDAF5C8 ? Are they automatically deallocated from the memory?
Can’t we just “set the values” of 0000003F9EDAF3B8 and 0000003F9EDAF3C8 to the values of end and start points without creating a copy so to save a piece of memory?
PLE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.