Anda di halaman 1dari 6

Chapter 8 Solutions

1. Before giving the code, it is important to mention that arrays in C++ arent assignable. Thats way I will show
two equivalent implementations.
1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8
9
10
11
12
13

s t r u c t Address {
c o n s t char * name ;
i n t number ;
c o n s t char * street ;
c o n s t char * town ;
char state [ 2 ] ;
c o n s t char * zip ;
};

14
15
16
17
18
19
20
21
22

v o i d print ( c o n s t Address& A ) {
cout << "Name : " << A . name << endl ;
cout << " Number : " << A . number << endl ;
cout << " S t r e e t : " << A . street << endl ;
cout << " Town : " << A . town << endl ;
cout << " S t a t e : " << A . state << endl ;
cout << " Z i p : " << A . zip << endl ;
}

23
24

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

25

/ * * * Implementation 1 * * * /
Address address1 ;
address1 . name = " Jim Dandy " ;
address1 . number = 6 1 ;
address1 . street = " South St " ;
address1 . town = "New Providence " ;
strcpy ( address1 . state , ( " NJ " ) ) ;
address1 . zip = " 07974 " ;

26
27
28
29
30
31
32
33
34

/ * * * Implementation 2 * * * /
Address address2 = {
" Jim Dandy " ,
61 , " South St " ,
"New Providence " ,
{ 'N ' , ' J ' } , " 07974 "
};

35
36
37
38
39
40
41
42

print ( address1 ) ;
print ( address2 ) ;

43
44
45

main.cpp
Implementation 2 requires that user provides the struct definition in order. If user provides town first and
street second, no error will be generated. However the object Address is wrong.

2. struct with pointer


1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8
9
10
11
12
13

s t r u c t Address {
c o n s t char * name ;
i n t number ;
c o n s t char * street ;
c o n s t char * town ;
char state [ 2 ] ;
c o n s t char * zip ;
};

14
15
16
17
18
19
20
21
22

v o i d print ( c o n s t Address& A ) {
cout << "Name : " << A . name << endl ;
cout << " Number : " << A . number << endl ;
cout << " S t r e e t : " << A . street << endl ;
cout << " Town : " << A . town << endl ;
cout << " S t a t e : " << A . state << endl ;
cout << " Z i p : " << A . zip << endl ;
}

23
24

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

25

/ * * * Implementation 1 * * * /
Address * address1 { new Address } ;
address1>name = " Jim Dandy " ;
address1>number = 6 1 ;
address1>street = " South St " ;
address1>town = "New Providence " ;
strcpy ( address1>state , ( " NJ " ) ) ;
address1>zip = " 07974 " ;

26
27
28
29
30
31
32
33
34

/ * * * Implementation 2 * * * /
Address * address2 { new Address } ;
* address2 = {
" Jim Dandy " ,
61 , " South St " ,
"New Providence " ,
{ 'N ' , ' J ' } , " 07974 "
};

35
36
37
38
39
40
41
42
43

print ( * address1 ) ;
print ( * address2 ) ;

44
45
46

d e l e t e address1 ;
d e l e t e address2 ;

47
48
49

main.cpp
3. Constructor and destructor allow to initialize PROPERLY and object in C++.

4, 5 Person struct.
1

# i n c l u d e < iostream >

2
3

u s i n g std : : cout ; u s i n g std : : endl ;

4
5
6
7
8
9

s t r u c t Person {
i n t age ;
char sex ;
std : : string name , last_name ;
};

10
11
12
13
14
15
16

v o i d print ( c o n s t Person& A ) {
cout << "Name : " << A . name << endl ;
cout << " L a s t Name : " << A . last_name << endl ;
cout << " Age : " << A . age << endl ;
cout << " Sex : " << A . sex << endl ;
}

17
18

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

19

Person client ;
client . name = " M a u r i c i o " ;
client . last_name = " Bedoya " ;
client . sex = 'H ' ;
client . age = 3 2 ;

20
21
22
23
24
25

print ( client ) ;

26
27

main.cpp
6 Person pointer
1

# i n c l u d e < iostream >

2
3

u s i n g std : : cout ; u s i n g std : : endl ;

4
5
6
7
8
9

s t r u c t Person {
i n t age ;
char sex ;
std : : string name , last_name ;
};

10
11
12
13
14
15
16

v o i d print ( c o n s t Person& A ) {
cout << "Name : " << A . name << endl ;
cout << " L a s t Name : " << A . last_name << endl ;
cout << " Age : " << A . age << endl ;
cout << " Sex : " << A . sex << endl ;
}

17
18
19
20
21
22
23

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


Person * client { new Person } ;
client>name = " M a u r i c i o " ;
client>last_name = " Bedoya " ;
client>sex = 'H ' ;
client>age = 3 2 ;

24
25
26

print ( * client ) ;
d e l e t e client ;

27

main.cpp
7 Not implemented.
8 The UML with the constructor is:
Person
age: int
sex: char
name: string
last name: string
Person(string,string,int,char):Person

Figure 1: Person class with constructor

# i n c l u d e < iostream >

2
3

u s i n g std : : cout ; u s i n g std : : endl ;

4
5
6
7
8
9

s t r u c t Person {
i n t age_ ;
char sex_ ;
std : : string name_ , last_name_ ;
Person ( c o n s t std : : string& name , c o n s t std : : string& lastname , c o n s t char& sex , c o n s t i n t & age ) : name_ ( name ) , last_name_ ( lastname ) , sex_ ( sex ) , age_ ( age ) { }

10
11

};

12
13
14
15
16
17
18

v o i d print ( c o n s t Person& A ) {
cout << "Name : " << A . name_ << endl ;
cout << " L a s t Name : " << A . last_name_ << endl ;
cout << " Age : " << A . age_ << endl ;
cout << " Sex : " << A . sex_ << endl ;
}

19
20

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

21

Person * client { new Person ( " M a u r i c i o " , " Bedoya " , 'H ' , 3 2 ) } ;

22
23

print ( * client ) ;
d e l e t e client ;

24
25
26

main.cpp
9 Even with the destructor use delete. For each new there must be a delete. Class Person with destructor
not implemented.
10 Rectangle should be implemented in two different files: .h and .cpp. The next code is implemented in
main.cpp. Reader should create the previous two files and compare result. At the same time, this implementation is tricky. Because two members are defined constant, they must be initialized at compile time.

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8
9
10
11
12

s t r u c t Rectangle {
c o n s t double base ;
c o n s t double height ;
double area ( ) {
r e t u r n ( base * height ) ;
}
};

13
14
15
16

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


Rectangle R = { 1 0 , 2 } ;
cout << "R area1 i s : " << R . area ( ) << endl ;

17

Rectangle R1 { } ; / / base and h e i g h t are zero .


cout << "R1 area1 i s : " << R1 . area ( ) << endl ;

18
19
20
21
22

//
}

Rectangle R2 ;

/ / E r r o r here base and heght aren ' t i n i t i a l i z e d .

main.cpp
11 Employee struct should be implemented in two different files: .h and .cpp. The next code is implemented
in main.cpp. Reader should create the previous two files and compare result.
1

# i n c l u d e < iostream >

2
3
4
5

u s i n g std : : cerr ;
u s i n g std : : cout ;
u s i n g std : : endl ;

6
7
8
9
10
11
12
13

b o o l isNegative ( c o n s t double& data ) {


i f ( data < 0 ) {
cerr << " Negative data p r o v i d e d . " << endl ;
return 0;
}
return 1;
}

14
15
16
17
18
19
20

21
22
23

s t r u c t Employee {
std : : string name ;
i n t age ;
double hourly_salary ;
v o i d Monthly_Salary ( ) {
double Salary { ( ( isNegative ( hourly_salary ) * isNegative ( age ) * hourly_salary * 8 * 20)) };
cout << " Monthly s a l a r y f o r " << name << " i s : " << ( unsigned ) ( Salary ) << endl ;
}
};

24
25
26
27
28

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


Employee E1 { " M a u r i c i o " , 32 , 1 2 3 } ;
E1 . Monthly_Salary ( ) ;
}

main.cpp

12 Person Struct.
It would be better to use plain enum rather than enum class due to print function. Change from enum class
to enum and identify that no casting is need it.
1

# i n c l u d e < iostream >

2
3
4
5

u s i n g std : : cerr ;
u s i n g std : : cout ;
u s i n g std : : endl ;

6
7
8

enum c l a s s sex { male = 0 , female = 1 , gay = 2 , lesbian = 3 , transsexual = 4 } ;


enum c l a s s marital_status { single = 0 , married = 1 , divorced = 2 , widow = 3 } ;

9
10
11
12
13
14
15
16

17
18

s t r u c t Person {
std : : string name_ , last_name_ ;
i n t age_ ;
sex sex_ ;
marital_status maritalStatus_ ;
v o i d print ( ) {
cout << " Person " << last_name_ << " i s " << s t a t i c _ c a s t <std : : underlying_type<sex > : : type>(sex_ ) << " and has " << age_ << " years o l d . M a r i t a l s t a t u s o f " << name_ << " i s " << s t a t i c _ c a s t <std : : underlying_type<marital_status > : : type>(maritalStatus_ ) << endl ;
}
};

19
20
21
22
23
24
25
26
27
28

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


Person P1 = {
" Mauricio " ,
" Bedoya " ,
32 ,
sex : : male ,
marital_status : : single
};

29

Person P2 = {
" Henrry " ,
" Guerrero " ,
32 ,
sex : : male ,
marital_status : : married
};

30
31
32
33
34
35
36
37

P1 . print ( ) ;
P2 . print ( ) ;

38
39
40

main.cpp

Anda mungkin juga menyukai