Skip to content

Commit

Permalink
Примери за извикване на конструктори на член-данни обекти
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed Mar 30, 2022
1 parent fd814d7 commit ffb3cb7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lectures/1/geometry/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ void testGeometry() {
void testGeometry3D() {
Point3D p(1, 3, 8);
p.printnl();
Point3D q;
Point3D q = p;
q.printnl();
q.read();
p.translate(q);
std::cout << p.distanceToOrigin() << std::endl;
Expand Down
6 changes: 6 additions & 0 deletions lectures/1/geometry/point2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
#include "point2d.hpp"

Point2D::Point2D() {
std::clog << "Point2D::Point2D()\n";
setX(0);
setY(0);
}

Point2D::Point2D(double _x, double _y) {
std::clog << "Point2D::Point2D(...)\n";
setX(_x);
setY(_y);
}

Point2D::Point2D(Point2D const& other) : x(other.x), y(other.y) {
std::clog << "Point2D::Point2D(Point2D const&)\n";
}

// селектор за разстояние до центъра на координатната система
double Point2D::distanceToOrigin() const {
//return distanceTo(Point2D());
Expand Down
1 change: 1 addition & 0 deletions lectures/1/geometry/point2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Point2D {
// конструктори
Point2D();
Point2D(double _x, double _y);
Point2D(Point2D const&);

// селектори за координатите
double getX() const { return x; }
Expand Down
14 changes: 12 additions & 2 deletions lectures/1/geometry/point3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@
#include "point3d.hpp"

Point3D::Point3D() {
std::clog << "Point3D::Point3D()\n";
setP(Point2D());
setZ(0);
}

Point3D::Point3D(double _x, double _y, double _z) {
setP(Point2D(_x, _y));
Point3D::Point3D(double _x, double _y, double _z) : p(_x, _y) {
std::clog << "Point3D::Point3D(...)\n";
getP().printnl();
// setP(Point2D(_x, _y));
setZ(_z);
}

/*
Point3D::Point3D(Point3D const& other) : p(other.p), z(other.z) {
// setP(other.getP());
//setZ(other.getZ());
}
*/

Point3D::Point3D(Point2D const& _p, double _z) {
setP(_p);
setZ(_z);
Expand Down
3 changes: 2 additions & 1 deletion lectures/1/geometry/point3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class Point3D {
Point3D();
Point3D(Point2D const& _p, double _z);
Point3D(double _x, double _y, double _z);
// Point3D(Point3D const&);

// селектори
Point2D getP() const { return p; }
Point2D const& getP() const { return p; }
double getX() const { return p.getX(); }
double getY() const { return p.getY(); }
double getZ() const { return z; }
Expand Down

0 comments on commit ffb3cb7

Please sign in to comment.