编程练习1
//account.h
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <string>
using std::string;
class Account
{
private:
string fullname;
string ID;
double deposit;
public:
Account(string, string, double);
~Account();
void show();
void add(double);
void withdraw(double);
};
#endif
//account.cpp
#include "account.h"
#include <iostream>
Account::Account(string sname, string sID, double de)
{
fullname = sname;
ID = sID;
deposit = de;
}
Account::~Account()
{
}
void Account::show()
{
using std::cout;
using std::endl;
cout << "User's name is: " << fullname << endl;
cout << "User's ID is: " << ID << endl;
cout << "User's deposit is: " << deposit << endl;
}
void Account::add(double addmoney)
{
using std::cout;
using std::endl;
deposit += addmoney;
cout << "The user has added " << addmoney << " to this account" << endl;
cout << "Now the user's deposit is " << deposit << endl;
}
void Account::withdraw(double withdrawmoney)
{
using std::cout;
using std::endl;
if (withdrawmoney <= deposit)
{
deposit -= withdrawmoney;
cout << "The user has withdrawn " << withdrawmoney << " to this account" << endl;
cout << "Now the user's deposit is " << deposit << endl;
}
else
{
cout << "The user doesn't have enough deposit!" << endl;
}
}
//main.cpp
#include "account.h"
int main()
{
Account account1("James Bond", "JB007", 2500);
account1.show();
account1.add(372.5);
account1.withdraw(123.4);
system("pause");
return 0;
}
编程练习2
//Person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
class Person
{
private:
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person();
Person(const std::string &ln, const char *fn = "Heyyou");
~Person();
void Show() const;
void FormalShow() const;
};
#endif
//Person.cpp
#include <string>
#include <iostream>
#include "Person.h"
//Person(const std::string &ln, const char *fn = "Heyyou");
//~Person();
//void Show() const;
//void FormalShow() const;
Person::Person()
{
lname = "";
fname[0] = '\0';
}
Person::Person(const std::string &ln, const char *fn)
{
lname = ln;
int i;
for(i=0;i!=strlen(fn);i++)
fname[i]=*(fn+i);
fname[i] = '\0';
}
Person::~Person()
{
}
void Person::Show() const
{
std:: cout << "This Person's firsname: " << fname
<< ", and his last name is: " << lname <<"\n";
}
void Person::FormalShow() const
{
std:: cout << "This Person's lastname: " << lname
<< ", and his first name is: " << fname <<"\n";
}
//main.cpp
#include "Person.h"
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy","Sam");
one.Show();
one.FormalShow();
two.Show();
two.FormalShow();
three.Show();
three.FormalShow();
system("pause");
return 0;
}
编程练习3
//golf.h
#ifndef GOLF_H_
#define GOLF_H_
const int LEN = 40;
class golf
{
private:
char fullname[LEN];
int handicap;
public:
golf(char *, int);
golf();
~golf();
void sethandicap(int);
void showgolf();
};
#endif
//golf.cpp
#include "golf.h"
#include <cstring>
#include <iostream>
using namespace std;
golf::golf(char *n, int h)
{
strcpy(fullname, n);
handicap = h;
}
golf::golf()
{
cout << "using intereactive method to creat a golf object" << endl;
char name[LEN];
int h;
cout << "Please input the fullname" << endl;
if (cin.get(name, LEN))
{
cin.get();
cout << "Please enter the number of handicap (must be integer): ";
cin >> h;
cin.get();
}
golf gtem(name, h);
*this = gtem;
}
golf::~golf()
{
}
void golf::sethandicap(int h)
{
handicap = h;
}
void golf::showgolf()
{
cout << fullname << " has " << handicap <<" times of handicap" << endl;
}
//main.cpp
#include "golf.h"
#include <iostream>;
int main()
{
golf golf1("Li Lei", 10);
golf golf2;
golf1.showgolf();
golf2.showgolf();
golf1.sethandicap(5);
golf1.showgolf();
system("pause");
return 0;
}
编程练习4
//sales.h
#ifndef SALES_H_
#define SALES_H_
const int QUARTERS = 4;
class Sales
{
private:
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales(double *s);
~Sales(void);
void setSales();
void showSales();
};
#endif
//sales.cpp
#include "Sales.h"
#include <iostream>
using namespace std;
Sales::Sales(double *s)
{
sales[0] = s[0];
double total = sales[0];
max = sales[0];
min = sales[0];
int i;
for (i = 1; i < QUARTERS && s[i]>0; i++)
{
sales[i] = s[i];
total += sales[i];
if(max < sales[i])
max = sales[i];
if(min > sales[i])
min = sales[i];
}
average = total/i;
}
Sales::~Sales()
{
}
void Sales::setSales()
{
double total = 0;
for (int i = 0; i < QUARTERS; i++)
{
cout << "Please enter the sales for Quater " << i+1 << " : ";
cin >> sales[i];
if (i == 0)
{
max = sales[i];
min = sales[i];
}
else {
if (max < sales[i])
max = sales[i];
if (min > sales[i])
min = sales[i];
}
total += sales[i];
}
average = total/QUARTERS;
}
void Sales::showSales()
{
for (int i = 0; i < QUARTERS; i++)
{
if (sales[i] >0)
cout << "The sales for Quater " << i+1 << " is " << sales[i] << endl;
}
cout << "The max sales is " << max<< endl;
cout << "The min sales is " << min<< endl;
cout << "The average sales is " << average<< endl;
}
//main.cpp
#include "sales.h"
#include <iostream>
int main()
{
double d[] = {1000,2000,3000};
Sales s1(d);
s1.showSales();
s1.setSales();
s1.showSales();
system("pause");
return 0;
}
编程练习5
//stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer {
char fullname[35];
double payment;
};
typedef customer Item;
class Stack
{
private:
enum {MAX = 10};
Item items[MAX];
int top;
double totalpay;
public:
Stack();
~Stack();
bool isempty() const;
bool isfull() const;
bool push(const Item & item);
bool pop(Item & item);
};
#endif
//stack.cpp
#include "Stack.h"
#include <iostream>
using namespace std;
Stack::Stack()
{
top = 0;
totalpay = 0;
}
Stack::~Stack()
{
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::push(const Item & item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
bool Stack::pop(Item & item)
{
if (top > 0)
{
item = items[--top];
totalpay += item.payment;
cout << "now the payment is " << totalpay << endl;
return true;
}
else
return false;
}
//main.cpp
#include <iostream>
#include <cctype>
#include "Stack.h"
int main()
{
using namespace std;
Stack st;
char ch;
customer c;
cout << "Please enter A to add a customer, " << endl;
cout << "P to process a custmer, or Q to quite" << endl;
while(cin >> ch && toupper(ch) != 'Q')
{
while(cin.get() != '\n')
continue;
if (!isalpha(ch))
{
cout << '\a';
continue;
}
switch(ch)
{
case 'A' :
case 'a': cout << "Enter the customer's name: ";
cin.get(c.fullname, 35);
cout << "Enter the customer's payment: ";
cin >> c.payment;
//cin.get();
if (st.isfull())
cout << "stack already full" << endl;
else
st.push(c);
break;
case 'p':
case 'P':
if (st.isempty())
cout << " Stack already empty" << endl;
else
{
st.pop(c);
cout << " pause" << endl;
}
break;
}
cout << "Please enter A to add a customer, " << endl;
cout << "P to process a custmer, or Q to quite" << endl;
}
system("pause");
return 0;
}
编程练习6
//move.h
#ifndef MOVE_H_
#define MOVE_H_
class Move
{
private:
double x;
double y;
public:
Move(double a = 0, double b = 0);
~Move(void);
void showmove() const;
Move add(const Move & m) const;
void reset(double a = 0, double b = 0);
};
#endif
//move.cpp
#include "Move.h"
#include <iostream>
using namespace std;
Move::Move(double a, double b)
{
x = a;
y = b;
}
Move::~Move(void)
{
}
void Move::showmove() const
{
cout << "Current x is " << x << ", and current y is " << y << endl;
}
Move Move::add(const Move & m) const
{
Move newmove(m.x+x, m.y+y);
return newmove;
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
//main.cpp
#include "Move.h"
#include <iostream>
int main()
{
Move move1;
move1.showmove();
Move move2(3,5);
Move move3(1,2);
move2.showmove();
move3.showmove();
Move move4 = move3.add(move2);
move4.showmove();
move4.reset();
move4.showmove();
system("pause");
return 0;
}
编程练习7
//plorg.h
#ifndef PLORG_H_
#define PLORG_H_
class Plorg
{
private:
char name[20];
int CI;
public:
Plorg(char *n = "Plorga");
~Plorg();
void showplorg() const;
void newCI(int ci);
};
#endif
//plorg.cpp
#include "Plorg.h"
#include <cstring>
#include <iostream>
using namespace std;
Plorg::Plorg(char *n)
{
CI = 50;
strcpy(name,n);
}
Plorg::~Plorg()
{
}
void Plorg::showplorg() const
{
cout << name << " 's CI is " << CI << endl;
}
void Plorg::newCI(int ci)
{
CI = ci;
}
//main.cpp
#include "Plorg.h"
#include <iostream>
int main()
{
Plorg p1;
p1.showplorg();
Plorg p2("PPPP");
p2.showplorg();
p2.newCI(90);
p2.showplorg();
system("pause");
return 0;
}
练习8涉及到模板函数,函数指针等知识,这里先偷个懒,之后补上