nex036ara

klasa_otpornik

Nov 27th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. //otpornik.hpp
  2.  
  3.  
  4. #ifndef OTPORNIK_DEF
  5. #define OTPORNIK_DEF
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class Otpornik{
  10.     private:
  11.         double r;
  12.     public:
  13.         Otpornik();
  14.         Otpornik(double);
  15.         Otpornik(const Otpornik&);
  16.         void setR(double);
  17.         double getR()const;
  18.        Otpornik& operator=(const Otpornik&);
  19.        friend Otpornik operator+(const Otpornik&, const Otpornik&);
  20.        friend Otpornik operator||(const Otpornik&, const Otpornik&);
  21.        friend ostream& operator<<(ostream&, const Otpornik&);
  22.        friend istream& operator>>(istream&, Otpornik&);
  23.  
  24.  
  25.  
  26. };
  27. #endif
  28.  
  29. //otpornik.cpp
  30.  
  31.  
  32.  
  33. #include "otpornik.hpp"
  34.  
  35. Otpornik:: Otpornik() {r = 0;}
  36. Otpornik:: Otpornik(double rr) {r = rr;}
  37. Otpornik:: Otpornik(const Otpornik &x){r = x.r;}
  38. void Otpornik:: setR(double rr){r = rr;}
  39. double Otpornik::getR()const{return r;}
  40. Otpornik& Otpornik::operator=(const Otpornik &x){r = x.r;}
  41.  
  42. Otpornik operator+(const Otpornik &x, const Otpornik &y){
  43.     Otpornik z( x.r + y.r);
  44.     return z;
  45.     }
  46. Otpornik operator||(const Otpornik &x, const Otpornik&y){
  47.     Otpornik z((x.r * y.r)/ (x.r+y.r)) ;
  48.     return z;
  49. }
  50. ostream& operator<<(ostream &out, const Otpornik &x){
  51.     out<<x.r;
  52.     return out;
  53. }
  54.  
  55. istream& operator>>(istream &in, Otpornik &x){
  56.     in>>x.r;
  57.     return in;
  58. }
  59.  
  60. //main.cpp
  61.  
  62.  
  63. #include "otpornik.hpp"
  64.  
  65. int main(){
  66.  
  67.     Otpornik r1,r2;
  68.     cout<<"r1,r2?"<<endl;
  69.     cin>>r1;
  70.     cin>>r2;
  71.  
  72.     cout<<"serijska veza:"<<(r1+r2)<<endl;
  73.     cout<<"paralelna veza:"<<(r1||r2)<<endl;
  74.     r1=r2;
  75.     cout<<r1<<"="<<r2;
  76.  
  77.     return 0;
  78.     }
  79.  
  80.  
Add Comment
Please, Sign In to add comment