


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



2 ∗ A n u c l e u s c l a s s 3 ∗ / 4 # pragma o n c e 5 6 # include < i o s t r e a m > 7 # include < s t r i n g > 8 using namespace std ; 9 10 / ∗ ∗ 11 ∗ n u c l e u s i n t e r f a c e 12 ∗ / 13 class nucleus { 14 public : 15 / / d e f a u l t c t o r makes a H+ i o n 16 nucleus ( ) ; 17 nucleus ( int num_protons , int num_neutrons ) ; 18 / / t h e a t o m i c s y m b o l f o r t h e n u c l e u s 19 string symbol ( ) const ; 20 / / g e t f o r p r o t o n s 21 int get_protons ( ) const ; 22 / / s e t and g e t f o r n e u t r o n s 23 void set_neutrons ( int newvalue ) ; 24 int get_neutrons ( ) const ; 25 / / a new n u c l e u s bas ed on t h e c a l l i n g o b j e c t b u t w i t h num 26 / / n e u t r o n s f r o m nuc added. 27 nucleus transfer_neutr on s ( nucleus& nuc , int num ) ; 28 / / r e a d v a l u e f r o m t h e i n p u t s t r e a m −− 29 / / r e t u r n s a r e f e r e n c e t o t h e i s t r e a m a r g u m e n t p r o v i d e d 30 istream& input ( istream& is ) ; 31 / / p r i n t m y s e l f t o t h e o u t p u t s t r e a m 32 / / r e t u r n s a r e f e r e n c e t o t h e o s t r e a m a r g u m e n t p r o v i d e d 33 ostream& output ( ostream& os ) const ; 34 private : 35 / / s e t s n e u t r o n s =n and r e t u r n s t r u e i f n >= 36 bool alter_neutrons ( int n ) ; 37 / / d a t a members 38 int protons ; 39 int neutrons ; 40 } ;
2 ∗ A n u c l e u s c l a s s 3 ∗ / 4 5 # include < i o s t r e a m > / / s y s t e m h e a d e r s f i r s t 6 7 # include " nucleus. h " / / d e v e l o p e r h e a d e r s s ec ond 8 # include " atomic_symbol. h " / / a t o m i c num − > s y m b o l f u n c t i o n 9 10 using namespace std ; 11 12 / ∗ ∗ 13 ∗ n u c l e u s i m p l e m e n t a t i o n 14 ∗ / 15 16 / / d e f a u l t c t o r c r e a t e s H+ by d a t a member a s s i g n m e n t 17 nucleus : : nucleus ( ) 18 { 19 protons = 1 ; 20 neutrons = 0 ; 21 } 22 23 / / t h i s c t o r c r e a t e s an a r b i t r a r y n u c l e u s by 24 / / d a t a member a s s i g n m e n t 25 nucleus : : nucleus ( int num_protons , int num_neutrons ) 26 { 27 if ( num_protons <= 0 ) { 28 cout << " Error : invalid num_protons. " << endl ; 29 } else if ( neutrons < 0 ) { 30 cout << " Error : negative neutrons. " << endl ; 31 } else { 32 / / e v e r y t h i n g ok 33 protons = num_protons ; 34 neutrons = num_neutrons ; 35 return ; 36 } 37 / / we g e t h e r e on an e r r o r o n l y −− c r e a t e H+ 38 protons = 1 ; 39 neutrons = 0 ; 40 }
42 string nucleus : : symbol ( ) const 43 { 44 return atomic_symbol ( protons ) ; 45 } 46 47 int nucleus : : get_protons ( ) const 48 { 49 return protons ; 50 } 51 52 int nucleus : : get_neutrons ( ) const 53 { 54 return neutrons ; 55 } 56 57 void nucleus : : set_neutrons ( int newvalue ) 58 { 59 if (! alter_neutrons ( newvalue ) ) { 60 / / ERROR! 61 cout << " Oops! Negative neutrons! " ; 62 cout << endl ; 63 } 64 } 65 66 / / c opy o f c a l l i n g o b j e c t + num n e u t r o n s f r o m nuc 67 nucleus nucleus : : transfer_neutr on s ( nucleus& nuc , 68 int num ) 69 { 70 if ( num < 0 ) { 71 cout << " Negative neutron transfer! " << endl ; 72 } else if ( nuc. alter_neutrons ( nuc. neutrons −num ) ) { 73 / / i f t h e y e x i s t r e t u r n a new n u c l e u s 74 return nucleus ( protons , neutrons + num ) ; 75 } else { 76 / / ERROR! 77 cout << " Not enough neutrons! " << endl ; 78 } 79 80 / / r e t u r n H y drogen on e r r o r 81 return nucleus ( ) ; 82 } 83 84 / / h e l p e r f u n c t i o n − s e t t h e number o f n e u t r o n s 85 bool nucleus : : alter_neutrons ( int n ) 86 { 87 if ( n >= 0 ) { 88 neutrons = n ; 89 return true ; 90 } 91 return false ; 92 } 93 94 ostream& nucleus : : output ( ostream& os ) const 95 { 96 / / Do n o t p r i n t an e n d l o r ’ \ n ’! 97 os << symbol ( ) << " " << protons << " " << neutrons ; 98 return os ; 99 } 100 101 istream& nucleus : : input ( istream& is ) 102 { 103 int p , n ; 104 string s ; 105 106 / / f o r m a t i s SYMBOL #p # n 107 if ( is >> s >> p >> n ) { 108 / / i f t h e i n p u t w ork ed , AND t h e v a l u e s a r e v a l i d 109 if ( p > 0 && n >= 0 ) { 110 protons = p ; 111 neutrons = n ; 112 } else { 113 cout << " Invalid number of protons or neutrons! " ; 114 cout << endl ; 115 / / s e t t h e i n p u t s t r e a m t o a f a i l u r e s t a t e! 116 is. setstate ( ios : : failbit ) ; 117 } 118 } 119 / / r e t u r n s i s w i t h f a i l u r e s t a t e i n t a c t 120 return is ; 121 }
63 int main ( ) 64 { 65 / / f i l e name 66 const string hetest ( " HeliumTest. txt " ) ; 67 ofstream outfile ( hetest. c_str ( ) ) ; 68 if (! outfile ) { 69 cout << " !?! Cannot write " << hetest << endl ; 70 exit ( 1 ) ; 71 } 72 73 / / d e f a u l t c t o r y i e l d s H 74 nucleus Hydrogen ; 75 nucleus Helium ( 2 , 2 ) ; 76 77 cout << " Here is a nucleus : " ; 78 Hydrogen. output ( cout ) ; 79 cout << endl ; 80 81 cout << " And here is another : " ; 82 Helium. output ( cout ) << endl ; 83 84 / / c hange h y d r o g e n n e u t r o n s 85 Hydrogen. set_neutrons ( 2 ) ; 86 cout << " Set Hydrogen with 2 neutrons : " ; 87 Hydrogen. output ( cout ) << endl ; 88 89 test_neutron_t ra n sf er ( Helium , Hydrogen ) ; 90 91 labeled_nucle us _o ut pu t ( " New Hydrogen : " , Hydrogen , outfile ) ; 92 labeled_nucle us _o ut pu t ( " New Helium : " , Helium , outfile ) ; 93 94 / / c l o s e o u t p u t f i l e 95 outfile. close ( ) ; 96 97 / / t e s t r e a d i n g f r o m t h e n e w l y c r e a t e d o u t p u t f i l e 98 test_nucleus_in pu t ( hetest ) ; 99 100 / / t e s t " a d d i n g " t o g e t h e r t w o n u c l e i 101 Hydrogen. output ( cout ) << " + " ; 102 Helium. output ( cout ) << " -> " ; 103 add_nuclei ( Hydrogen , Helium ). output ( cout ) << endl ; 104 105 return 0 ; 106 }