第十章 对象和类
第一题:定义一个银行账户类,包括姓名,账号,存款及对应成员函数。
//file1 one.cpp
#include<iostream>
#include<string>
class Bank
{
private:
std::string name;
std::string acount;
double money;
public:
void setBank(Bank &b);
void showBank(Bank &b);
void saveBank(Bank &b);
void drawBank(Bank &b);
};
void Bank::setBank(Bank &b)
{
using std::cout;
using std::cin;
using std::endl;
cout << "Please enter the user's name: ";
getline(cin, b.name);
cout << "Please enter the user's acount: ";
cin >> b.acount;
cout << "Please enter the user's money: ";
cin >> b.money;
}
void Bank::showBank(Bank &b)
{
std::cout << "Name :" << b.name << std::endl;
std::cout << "Acount :" << b.acount << std::endl;
std::cout << "Money :" << b.money<< std::endl;
}
void Bank::saveBank(Bank &b)
{
std::cout << "How much do you want to save ? ";
double mo;
std::cin >> mo;
b.money = b.money + mo;
std::cout << "Now the balanec:" <<b.money<< std::endl;
}
void Bank::drawBank(Bank &b)
{
std::cout << "How much do you want to draw ? ";
double mo;
std::cin >> mo;
while (mo > b.money)
{
std::cout << "You draw too much!\n";
std::cout << "So,how much do you really want to draw ? ";
std::cin >> mo;
}
b.money = b.money - mo;
std::cout << "Now the balanec:" << b.money<< std::endl;
}
int main()
{
Bank usea;
usea.setBank(usea);
usea.saveBank(usea);
usea.drawBank(usea);
usea.showBank(usea);
system("pause");
return 0;
}
第二题:补全所给程序,并测试。
//file1 twohead.h
#include<iostream>
#include<string>
class Person{
private :
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person()
{
lname = " ";
fname[0] = '\0';
};
Person(const std::string &ln, const char *fn = "Heyyou");
void Show() const;
void FormalShow() const;
};
//file2 twohead.cpp
#include"twohead.h"
using namespace std;
Person::Person(const string &ln, const char *fn )
{
lname = ln;
int i = 0;
for (; i < LIMIT; i++)
{
if(!fn)
{
break;
}
fname[i] = *fn;
fn++;
}
}
void Person::Show() const
{
std::cout << fname << " " << lname;
}
void Person::FormalShow() const
{
std::cout << lname << " " << fname;
}
//file3 two.cpp
#include<iostream>
#include"twohead.h"
int main()
{
Person one;
Person two("GOODBOY");
Person three("DIMWIDDY", "Sam");
one.Show();
one.FormalShow();
std::cout << std::endl;
two.Show();
std::cout << std::endl;
two.FormalShow();
std::cout << std::endl;
three.Show();
std::cout << std::endl;
three.FormalShow();
std::cout << std::endl;
system("pause");
return 0;
}
第三题:完成上一章的编程练习1,使用golf类声明,并使用构造函数来提供初始值,使用this指针。
//file1 threehead.h
#ifndef THREEHEAD_H_
#define THREEHEAD_H_
class golf
{
private:
char fullname[40];
int handicap;
public:
golf();
golf(const char *name, int hc=0);
~golf();
golf & setgolf();
void showgolf();
};
#endif
//file2 threehead.cpp
#include<iostream>
#include<cstring>
#include"threehead.h"
golf::golf()
{
std::cout << "Default constructor" << std::endl;
fullname[40] = '\0';
handicap = 0;
}
golf::golf(const char *name, int hc)
{
strncpy_s(fullname, name, 40);
fullname[40] = '\0';
handicap = hc;
}
golf::~golf()
{
std::cout << "Destructor\n";
}
golf & golf::setgolf()
{
char funa[40];
int hacp;
std::cout << "Enter name: " ;
std::cin.getline(funa, 40);
std::cout << "Enter handicap: ";
std::cin >> hacp;
golf temp(funa,hacp);
strcpy_s(fullname, temp.fullname);
handicap = temp.handicap;
return *this;
}
void golf::showgolf()
{
std::cout << "Name is: " <<fullname<<"\n";
std::cout << "Handicap is: " <<handicap<<"\n";
}
//file3 three.cpp
#include<iostream>
#include"threehead.h"
int main()
{
golf play1("goodman");
play1.showgolf();
golf play2("badman",44);
play2.showgolf();
golf play3;
play3.setgolf();
play3.showgolf();
return 0;
}
第四题:完成上一章的编程练习4,将Sales结构及相关函数转换为一个类及方法,使用构造函数是实现原来的交互实现。
//file1 fourhead.h
#ifndef FOURHEAD_H_
#define FOURHEAD_H_
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
class sl
{
private:
Sales temp;
public:
sl();
sl(sl &s);
void showSales();
};
}
#endif
//file2 fourhead.cpp
#include<iostream>
#include"fourhead.h"
namespace SALES{
using namespace std;
sl::sl()
{
double sum=0;
for (int i = 0; i < QUARTERS; i++)
{
cout << "Please enter a number: ";
cin >> temp.sales[i];
temp.max = temp.min = temp.sales[0];
if (temp.max <= temp.sales[i])
temp.max = temp.sales[i];
if (temp.min >= temp.sales[i])
temp.min = temp.sales[i];
sum += temp.sales[i];
}
temp.average = sum / QUARTERS;
}
sl::sl(sl &s)
{
double sum=0;
for (int i = 0; i < QUARTERS; i++)
{
s.temp.max = s.temp.min = s.temp.sales[0];
if (s.temp.max <= s.temp.sales[i])
s.temp.max = s.temp.sales[i];
if (s.temp.min >= s.temp.sales[i])
s.temp.min = s.temp.sales[i];
sum += s.temp.sales[i];
}
s.temp.average = sum / QUARTERS;
}
void sl::showSales()
{
cout << "THE average is: " << temp.average << endl;
cout << "THE max is: " << temp.max << endl;
cout << "THE min is: " << temp.min << endl;
}
}
//file3 four.cpp
#include<iostream>
#include"fourhead.h"
using namespace SALES;
int main()
{
sl ca1;
ca1.showSales();
sl(ca2);
ca2.showSales();
system("pause");
return 0;
}
第五题:完成指定结构声明,使用堆栈来删除和添加该结构。
// file five.cpp
#include<iostream>
struct customer
{
char fullname[35];
double payment;
};
typedef customer* Item;
using namespace std;
int main()
{
Item ps = new customer;
cout << "Please enter infomation(enter q quit)\n";
cout << "The name: ";
cin >> ps->fullname;
int count = 0;
while (strcmp(ps->fullname, "q"))
{
cin.get();
cout << "The pay ment: ";
cin >> ps->payment;
count++;
cin.get();
ps++;
cout << "The name: ";
cin >> ps->fullname;
}
ps--;
cout << "Do you wann delete " << ps->fullname;
cout << " ? y/n ";
char choose;
double amount=0;
cin >> choose;
while (count)
{
if (choose == 'y')
{
amount = amount += ps->payment;
}
cout << "Now the money is " << amount << endl;
ps--;
if (--count)
{
cout << "Do you wann delete " << ps->fullname;
cout << " ? y/n ";
}
else
{
cout << "No users now ";
break;
}
cin >> choose;
}
ps++;
delete ps;
system("pause");
}
ps:程序再结束时可能因为指针的原因出现异常,待以后再解决。
第六题:完成指定类声明,并提供对应函数定义,最后进行测试。
// file six
#include<iostream>
using namespace std;
class Move
{
public:
Move(double a = 0, double b = 0);
void Moveadd(const Move &m) ;
void showmove()const;
void reset(double a = 0, double b = 0);
private:
double x;
double y;
};
Move::Move(double a , double b )
{
x = a;
y = b;
}
void Move::showmove()const
{
cout << "x is " << x << " , y is " << y << " .\n";
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
void Move::Moveadd(const Move &m)
{
x = x + m.x;
y = y + m.y;
}
int main()
{
Move a;
cout << "a inclued: ";
a.showmove();
Move b(3, 5);
cout << "b inclued: ";
b.showmove();
Move c(7, 5);
cout << "c inclued: ";
c.showmove();
b.Moveadd(c);
cout << "b+c is: ";
b.showmove();
b.reset();
cout << "b after reset: ";
b.showmove();
system("pause");
}
第七题:完成要求类声明,并提供对应函数定义,最后进行测试。
//file1 sevenhead.h
#ifndef SEVENHEAD_H_
#define SEVENHEAD_H_
class plorg
{
private:
char name[19];
int CI;
public:
plorg(char n[]="Plorga",int c=50);
void showplorg()const;
void setci(int a);
};
#endif // !SEVENHEAD_H_
//file2 sevenhead.cpp
#include"sevenhead.h"
#include<iostream>
#include<string>
plorg::plorg(char n[], int c )
{
//for(int i=0;i<19;i++)
//name[i]= n[i];
strncpy_s(name, n,18);
name[19] = '\0';
CI = c;
}
void plorg::showplorg()const
{
std::cout << "name: "<<name;
std::cout << ". CI: " << CI << std::endl;
}
void plorg::setci(int a)
{
CI = a;
}
//file3 seven.cpp
#include<iostream>
#include"sevenhead.h"
int main()
{
plorg a;
a.showplorg();
plorg b("good boy BOYBOYBOYBOYBOYBOYBOYBOY", 66);
b.showplorg();
b.setci(72);
b.showplorg();
system("pause");
return 0;
}
第八题:完成要求列表定义,使用类方法实现部分功能,并使用函数应用对其进行操作。
//file1 eighthead.h
#ifndef _EIGHTHEAD_
#define _EIGHTHEAD_
typedef unsigned long Item;
class List
{
private:
enum {MAX=10};
Item list[MAX];
int top;
public:
List();
bool isempty()const;
bool isfull()const;
bool push(const Item & item);
bool pop(Item & item);
void visit(void (*pf)(Item &));
};
#endif
//file2 eighthead.cpp
#include"eighthead.h"
#include<iostream>
List::List()
{
top = 0;
}
bool List::isempty()const
{
if (top == 0)
return true;
else
return false;
}
bool List::isfull()const
{
if (top == MAX)
return true;
else
return false;
}
bool List::push(const Item & item)
{
if (top < MAX)
{
list[top++] = item;
return true;
}
else
{
return false;
}
}
bool List::pop( Item & item)
{
if (top > 0)
{
item=list[--top];
return true;
}
else
{
return false;
}
}
void List::visit(void (*pf)(Item & ))
{
for (int i = 0; i < top; i++)
(*pf)(list[i]);
}
//file3 eight.cpp
#include<iostream>
#include"eighthead.h"
using namespace std;
void showlist(Item & item);
int main()
{
List IT;
Item c,b;
c = 64;
for (int i = 0; i < 10; i++)
{
IT.push(c);
}
IT.visit(showlist);
cout << IT.isempty() << endl;
cout << IT.isfull() << endl;
for (int i = 0; i < 10; i++)
{
IT.pop(b);
}
cout << IT.isempty() << endl;
cout << IT.isfull() << endl;
system("pause");
return 0;
}
void showlist(Item & item)
{
cout << item << endl;
}
ps:第十章完成了,感觉最后学习继承,友元等几个概念后可以开始实战了。虽然很多外界压力,挺住,加油!