Advertisement
Guest User

Untitled

a guest
May 8th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. //pendulum.h
  2. #include<string>
  3. using std::string;
  4. #ifndef pendulum_H
  5. #define pendulum_H
  6.  
  7. class Pendulum
  8. {
  9. public:
  10.     Pendulum(const double,const double,double,double);
  11.     //check function
  12.     const double check(const double, const string) const;  //used for (L,M)
  13.     // getfuncties
  14.     double getL() const;
  15.     double getM() const;
  16.     double getTheta();
  17.     double getOmega();
  18.     //overload operator
  19.     Pendulum operator+ (Pendulum);
  20.     Pendulum operator*(const double a);
  21.     Pendulum operator=(Pendulum);
  22.     //copy constructor
  23.     Pendulum(const Pendulum&  );
  24. private:
  25.     double L_,M_,Theta_,Omega_;
  26. };
  27. #endif
  28.  
  29. //pendulum.cpp
  30. #include "pendulum.h"
  31. #include <iostream>
  32. #include <string>
  33. using namespace::std;
  34. //constructor
  35. Pendulum::Pendulum(const double L, const double M, double Theta, double Omega)
  36.          :L_(check(L,"L")),M_(check(M,"M"))
  37. {}
  38. //check functie
  39. const double Pendulum::check(const double d, const string str) const
  40. {
  41.     if (d<0.)
  42.     {
  43.         cout << "ERROR: " << str << " (" << d << ") has to be positive"  << endl;
  44.         exit(EXIT_FAILURE);
  45.     }
  46.     return d;
  47. }
  48. //getfuncties
  49. double Pendulum::getM() const
  50. {
  51.     return M_;
  52. }
  53. double Pendulum::getL() const
  54. {
  55.     return L_;
  56. }
  57. double Pendulum::getTheta()
  58. {
  59.     return Theta_;
  60. }
  61. double Pendulum::getOmega()
  62. {
  63.     return Omega_;
  64. }
  65. //overloading operators
  66.  Pendulum Pendulum::operator+ (Pendulum param)
  67. {
  68. return Pendulum(L_, M_, Theta_+param.Theta_, Omega_+param.Omega_);
  69. }
  70.  
  71. Pendulum Pendulum::  operator* (const double param)
  72. {
  73. return Pendulum(L_, M_, param*Theta_, param*Omega_);
  74. }
  75.  
  76. Pendulum Pendulum::operator=(Pendulum param)
  77. {
  78. return Pendulum(L_, M_, param.Theta_, param.Omega_);
  79. }
  80. //copy constructor
  81. Pendulum::Pendulum(const Pendulum& Object)
  82. {
  83. *this=Object;
  84. }
  85.  
  86. //doublependulum.h
  87. #ifndef doublependulum_H
  88. #define doublependulum_H
  89. #include "pendulum.h"
  90.  
  91. class DoublePendulum
  92. {
  93. public:
  94.     //constructor
  95.     DoublePendulum(Pendulum ,Pendulum );   
  96.     //getfuncties
  97.     Pendulum getUp();
  98.     Pendulum getDown();
  99.     //Overload operators
  100.     DoublePendulum operator+ (DoublePendulum);
  101.     DoublePendulum operator*(const double a);
  102.     DoublePendulum &operator=(const DoublePendulum &);
  103.     //copy constructor
  104.     DoublePendulum(const DoublePendulum& );
  105. private:
  106.     Pendulum PendUp;
  107.     Pendulum PendDown; 
  108. };
  109. #endif
  110.  
  111.  
  112. //doublependulum.cpp
  113. #include "doublependulum.h"
  114.  
  115. //constructor
  116. DoublePendulum::DoublePendulum(Pendulum Up,Pendulum Down)
  117.                :PendUp(Up),PendDown(Down)
  118. {}
  119. //getfunctions
  120. Pendulum DoublePendulum::getUp()
  121. {
  122. return PendUp;
  123. }
  124. Pendulum DoublePendulum::getDown()
  125. {
  126. return PendDown;
  127. }
  128. //Overload operators
  129. DoublePendulum DoublePendulum::operator+ (DoublePendulum param)
  130. {
  131. return DoublePendulum(PendUp + param.PendUp,PendDown + param.PendDown);
  132. }
  133.  
  134. DoublePendulum DoublePendulum::operator* (const double param)
  135. {
  136. return DoublePendulum(PendUp*param,PendDown*param);
  137. }
  138.  
  139. DoublePendulum& DoublePendulum::operator= (const DoublePendulum& param)
  140. {
  141. return *this; // assign to members of this object
  142. }
  143. //Copy constructor
  144. DoublePendulum::DoublePendulum(const DoublePendulum& Object)
  145. {
  146. *this=Object;
  147. }
  148.  
  149.  
  150. //Main.cpp
  151. #include "pendulum.h"
  152. #include "doublependulum.h"
  153. int main()
  154. {return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement