Operator Overloading Handout for CSCI261: Nucleus Class Implementation - Prof. Keith E. He, Exams of Computer Science

The handout for csci261 students on operator overloading for the nucleus class. It includes the class header file (nucleus_op.h) and implementation file (nucleus_op.cxx). The class includes constructor functions, getter and setter functions for protons and neutrons, and overloaded operators for addition and subtraction of neutrons.

Typology: Exams

Pre 2010

Uploaded on 08/19/2009

koofers-user-qhb
koofers-user-qhb 🇺🇸

1

(1)

8 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI261 Operator Overloading Handout July 29, 2009
Class “Header” File:
nucleus_op.h
1/
2A nu c l e u s c l a s s
3/
4
#pragma
on ce
5
6
#include
<i os tr ea m >
7
#include
< st r i n g >
8
using namespace std
;
9
10 /
11 nu c le u s i n t e r f a c e
12 /
13
class nucleus_op
{
14
public
:
15 / / d e f a u l t c t o r ma kes a H+ i o n
16
nucleus_op
( ) ;
17
nucleus_op
(
int num_protons
,
18
int num_neutrons
) ;
19 / / g et f o r pr o t on s
20
int get_protons
( )
const
;
21 / / ge t at om i c s ym bol
22
string get_symbol
( )
const
;
23 / / s e t a nd ge t f o r n e ut r o n s
24
void set_neutrons
(
int newvalue
) ;
25
int get_neutrons
( )
const
;
26 / / rem ove a n eu tr o n f ro m a n uc le us −− re t u r n s t h e n umb er rem ov e d
27
friend int operator
(
nucleus_op
&
lhs
,
int rhs
) ;
28 / / r ea d va l u e f r om t he i n p u t st r ea m
29 / / r e t u r n s a r e f e r e n c e t o t h e is t r e a m a rg um e nt p r o v i d e d
30
friend istream
&
operator
>> (
istream
&
is
,
nucleus_op
&
rhs
) ;
31
private
:
32 / / s e t s ne u t r o ns = n an d r e t u r n s t r u e i f n >=0
33
bool alter_neutrons
(
int n
) ;
34 / / da t a memb ers
35
int protons
;
36
int neutrons
;
37 };
38
39 /
40 g l o b a l op e r a t o r s
41 /
42 / / add a n e ut r on t o a n uc le u s −− re t u r n s a new n u cl e u s !
43
nucleus_op operator
+(
nucleus_op
&
lhs
,
int rhs
) ;
44 / / p r i n t my s e l f t o th e o u t p u t st r e am
45 / / 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 ea m ar gu m en t p r o vi d e d
46
ostream
&
operator
<< (
ostream
&
os
,
const nucleus_op
&
rhs
) ;
Class Implementation File:
nucleus_op.cxx
1/
2A nu c l e u s c l a s s
3/
4
5
#include
<i os tr ea m > / / sy st em he ad er s f i r s t
6
7
#include "atomic_symbol .h"
8
#include "nucleus_op .h"
/ / d ev el o pe r he ad er s se co nd
9
10
using namespace std
;
11
12 /
13 nu c l e u s im p l e m e n t a t i o n
14 /
15
16 / / d e fa u l t c to r c re a te s H+ w i th member i n i t ia l i z a t i o n l i s t s
17
nucleus_op
: :
nucleus_op
( )
18 :
protons
(1) ,
neutrons
(0)
19 {
20
protons
= 1;
21
neutrons
= 0;
22 }
1
pf3
pf4

Partial preview of the text

Download Operator Overloading Handout for CSCI261: Nucleus Class Implementation - Prof. Keith E. He and more Exams Computer Science in PDF only on Docsity!

Class “Header” File: nucleus_op.h

2A 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 / ∗ ∗ 11n u c l e u s i n t e r f a c e 12/ 13 class nucleus_op { 14 public : 15 / / d e f a u l t c t o r makes a H+ i o n 16 nucleus_op ( ) ; 17 nucleus_op ( int num_protons , 18 int num_neutrons ) ; 19 / / g e t f o r p r o t o n s 20 int get_protons ( ) const ; 21 / / g e t a t o m i c s y m b o l 22 string get_symbol ( ) const ; 23 / / s e t and g e t f o r n e u t r o n s 24 void set_neutrons ( int newvalue ) ; 25 int get_neutrons ( ) const ; 26 / / remove a n e u t r o n f r o m a n u c l e u s −− r e t u r n s t h e number rem ov ed 27 friend int operator −( nucleus_op& lhs , int rhs ) ; 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 friend istream& operator > >( istream& is , nucleus_op& rhs ) ; 31 private : 32 / / 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 >= 33 bool alter_neutrons ( int n ) ; 34 / / d a t a members 35 int protons ; 36 int neutrons ; 37 } ; 38 39 / ∗ ∗ 40g l o b a l o p e r a t o r s 41/ 42 / / add a n e u t r o n t o a n u c l e u s −− r e t u r n s a new n u c l e u s! 43 nucleus_op operator + ( nucleus_op& lhs , int rhs ) ; 44 / / 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 45 / / 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 46 ostream& operator < <( ostream& os , const nucleus_op& rhs ) ;

Class Implementation File: nucleus_op.cxx

