Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4.  
  5. class VECTOR {
  6. private:
  7.     int v[3];
  8. public:
  9.     // контруктор по умочанию
  10.     VECTOR();
  11.     // контруктор с тремя аргументами
  12.     VECTOR(int a, int b, int c);
  13.     void read();
  14.     void print();
  15.     VECTOR mult(int s) const ;
  16.     VECTOR operator+(const VECTOR& other);
  17.     VECTOR operator-(const VECTOR& other);
  18.     VECTOR& operator--();
  19. };
  20.  
  21. VECTOR::VECTOR() {
  22.     v[0] = v[1] = v[2] = 0;
  23. }
  24.  
  25. VECTOR::VECTOR(int a, int b, int c) {
  26.     v[0] = a;
  27.     v[1] = b;
  28.     v[2] = c;
  29. }
  30.  
  31. void VECTOR::read() {
  32.     std::cin >> v[0] >> v[1] >> v[2];
  33. }
  34.  
  35. void VECTOR::print() {
  36.     std::cout << "("
  37.               << v[0] << ", "
  38.               << v[1] << ", "
  39.               << v[2] << ")" << std::endl;
  40. }
  41.  
  42. VECTOR VECTOR::operator+(const VECTOR& other) {
  43.     return VECTOR(v[0] + other.v[0], v[1] + other.v[1], v[2] + other.v[2]);    
  44. }
  45.    
  46. VECTOR VECTOR::operator-(const VECTOR& other) {
  47.     return *this + other.mult(-1);
  48. }
  49.  
  50. VECTOR& VECTOR::operator--() {
  51.     v[0]--;
  52.     v[1]--;
  53.     v[2]--;
  54.    
  55.     return *this;
  56. }
  57.  
  58. VECTOR VECTOR::mult(int s) const {
  59.     return VECTOR(v[0] * s, v[1] * s, v[2] * s);
  60. }
  61.  
  62. int main()
  63. {
  64.   VECTOR vec;
  65.   vec.read();
  66.   std::cout << "vec = ";
  67.   vec.print();
  68.  
  69.   std::cout << "vec2 = vec * 2 = ";
  70.   VECTOR vec2 = vec.mult(2);
  71.   vec2.print();
  72.  
  73.   std::cout << "vec + vec2 = : ";
  74.   (vec + vec2).print();
  75.   std::cout << "vec + vec2 = : ";
  76.   (vec - vec2).print();
  77.   std::cout << "--vec = ";
  78.   (--vec).print();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement