Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Force
  5. {
  6.     double mass;
  7.     double acceleration;
  8.    
  9.     public:
  10.         Force(double m, double a);
  11.         Force(const Force&);
  12.         ~Force();
  13.         double Mass(void){
  14.             return mass;
  15.         }
  16. };
  17.  
  18. Force::Force(double m, double a)
  19. {
  20.     mass = m;
  21.     acceleration = a;
  22.     cout<<"Passing through the constructor with arguments\n";
  23. }
  24.  
  25. Force::Force(const Force &g)
  26. {
  27.     mass = g.mass;
  28.     acceleration = g.acceleration;
  29.     cout<<"Passing through copy constructor.\n";
  30. }
  31.  
  32. Force::~Force()
  33. {
  34.     cout<<"Passing through the destrctor.\n";
  35. }
  36.  
  37. void idle1(Force f)
  38. {
  39.     cout<<"Entering function idle ONE \n\n";
  40. }
  41.  
  42. Force idle2(Force &f)
  43. {
  44.     cout<<"Entering function idle TWO \n\n";
  45.     return f;
  46. }
  47.  
  48. Force idle3(Force *f)
  49. {
  50.     cout<<"Entering function idle Three\n\n";
  51.     return (*f);
  52. }
  53.  
  54. int main()
  55. {
  56.     cout<<" f1"<<endl;
  57.     Force f1(0.0,0.0);
  58.    
  59.     cout<<" f2"<<endl;
  60.     Force f2(4.3, 9.9);
  61.     f1 = Force(f2); // using the copy constructor to copy attribute values
  62.     //from source object f2 into destination object f1
  63.     cout<<"Calling idle1"<<endl;
  64.     idle1(f1);
  65.    
  66.     cout<<"f3"<<endl;
  67.     Force f3(7.5, 9.8);
  68.    
  69.     cout<<"f4"<<endl;
  70.     Force f4(0.0,0.0);
  71.    
  72.     cout<<"f4 <-copy<-f3"<<endl;
  73.     f4 = Force(f3);
  74.    
  75.     cout<<"calling idle2"<<endl;
  76.     Force f5 = idle2(f4);
  77.    
  78.     cout<<"Calling idle3"<<endl;
  79.     Force f6 = idle3(&f4);
  80.    
  81.     cout<<"The mass of the final force (#6): "<< f6.Mass() <<endl;
  82.     return 0;
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement