10.1
#include
using namespace std;
#include
class Bankaccount
{
public:
};
Bankaccount::Bankaccount()
{
}
Bankaccount::Bankaccount(char*na, char*acnum, int dep)
{
}
void Bankaccount::show()
{
}
void Bankaccount::depositmoney(int money)
{
}
void Bankaccount::withdrawing_money(int money)
{
}
int main()
{
}
10.2
#include
using namespace std;
#include
#include
class Person
{
public:
};
Person::Person(const string &ln, const char * fn )//不要写成 const char *fn = "Heyyou",重定义
{
}
void Person::show()const
{
}
void Person::FormalShow()const
{
}
int main()
{
three.FormalShow();
}
10.3
//golf.h
#ifndef GOLF_H_
#define GOLF_H_
const int Len = 40;
class golf
{
char fullname[Len];
int handicap;
public:
golf(const char * name, int hc);
int setgolf();
void handicaprewrite( int hc);
void showgolf()const;
};
#endif
//golf.cpp
#include"golf.h"
#include<iostream>
using namespace std;
#include<cstring>
golf::golf(const char * name, int hc)
{
strcpy_s(fullname, strlen(name) + 1, name);
handicap = hc;
}
int golf:: setgolf()
{
cout << "Enter your name: \n";
cin.getline(fullname, Len);
cout << "Enter your handicap: \n";
cin >> handicap;
cin.get();
if (fullname == "") return 0;
return 1;
}
void golf::handicaprewrite(int hc)
{
handicap = hc;
}
void golf:: showgolf()const
{
cout << "Name : " << fullname << endl;
cout << "Handicap : " << handicap << endl;
}
//pe9 -1.cpp
#include<iostream>
#include"golf.h"
#include<cstring>
using namespace std;
int main()
{
golf g("Lily Cheng",25);
g.showgolf();
int n = 4;
for (int i = 0; i < n; i++)
{
g.setgolf();
g.showgolf();
}
}
10.4
//sale.h
#ifndef SALE_H_
#define SALE_H_
namespace SALES
{
const int QUARTERS = 4;
class Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
public:
~Sales(){};
Sales(const double ar[], int n);
Sales();
void showSales()const;
};
}
#endif
//sales.cpp
#include"sale.h"
#include<iostream>
using namespace std;
namespace SALES
{
Sales::Sales(const double ar[], int n)
{
int i;
double total = 0;
max = -999;
min = 999;
if (n > 4) n = 4;
for (i = 0; i < n; i++)
{
sales[i] = ar[i];
if (max < sales[i]) max = sales[i];
if ( min > sales[i]) min = sales[i];
total += sales[i];
}
if (n < 4)
{
for (; i < 4; i++)
sales[i] = 0;
if ( min > 0) min = 0;
if (max < 0) max = 0;
}
average = total / 4;
}
Sales::Sales()
{
int i;
double total = 0;
max = -999;
min = 999;
cout << "Sales : \n";
for (i = 0; i < 4; i++)
{
cout << i << " : ";
cin >>sales[i];
if (sales[i]> max)
max = sales[i];
if ( sales[i] < min)
min = sales[i];
total += sales[i];
}
average = total / 4;
}
void Sales::showSales()const
{
for (int i = 0; i < 4; i++)
cout << sales[i] << endl;
cout << "Average: " << average << endl;
cout << "Max: " << max << endl;
cout << "Min: " << min << endl;
}
}
//main.cpp
#include<iostream>
using namespace std;
#include"sale.h"
int main()
{
using namespace SALES;
double ar[4] = { 3.3, 2.2, 1.1, 4.4 };
double ar2[3] = { 2.2, 3.1, 1.1 };
Sales s(ar, 4);
s.showSales();
Sales s2(ar2, 3);
s2.showSales();
Sales ss;
ss.showSales();
return 0;
}
10.6
#include<iostream>
using namespace std;
class Move
{
double x;
double y;
public:
Move(double a = 0, double b = 0) :x(a), y(b){};
void showmove()const;
Move add(const Move&m)const ;
void reset(double a = 0, double b = 0){
x = a; y = b;
}
};
void Move::showmove()const
{
cout << "x = " << x << ", ";
cout << "y = " << y << endl;
}
Move Move::add(const Move&m)const
{
double xx;
double yy;
xx = m.x + x;
yy = m.y + y;
Move a(xx, yy);
a.showmove();
return a;
}
void main()
{
Move a(5.5, 6.6);
a.showmove();
Move b(3.3, 4.4);
b.showmove();
a.add(b);
a.reset();
a.showmove();
return;
}
10.7
#include<iostream>
using namespace std;
#include<cstring>
const int LIMIT = 19;
class Betelgeusean_plorg
{
char plorg[LIMIT];
int CI;
public:
Betelgeusean_plorg(char*name = "PLORGA", int ci = 50);
void SetCI(int ci);
void showPlorg()const;
};
Betelgeusean_plorg::Betelgeusean_plorg(char*name, int ci)
{
strcpy_s(plorg, LIMIT, name);
CI = ci;
}
void Betelgeusean_plorg::showPlorg()const
{
cout << "Plorg Name: " << plorg << endl;
cout << "CI: " << CI << endl;
}
void Betelgeusean_plorg::SetCI(int ci)
{
CI = ci;
}
int main()
{
Betelgeusean_plorg p;
p.showPlorg();
p.SetCI(55);
p.showPlorg();
Betelgeusean_plorg pl("Hey you", 25);
pl.showPlorg();
return 0;
}
10.5
//stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer
{
char fullname[35];
double payment;
};
typedef customer Item;
class Stack
{
enum{MAX = 10};
Item items[MAX];
int top;
static double total;
public:
Stack();
bool isempty()const;
bool isfull()const;
bool push(const Item & item);
bool pop(Item & item);
};
#endif
//stack.cpp
#include"Stack.h"
#include<iostream>
double Stack::total = 0;//要在cpp里赋值,不能在头文件赋值,会重定义
Stack::Stack()
{
top = 0;
}
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];
total += items[top].payment;
std::cout << "Total: " << total << std::endl;
return true;
}
else return false;
}
#include<iostream>
#include<cctype>
#include"Stack.h"
using namespace std;
int main()
{
Stack st;
char ch;
customer po;
cout << "Please Enter A to add a customer.\n"
<< "P to process a customer , or Q to quit.\n";
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 a customer's name to add: ";
cin.getline(po.fullname, 35);
cout << "Enter his\\her payment: ";
cin >> po.payment;
if (st.isfull())
cout << "stack already full\n";
else
st.push(po);
break;
case'P':
case'p':if (st.isempty())
cout << "stack already empty\n";
else{
st.pop(po);
cout << "customer # name: " << po.fullname << ", payment: " << po.payment
<< " popped.\n";
}break;
}
cout << "Please Enter A to add a customer.\n"
<< "P to process a customer , or Q to quit.\n";
}
cout << "Bye\n";
return 0;
}
#define LIST_H_
const int LIMIT = 10;
template < typename T>
class List
{
T list[LIMIT];
int count;
public:
List();//创建空列表
bool listadd(const T & a);
bool isempty()const;
bool isfull()const;
void visit(void(*pf)(T & a));
void show()const;
};
template<typename T>
void add_one(T &a);//随便一个函数
#include<iostream>
#include"List.h"
template <typename T>//必须写出模板类,否则T无效
List<T>::List()//模板类的定义必须有模板参数,不能只写List::List
{
list[0] = '\0';
count = 0;
}
bool List<T>::listadd(const T &a)
{
if (isfull() == 0)
{
list[count++] = a;
return true;
}
else return false;
}
bool List<T>::isempty()const
{
return count == 0;
}
bool List<T>::isfull()const
{
return count == LIMIT;
}
void List<T>::visit(void(*pf)(T &a))
{
for (int i = 0; i < count; i++)
pf(list[i]);
}
void List<T>::show()const
{
for (int i = 0; i < count; i++)
cout << list[i] << " ";
}
void add_one(T&a)
{
a = a*a;
}
#include"List.cpp"//模板定义和模板实现分开时,需要弄这个
#include<iostream>
using namespace std;
int main()
{
List<int> list;
int i;
cout << "empty? " << list.isempty() << endl;
for (i = 0; i < 5; i++)
{
list.listadd((i+2) );
}
list.show();
cout << endl;
cout << "full? " << list.isfull() << endl;
list.visit(add_one);
list.show();
return 0;
}