AbdulFathaah

vector addition C++

Dec 19th, 2023 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. /*program to do vector addition*/
  2.  
  3. #include<iostream.h>
  4. #include<conio.h>
  5.  
  6. class VecAdd
  7. {
  8. private : int a,b,c; //coefficients of i,j,k
  9. public : void read();
  10. void display();
  11. VecAdd add(VecAdd);
  12. };
  13. void VecAdd :: read()
  14. {
  15. cout<<"Enter coefficient of i: ";
  16. cin>>a;
  17. cout<<"Enter coefficient of j: ";
  18. cin>>b;
  19. cout<<"Enter coefficient of k: ";
  20. cin>>c;
  21. cout<<"\n";
  22. }
  23. void VecAdd :: display()
  24. {
  25. cout<<a<<"i + "<<b<<"j + "<<c<<"k\n";
  26. }
  27. VecAdd VecAdd :: add(VecAdd x)
  28. {
  29. VecAdd z;
  30. z.a=a+x.a;
  31. z.b=b+x.b;
  32. z.c=c+x.c;
  33. return z;
  34. }
  35. int main()
  36. {
  37. VecAdd v1,v2,v3;
  38. clrscr();
  39. v1.read();
  40. v2.read();
  41. cout<<"Before Addition:\n";
  42. v1.display();
  43. v2.display();
  44. v3=v1.add(v2);
  45. cout<<"After Addition:\n";
  46. v3.display();
  47. getch();
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment