Documente Academic
Documente Profesional
Documente Cultură
Provide method definitions for the class described in Review Question 5 and write a
short program that illustrates all the features.
My answer:
bankaccount.h
1 #ifndef BANKACCOUNT_H_
2 #define BANKACCOUNT_H_
3
4 #include <iostream>
5 #include <string>
6
7 class BankAccount
8 {
9 private:
10 std::string DepositorName;
11 std::string AccountNumber;
12 double Balance;
13public:
14 BankAccount();
15 BankAccount(std::string depname, std::string acnum, double bal);
16 ~BankAccount();
17 void ShowInfo() const;
18 void Withdraw(double amount);
19 void Deposit(double depamount);
20};
21
22#endif
bankaccount.cpp
1 #include "bankaccount.h";
2
3 BankAccount::BankAccount()
4 {
AccountNumber = "00000000";
Balance = 0;
8 }
9
10BankAccount::BankAccount(std::string depname, std::string acnum, double bal)
11{
12 DepositorName = depname;
13 AccountNumber = acnum;
14 Balance = bal;
15}
16
17BankAccount::~BankAccount()
18{
19}
20
21void BankAccount::ShowInfo() const
22{
23 using namespace std;
24 cout.precision(2);
25 cout.setf(ios_base::fixed, ios_base::floatfield);
26 cout.setf(ios_base::showpoint);
27 cout << "\nDepositor's name: " << DepositorName;
28 cout << "\nAccount number: " << AccountNumber;
29 cout << "\nBalance: $" << Balance;
30}
31
32void BankAccount::Withdraw(double amount)
33{
34 if (amount < 0)
35
std::cout << "\nPlease enter positive number for the amount to withdraw.";
36 else
37
Balance -= amount;
38}
39
40void BankAccount::Deposit(double depamount)
41{
42 if (depamount < 0)
43
std::cout << "\nPlease enter positive number for the amount to diposit.";
44 else
45
Balance += depamount;
46}
cp10ex1.cpp
1 #include <iostream>
2 #include "bankaccount.h"
3
4 int main()
5 {
6
7
8
Alex.ShowInfo();
10
11 BankAccount Empty;
12 Empty.ShowInfo();
13
14 Alex.Withdraw(15000.13);
15 Alex.ShowInfo();
16 Alex.Deposit(7500.25);
17 Alex.ShowInfo();
18
19 Empty.Deposit(5000.45);
20 Empty.ShowInfo();
21
22 cin.get();
23 cin.get();
24 return 0;
25}
5 #include <iostream>
6
7 class Person {
8
private:
10
11
12 public:
13
14
15
~Person();
16
17
18
19};
20
21#endif
person.cpp
1 #include "person.h"
2
3 Person::Person(const std::string & ln, const char * fn)
4 {
5
lname = ln;
strcpy(fname,fn);
7 }
8
9 Person::~Person()
10{
11}
12
13void Person::Show() const
14{
15 std::cout << "\n" << fname << " " << lname;
16}
17
18void Person::FormalShow() const
19{
20 std::cout << "\n" << lname << ", " << fname;
21}
cp10ex2.cpp
1 #include "person.h"
2 int main()
3 {
4
5
6
one.Show();
1.
Daniel says:
April 2, 2013 at 6:49 am
Why it generated the error LNK2019?
3. Do Programming Exercise 1 from Chapter 9, but replace the code shown there with an
appropriate golf class declaration. Replace setgolf(golf &, const char *, int)
with a constructor with the appropriate argument for providing initial values. Retain the
interactive version of setgolf(), but implement it by using the constructor. (For example,
for the code for setgolf(), obtain the data, pass the data to the constructor to create
a temporary object, and assign the temporary object to the invoking object, which is
*this.)
My answer:
golf.h
1 #ifndef GOLF_H_
2 #define GOLF_H_
3
4 #include <iostream>
5
6 class Golf
7 {
8 private:
9
10 char fullname[Len];
11 int handicap;
12 int setgolf(Golf & g);
13public:
14 // non-interactive version:
15 // function sets golf structure to provided name, handicap
16 // using values passed as arguments to the function
17 Golf(const char * name, int hc);
18 // interactive version:
strcpy(fullname,name);
handicap = hc;
10}
11// interactive version:
12// function solicits name and handicap from user
13// and sets the members of g to the values entered
14// returns 1 if name is entered, 0 if name is empty string
15Golf::Golf()
16{
17 setgolf(*this);
18}
19
return 0;
41
42 cout << "\nEnter handicap number: ";
43 cin >> g.handicap;
44 return 1;
45}
cp10ex3.cpp
1 #include <iostream>
2 #include "golf.h"
3
4 int main()
5 {
6
7
8
9
Golf ann;
10 ann.Showgolf();
11 ann.Resethandicap(50);
12 ann.Showgolf();
13
14 Golf alex("Alex Ignatkov",23);
15 alex.Showgolf();
16 alex.Resethandicap(17);
17 alex.Showgolf();
18
19 cin.get();
20 cin.get();
21 return 0;
22}
4. Do Programming Exercise 4 from Chapter 9, but convert the Sales structure and its
associated functions to a class and its methods. Replace the setSales(Sales &, double
[], int) function with a constructor. Implement the interactive setSales(Sales &)
method by using the constructor. Keep the class within the namespace SALES.
My answer:
sales.h
1 #ifndef SALES_H_
2 #define SALES_H_
3
4 namespace SALES
5 {
6
class Sales
private:
10
double sales[QUARTERS];
11
double average;
12
double max;
13
double min;
14 public:
15
16
17
18
19
20
21
22
23
Sales();
24
~Sales();
25
26
27 };
28}
29
30#endif
sales.cpp
1 #include <iostream>
2 #include "sales.h"
3
4 namespace SALES
5 {
6
using std::cout;
using std::cin;
using std::endl;
16
double total = 0;
17
18
19
20
21
sales[i] = ar[i];
22
23
min = ar[i];
24
25
max = ar[i];
26
total += ar[i];
27
28
29
30
31
32 }
33// gathers sales for 4 quarters interactively, stores them
34// in the sales member of s and computes and stores the
35// average, maximum, and minumum values
36 Sales::Sales()
37 {
38
double qt,total = 0;
39
40
41
42
43
cout << "Enter sales for quarter #" << i+1 << ": ";
44
45
sales[i] = qt;
46
total += qt;
47
48
49
50
51
52
53
min = sales[i];
54
55
56
max = sales[i];
}
57
58
59
60 }
61
62// display all information in structure s
63 void Sales::showSales() const
64 {
65
66
67
cout << "\nSales Quarter #" << i+1 << ": " << sales[i];
68
69
70
71 }
72
73 Sales::~Sales()
74 {
75 }
76
77}
cp10ex4.cpp
1 #include <iostream>
2 #include "sales.h"
3
4 int main()
5 {
6
SALES::Sales s1;
SALES::Sales s2(ar,3);
9
10 s1.showSales();
11 s2.showSales();
12
13 std::cin.get();
14 std::cin.get();
15 return 0;
16}
be added to a running total, and the running total should be reported. Note: You should
be able to use the Stack class unaltered; just change the typedef declaration so that
Item is type customer instead of unsigned long.
My answer:
stack.h
1 // stack.h -- class definition for the stack ADT
2 #ifndef STACK_H_
3 #define STACK_H_
4
5 struct customer {
6 char fullname[35];
7 double payment;
8 };
9
10typedef customer Item;
11
12class Stack
13{
14 private:
15
16
17
18 public:
19
Stack();
20
21
22
23
24
25
26};
27#endif
stack.cpp
1 // stack.cpp -- Stack member functions
2 #include "stack.h"
3
4 Stack::Stack() // create an empty stack
5 {
6
top = 0;
7 }
8 bool Stack::isempty() const
9 {
10 return top == 0;
11}
12bool Stack::isfull() const
13{
14 return top == MAX;
15}
16bool Stack::push(const Item & item)
17{
18 if (top < MAX)
19 {
20
items[top++] = item;
21
return true;
22 }
23 else
24
return false;
25}
26bool Stack::pop(Item & item)
27{
28 if (top > 0)
29 {
30
item = items[--top];
31
return true;
32 }
33 else
34
return false;
35}
cp10ex5.cpp
1 #include <iostream>
2 #include <cctype> // or ctype.h
3 #include "stack.h"
4
5 int main()
6 {
7
8
9
10 char ch;
11 customer po;
12 double runtotal = 0;
13
14 cout << "Please enter A to add a customer,\n"
15
19
continue;
20
if (!isalpha(ch))
21
22
23
continue;
24
25
switch(ch)
26
27
case 'A':
28
29
cin.getline(po.fullname,35);
30
cout << "Enter a payment for " << po.fullname << " : ";
31
32
if (st.isfull())
33
34
else
35
st.push(po);
36
break;
37
case 'R':
38
39
40
else
41
42
st.pop(po);
43
runtotal += po.payment;
44
removed\n";
45
cout << "Customer " << po.fullname << " with payment of " << po.payment << " is
}
46
break;
47
}
48
49
50
51
52
53
54
55
56 cin.get();
57 cin.get();
58 return 0;
}
private:
double x;
double y;
public:
10
11
12
13
14
15
16
17};
18
19#endif
move.cpp
1 #include "move.h"
2 #include <iostream>
3
4 Move::Move(double a, double b)
5 {
6
x = a;
y = b;
8 }
9
10void Move::showmove() const
11{
12 std::cout << "x = " << x << ", y = " << y;
13}
14
15Move Move::add(const Move &m) const
16{
17 Move temp;
18 temp.x = x + m.x;
19 temp.y = y + m.y;
20
21 return temp;
22}
23
24void Move::reset(double a, double b)
25{
26 x = a;
27 y = b;
28}
cp10ex6.cpp
1 #include <iostream>
2 #include "move.h"
3
4 int main()
5 {
6
7
Move obj1(5,6);
Move obj2(10,11);
Move obj3;
10
11 obj1 = obj1.add(obj2);
12 std::cout << "obj1: ";
13 obj1.showmove();
14
15 obj2 = obj2.add(obj1);
16 std::cout << "\nobj2: ";
17 obj2.showmove();
18
19 obj3 = obj1.add(obj2);
20 std::cout << "\nobj3: ";
21 obj3.showmove();
22
23 std::cin.get();
24 std::cin.get();
25 return 0;
26}
char fullname[MAX];
int CI;
10public:
11 Plorg();
12 Plorg(const char * fn, int CIvalue);
13 void Show() const;
14 void ChangeCI(int newCI) { CI = newCI;}
15};
16
17#endif
plorg.cpp
1 #include <iostream>
2 #include "plorg.h"
3
4 Plorg::Plorg()
5 {
6
CI = 50;
strcpy(fullname,"Plorga");
8 }
9
10Plorg::Plorg(const char *fn, int CIvalue)
11{
12 strcpy(fullname,fn);
13 CI = CIvalue;
14}
15
16void Plorg::Show() const
17{
18 std::cout << "\nName is " << fullname;
19 std::cout << "\nCI is " << CI;
20}
cp10ex7.cpp
1 #include <iostream>
2 #include "plorg.h"
3
4 int main()
5 {
6
Plorg Sample;
Sample.Show();
Plorg Sample2("Sample2",20);
10 Sample2.Show();
11
12 Sample.ChangeCI(30);
13 Sample.Show();
14
15 Sample2.ChangeCI(100);
16 Sample2.Show();
17
18 std::cin.get();
19 std::cin.get();
20 return 0;
21}
1 #ifndef LIST_H_
2 #define LIST_H_
3
4 #include <iostream>
5 #include <string>
6
7 struct computergame
8 {
9
std::string gamename;
10 double price;
11};
12
13typedef computergame Item;
14
15class List
16{
17private:
18 enum {MAX = 10};
19 Item list[MAX];
20 int itemcount;
21public:
22 List();
23 bool isempty() const;
24 bool isfull() const;
25 int count() const;
26 bool add(const Item & item);
27 void priceadjust(void (*pf)(Item &));
28 void showlist();
29};
30
31#endif
list.cpp
1 #include "list.h"
2
3 List::List() // create an empty stack
4 {
5
itemcount = 0;
6 }
7 bool List::isempty() const
8 {
9
return itemcount == 0;
10}
11bool List::isfull() const
12{
13 return itemcount == MAX;
14}
15bool List::add(const Item & item)
16{
17 if (itemcount < MAX)
18 {
19
list[itemcount++] = item;
20
return true;
21 }
22 else
23
return false;
24}
25int List::count() const
26{
27 return itemcount;
28}
29
30void List::priceadjust(void (*pf)(Item &))
31{
32 for (int i = 0; i < itemcount; i++)
33
pf(list[i]);
34}
35
36void List::showlist()
37{
38 using namespace std;
39 cout.precision(2);
40 cout.setf(ios_base::fixed, ios_base::floatfield);
41 cout.setf(ios_base::showpoint);
42
43 for (int i = 0; i < itemcount; i++)
44
std::cout << "\nGame #" << i+1 << ": " << list[i].gamename << ", price - $" << list[i].price << ".";
45}
cp10ex8.cpp
1 #include "list.h"
2
3 inline void inflatrate(Item & gm) { gm.price *= 1.15;}
4
5 int main()
6 {
7
8
9
10 List l1;
11 Item mygame;
12
13 cout << "Please enter computer games you own with price (Q to quit) :\n";
14 cout << "Name of game: ";
15 while(!l1.isfull() && getline(cin,mygame.gamename) && mygame.gamename[0] != '\0')
16 {
17
18
break;
19
20
21
22
23
l1.add(mygame);
24
25
26 }
27
28 cout << "\nItems entered: " << l1.count();
29
30 if (l1.isempty())
31
32 else
33 {
34
l1.showlist();
35
36
l1.priceadjust(inflatrate);
37
l1.showlist();
38 }
39
40 std::cin.get();
41 std::cin.get();
42 return 0;
43}