Skip to content

Instantly share code, notes, and snippets.

@yanmhlv
Last active January 3, 2016 03:19
Show Gist options
  • Save yanmhlv/8401502 to your computer and use it in GitHub Desktop.
Save yanmhlv/8401502 to your computer and use it in GitHub Desktop.
g++ -c *.cpp && g++ *.o && ./a.out
#include "point.h"
#include <iostream>
using namespace std;
int main(){
Point2D p1 = Point2D(10, 20);
cout << "Координаты точки p1: ";
p1.print_coordinates();
Point2D p2 = Point2D(30, 40);
cout << "Координаты точки p2: ";
p2.print_coordinates();
Point2D result = Point2D(0, 0);
result = p1 + p2;
cout << "Сумма точек p1 и p2: ";
result.print_coordinates(); // 40|60
result = p1 - p2;
cout << "Разница точек p1 и p2: ";
result.print_coordinates(); // 70|100
cout << "Дружественная функция класса Point2D: " << endl;
cout << p2.get_rand() << endl;
// for(int i=0; i<20; i++){
// Point3D p = Point3D(10, 10, 2);
// cout << p.get_count() << endl;
// }
return 0;
}
#include "point.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
int Point::get_rand(){
return rand() % 10;
}
Point2D::Point2D(int i1, int i2): Point() {
count += 1;
x = i1;
y = i2;
}
int Point2D::get_count(){
return count;
}
void Point2D::set_x(int new_x){
x = new_x;
}
void Point2D::set_y(int new_y){
y = new_y;
}
void Point2D::print_coordinates(){
cout << x << "|" << y << endl;
}
void Point3D::print_coordinates(){
cout << x << "|" << y << endl;
}
Point2D Point2D::operator+(Point2D& other){
Point2D p = Point2D(x + other.x, y + other.y);
return p;
}
Point2D Point2D::operator-(Point2D& other){
Point2D p = Point2D(x - other.x, y - other.y);
return p;
}
Point3D::Point3D(int i1, int i2, int i3): Point2D(0,0) {
count += 1;
x = i1;
y = i2;
z = i3;
}
class Count {
public:
int count;
virtual int get_count() =0;
};
class Point {
public:
virtual void print_coordinates() =0;
int get_rand();
};
class Point2D : public Point, public Count {
public:
Point2D(int, int);
void set_x(int);
void set_y(int);
void print_coordinates();
int get_count();
friend int Point::get_rand();
Point2D operator+(Point2D&);
Point2D operator-(Point2D&);
protected:
int x;
int y;
};
class Point3D : public Point2D {
public:
Point3D(int, int, int);
void set_z(int);
void print_coordinates();
friend int Point::get_rand();
Point3D operator+(Point3D&);
Point3D operator-(Point3D&);
protected:
int z;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment