What happens to the instances of classes created within a constructor of another class?

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?

New contributor

PLE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật