第十章
10.10.1
#ifndef BAND_H_
#define BAND_H_
#include<string>
using namespace std;
class Band
{
public:
Band(string bandName = "NULL", string account="NULL", double deposit = 0.0);
~Band();
void ShowBand();
void PushBand(double Indesposit);
void PopBand(double Outdesposit);
private:
std::string m_bandName;
std::string m_account;
double m_deposit;
};
#endif
#include <iostream>
#include "Band.h"
using namespace std;
Band::Band(string bandName, string account, double deposit)
{
m_bandName = bandName;
m_account = account;
m_deposit = deposit;
}
Band::~Band()
{
}
void Band::ShowBand()
{
cout << "BandName: " << m_bandName << endl;
cout << "Bandaccount: " << m_account << endl;
cout << "Desposit: " << m_deposit << endl;
}
void Band::PushBand(double Indesposit)
{
m_deposit += Indesposit;
}
void Band::PopBand(double Outdesposit)
{
m_deposit += Outdesposit;
}
#include <iostream>
#include "Band.h"
int main()
{
Band My("Arthur Yong", "18826451338", 15000.0);
My.ShowBand();
My.PushBand(1500.5);
My.PopBand(3578.9);
My.ShowBand();
cin.get();
return 0;
}
10.10.2
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
using namespace std;
class Person
{
public:
Person(){ m_lname = ""; m_fname[0] = ‘\0‘; }
Person(const string &ln, const char *fn = "Heyyou");
void Show() const;
void FormalShow() const;
private:
static const int LIMIT = 25;
string m_lname;
char m_fname[LIMIT];
};
#endif
#include <iostream>
#include "Person.h"
using namespace std;
Person::Person(const string &ln, const char *fn)
{
m_lname = ln;
strcpy_s(m_fname, fn);
}
void Person::Show() const
{
cout << m_fname << " " << m_lname << endl;
}
void Person::FormalShow() const
{
cout << m_lname << " " << m_fname<< endl;
}
#include<iostream>
#include"Person.h"
using namespace std;
int main()
{
Person one;
Person two("Smywiddy");
Person three("Dimwiddy", "Sam");
two.Show();
cout << endl;
three.FormalShow();
cin.get();
return 0;
}
10.10.3
#ifndef GOLF_H_
#define GOLF_H_
class golf
{
public:
golf();
golf(const char *fullname,int handcap);
~golf();
golf setgolf(const char *fullname,int handcap);
void showgolf() const;
private:
static const int m_len = 40;
char m_fullname[m_len];
int m_handcap;
};
#endif
#include <iostream>
#include "golf.h"
golf::golf()
{
m_fullname[0] = ‘\0‘;
m_handcap = 0;
}
golf::golf(const char *fullname, int handcap)
{
strcpy_s(m_fullname, fullname);
m_handcap = handcap;
}
golf::~golf()
{}
golf golf::setgolf(const char *fullname, int handcap)
{
golf temp = golf(fullname, handcap);
//golf temp(fullname,handcap);
strcpy_s(m_fullname, temp.m_fullname);
m_handcap = temp.m_handcap;
return *this;
}
void golf::showgolf() const
{
using namespace std;
cout << "fullname : " << m_fullname << endl;
cout << "handcap: " << m_handcap << endl;
}
#include <iostream>
#include "golf.h"
int main()
{
using namespace std;
golf A;
golf B("Arthur Yong", 3);
B.showgolf();
A.setgolf("Avalon Y", 5);
A.showgolf();
cin.get();
return 0;
}
10.10.4
#ifndef CUSTOMER_H_
#define CUSTOMER_H_
struct customer
{
char fullname[35];
double payment;
};
#endif
#ifndef STACK_H_
#define STACK_H_
template <typename T>
class Stack
{
public:
Stack(int size = Stack_init_size);
//构造函数
~Stack();
Stack(const Stack<T> &s);
//拷贝构造函数
const Stack<T>& operator=(const Stack<T> &s);
//赋值构造函数
void ClearStack();
//清空栈
bool StackEmpty();
//若栈为空返回TRUE否则返回FALSE
int StackLength();
//返回栈的长度,即入栈元素个数
bool GetTop(T &elem);
//若栈不空,用elem返回栈顶元素,并返回true,否则返回ERROR
void Push(T elem);
//插入元素elem为新的栈顶元素
bool Pop(T &elem);
//若栈不为空,则删除栈顶元素,用elem返回其值,并返回true,否则返回error
private:
T *base;
T *top;
int m_size;
enum StackSize
{
Stack_init_size = 100, //存储空间初始分配量
Stack_incerement = 10 //存储空间分配增加量
};
};
template <typename T>
Stack<T>::Stack(int size)
{
base = new T[Stack_init_size];
if (base==NULL)
{
exit(1);
}
top = base;
m_size = size;
}
template <typename T>
Stack<T>::Stack(const Stack<T> &s)
{
base = new T[s.m_size];
if (base == NULL)
{
exit(1);
}
top = base;
m_size = s.m_size;
T * p1 =s.base ;
T * p2 = base;
while (p1!=s.top)
{
*p2 = *p1;
p1++;
p2++;
}
top = p2;
}
template <typename T>
const Stack<T>& Stack<T>::operator=(const Stack<T> &s)
{
delete[] base;
base = new T[s.m_size];
if (base == NULL)
{
exit(1);
}
top = base;
m_size = s.m_size;
T * p1 = s.base;
T * p2 = base;
while (p1 != s.top)
{
*p2 = *p1;
p1++;
p2++;
}
top = p2;
return *this;
}
template <typename T>
Stack<T>::~Stack()
{
delete[] base;
}
template <typename T>
int Stack<T>::StackLength()
{
int n = 0;
T * p = top;
while (p!= base)
{
n++;
p--;
}
return n;
}
/*
template <typename T>
void Stack<T>::ClearStack()
{
while (top != base)
{
top--;
*top = 0;
}
}*/
template <typename T>
bool Stack<T>::StackEmpty()
{
if (top == base)
{
return true;
}
return false;
}
template <typename T>
bool Stack<T>::GetTop(T &elem)
{
if (StackEmpty())
{
return false;
}
else
{
top--;
elem = *top;
// *top = 0;
return true;
}
}
template <typename T>
void Stack<T>::Push(T elem)
{
int size_increment;
if (top - base>Stack_init_size)
{
m_size = m_size + Stack_incerement;
T *temp = new T[m_size];
T *p = base;
T *q = temp;
while ((p + 1) != top)
{
*q = *p;
q++;
p++;
}
delete[] base;
base = temp;
*q = elem;
q++;
}
else
{
*top = elem;
top++;
}
}
template <typename T>
bool Stack<T>::Pop(T &elem)
{
if (top == base)
{
return false;
}
top--;
elem = *top;
// *top = 0;
return true;
}
#endif
#include <iostream>
#include "Stack.h"
#include"customer.h"
#include <cstdlib>
int main()
{
using namespace std;
Stack<customer> T;
customer temp[2]=
{
{ "Arthur Yong",1500.5 }, {"Avalon Y",2000.5}
};
T.Push(temp[0]);
T.Push(temp[1]);
customer m;
T.Pop(m);
cout << m.fullname << " " << m.payment << endl;
system("pause");
return 0;
}
10.10.6
#ifndef MOVE_H_
#define MOVE_H_
using namespace std;
class Move
{
public:
Move(double x=0.0, double y=0.0){ m_x = x; m_y = y; };
~Move(){};
void showmove() const;
Move add(const Move & s) const;
void reset(double a = 0.0, double b = 0.0);
private:
double m_x;
double m_y;
};
#endif
#include <iostream>
#include "Move.h"
void Move::showmove() const
{
cout << "m_x=: " << m_x << " " << "m_y=: " << m_y << endl;
}
Move Move::add(const Move & s) const
{
double temp_x = m_x + s.m_x;
double temp_y = m_y + s.m_y;
Move temp=Move(temp_x,temp_y);
return temp;
}
void Move::reset(double a , double b)
{
m_x = a;
m_y = b;
}
#include <iostream>
#include "Move.h"
int main()
{
Move a(2.5,3.5);
Move b=Move(1.5,2.3);
Move c;
a.showmove();
b.showmove();
c = a.add(b);
c.showmove();
c.reset(5.5, 5.5);
c.showmove();
cin.get();
return 0;
}
10.10.7
#ifndef PLORG_H_
#define PLORG_H_
class plorg
{
public:
plorg();
plorg(const char *plorgname, int plorgCI);
~plorg();
void SetCI(int plorgCI);
void Showplorg();
private:
static const int len = 20;
char m_plorgname[len];
int m_plorgCI;
};
#endif
#include <iostream>
#include "Plorg.h"
plorg::plorg()
{
strcpy_s(m_plorgname, "Plorga");
m_plorgCI = 0;
}
plorg::plorg(const char *plorgname, int plorgCI)
{
strcpy_s(m_plorgname,plorgname);
m_plorgCI = plorgCI;
}
plorg::~plorg()
{
}
void plorg::SetCI(int plorgCI)
{
m_plorgCI = plorgCI;
}
void plorg::Showplorg()
{
using namespace std;
cout << "Plorg Name: " << m_plorgname << endl;
cout << "Plorg CI: " << m_plorgCI << endl;
}
#include <iostream>
#include"Plorg.h"
int main()
{
using namespace std;
plorg A;
A.Showplorg();
plorg B("Arthur Yong", 5);
B.Showplorg();
cin.get();
return 0;
}
时间: 2024-11-23 10:20:05