2A 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 " atomic_symbol. h " 8 # include " nucleus_op. h" / / d e v e l o p e r h e a d e r s s ec ond 9 10 using namespace std ; 11 12 / ∗ ∗ 13n 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+ w i t h member i n i t i a l i z a t i o n l i s t s 17 nucleus_op : : nucleus_op ( ) 18 : protons ( 1 ) , neutrons ( 0 ) 19 { 20 protons = 1 ; 21 neutrons = 0 ; 22 }

Class Implementation File: nucleus_op.cxx continued...

25 nucleus_op : : nucleus_op ( 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 } 41 42 int nucleus_op : : get_protons ( ) const 43 { 44 return protons ; 45 } 46 47 string nucleus_op : : get_symbol ( ) const 48 { 49 return atomic_symbol ( protons ) ; 50 } 51 52 int nucleus_op : : get_neutrons ( ) const 53 { 54 return neutrons ; 55 } 56 57 void nucleus_op : : set_neutrons ( int newvalue ) 58 { 59 if (! alter_neutrons ( newvalue ) ) { 60 / / ERROR! 61 cout << " Oops! Negative neutrons! " ; 62 cout << endl ; 63 } 64 } 65 66 / / h e l p e r f u n c t i o ns e t t h e number o f n e u t r o n s 67 bool nucleus_op : : alter_neutrons ( int n ) 68 { 69 if ( n >= 0 ) { 70 neutrons = n ; 71 return true ; 72 } 73 return false ; 74 } 75 76 77 int operator −( nucleus_op& lhs , int rhs ) 78 { 79 if ( rhs < 0 ) { 80 cout << " Negative neutrons subtracted !" << endl ; 81 } else if ( lhs. alter_neutrons ( lhs. neutrons −rhs ) ) { 82 / / we rem ov ed them , so r e t u r n t h e number 83 return rhs ; 84 } else { 85 / / ERROR! 86 cout << " Not enough neutrons! " << endl ; 87 } 88 89 / / none t r a n s f e r r e d , r e t u r n 0 90 return 0 ; 91 } 92 93 nucleus_op operator + ( nucleus_op& lhs , int rhs ) 94 { 95 / / c a l c new v a l u e 96 int newneutrons = lhs. get_neutrons ( ) + rhs ; 97 98 / / r e t u r n new o b j e c t 99 return nucleus_op ( lhs. get_protons ( ) , newneutrons ) ; 100 }

Driver “main”: nucleus_op_main.cxx continued...

36 void test_neutron _t ra ns fe r ( nucleus_op& n1 , 37 nucleus_op& n2 ) 38 { 39 int number ; 40 cout << " Neutrons to transfer from " << 41 n1 << " to " << n2 << " : " << flush ; 42 cin >> number ; 43 / / rem ov es number n e u t r o n s f r o m n1 , 44 / / adds them t o n2 , and r e t u r n s a new 45 / / n u c l e u s t h a t i s s av ed o v e r n 46 n2 = n2 + ( n1 − number ) ; 47 cout << endl ; / / p r e t t i e r c o n s o l e o u t p u t 48 } 49 / / c om bi ne t w o n u c l e i −−− NOTE : t h e m ai n r o u t i n e c an w r i t e 50 / / an o p e r a t o r f o r a n o t h e r c l a s s! AS LONG AS THE WORK 51 / / CAN BE DONE THROUGH THE PUBLIC INTERFACE! 52 nucleus_op operator + ( const nucleus_op& lhs , const nucleus_op& rhs ) 53 { 54 return nucleus_op ( lhs. get_protons ( ) + rhs. get_protons ( ) , 55 lhs. get_neutrons ( ) + rhs. get_neutrons ( ) ) ; 56 } 57 58 int main ( ) 59 { 60 / / f i l e name 61 const string hetest ( " HeliumTest. txt " ) ; 62 ofstream outfile ( hetest. c_str ( ) ) ; 63 if (! outfile ) { 64 cout << " !?! Cannot write " << hetest << endl ; 65 exit ( 1 ) ; 66 } 67 68 / / d e f a u l t c t o r y i e l d s H 69 nucleus_op Hydrogen ; 70 nucleus_op Helium ( 2 , 2 ) ; 71 72 test_nucleus_ou tp ut ( Hydrogen , Helium , outfile ) ; 73 74 / / c hange h y d r o g e n n e u t r o n s 75 Hydrogen. set_neutrons ( 2 ) ; 76 cout << " Set Hydrogen with 2 neutrons : " << 77 Hydrogen << endl ; 78 79 test_neutron_t ra n sf er ( Helium , Hydrogen ) ; 80 81 test_nucleus_ou tp ut ( Hydrogen , Helium , outfile ) ; 82 83 / / c l o s e o u t p u t f i l e 84 outfile. close ( ) ; 85 86 / / 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 87 test_nucleus_in pu t ( hetest ) ; 88 89 / / 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 90 cout << Hydrogen << " + " << Helium << " -> " << 91 Hydrogen + Helium << endl ; 92 93 return 0 ; 94 }

Example Driver Console Output (1 neutron transferred)

Here is nucleus 1: H 1 0

Here is nucleus 2: He 2 2

nucleus 2 has been written to output file.

Set Hydrogen with 2 neutrons: H 1 2

Neutrons to transfer from He 2 2 to H 1 2:

Here is nucleus 1: H 1 3

Here is nucleus 2: He 2 1

nucleus 2 has been written to output file.

First nucleus is He 2 2

Second nucleus is He 2 1

H 1 3 + He 2 1 -> Li 3 4

HeliumTest.txt Output File

He 2 2

He 2 1