Advertisement
Anton0093

4_4

Oct 24th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class res
  5. {
  6. public:
  7.     void read_vec();
  8.     res operator +( const res a);
  9.     int vec[2];
  10.     void vyvod_sum();
  11. };
  12.  
  13.  
  14. void res::read_vec(){
  15.     for (int i=0; i<2; i++)
  16.     {
  17.         cout<<"Введите "<<i+1<<"-ю координату:" <<endl;
  18.         cin>>vec[i];
  19.         if(!cin.good())
  20.         {
  21.             cout << "Значение введено неверно."<<endl<<"Повторите ввод" << endl;
  22.             cin.clear();
  23.             cin.ignore();
  24.             cin>>vec[i];
  25.         }
  26.     }
  27. };
  28.  
  29. res res::operator+(const res a){
  30.     res result;
  31.     for (int i =0; i<2; i++){
  32.         result.vec[i] = vec[i] + a.vec[i];
  33.     }
  34.     return result;
  35. };
  36.  
  37. void res::vyvod_sum(){
  38.     for (int i=0; i<2; i++)
  39.     {
  40.         cout<<vec[i]<<endl;
  41.     }
  42. };
  43.  
  44.  
  45. int main(){
  46.     setlocale(LC_ALL, "Russian");
  47.     res A,B,C;
  48.     cout<<"Первый двумерный вектор \n";
  49.     A.read_vec();
  50.     cout << "Второй двумерный вектор \n";
  51.     B.read_vec();
  52.     C = A + B;
  53.     cout<<"Сумма векторов равна "<<endl;
  54.     C.vyvod_sum();
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement