12.10.1
Cow.cpp
#include "Cow.h"
Cow::Cow(void)
{
strcpy(name, "xswl");
hobby = new char[1];
hobby[0] = '\0';
weight = 0;
}
Cow::Cow(const char * nm, const char * ho, double wt) : weight(wt)
{
strcpy(name, nm);
hobby = new char[strlen(ho) + 1];
strcpy(hobby, ho);
}
Cow::Cow(const Cow & c)
{
strcpy(name, c.name);
hobby = new char[strlen(c.hobby) + 1];
strcpy(hobby, c.hobby);
weight = c.weight;
}
Cow::~Cow(void)
{
delete [] hobby;
}
Cow & Cow::operator=(const Cow & c)
{
if(this == &c)
return *this;
strcpy(name, c.name);
hobby = new char[strlen(c.hobby) + 1];
strcpy(hobby, c.hobby);
weight = c.weight;
return *this;
}
void Cow::ShowCow(void) const
{
cout << "name: " << name << endl;
cout << "hobby: " << hobby << endl;
cout << "weight: " << weight << endl;
}
main.cpp
#include "Cow.h"
int main(void)
{
Cow a, b("hh", "xswl", 6.6), c(b);
a.ShowCow();
b.ShowCow();
c.ShowCow();
a = b;
a.ShowCow();
return 0;
}
12.10.2
String2.h
#ifndef STRING2_H
#define STRING2_H
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;
class String
{
private:
char * str;
int len;
static int num;
static const int CINLIM = 80;
public:
String(void);
String(const char *);
String(const String &);
~String(void);
int lenght(void) const { return len; };
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int);
const char & operator[](int) const;
String operator+(const String &);
void Stringlow(void);
void StringUp(void);
int count(char);
friend bool operator<(const String &, const String &);
friend bool operator>(const String &, const String &);
friend bool operator==(const String &, const String &);
friend ostream & operator<<(ostream &, const String &);
friend istream & operator>>(istream &, String &);
static int HowMany(void);
};
#endif
String2.cpp
#include "String2.h"
int String::num = 0;
String::String(void)
{
len = 1;
str = new char[1];
str[0] = '\0';
num++;
}
String::String(const char * s)
{
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
num++;
}
String::String(const String & st)
{
num++;
len = st.len;
str = new char[len + 1];
strcpy(str, st.str);
}
String::~String(void)
{
num--;
delete [] str;
}
String & String::operator=(const String & st)
{
if(this == &st)
return *this;
delete [] str;
len = st.len;
str = new char[len + 1];
strcpy(str, st.str);
return *this;
}
String & String::operator=(const char * s)
{
delete [] str;
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
return *this;
}
char & String::operator[](int i)
{
return str[i];
}
const char & String::operator[](int i) const
{
return str[i];
}
String String::operator+(const String & st)
{
char temp[len + 1];
strcpy(temp, str);
delete [] str;
len += st.len;
str = new char[len + 1];
strcpy(str, temp);
strcat(str, st.str);
return *this;
}
void String::Stringlow(void)
{
char * st = str;
while(*st)
*st++ = tolower(*st);
}
void String::StringUp(void)
{
char * st = str;
while(*st)
*st++ = toupper(*st);
}
int String::count(char ch)
{
int num = 0;
char * st = str;
while(*st)
{
if(*st++ == ch)
num++;
}
return num;
}
bool operator<(const String & st1, const String & st2)
{
return strcmp(st1.str, st2.str) < 0;
}
bool operator>(const String & st1, const String & st2)
{
return st2 < st1;
}
bool operator==(const String & st1, const String & st2)
{
return strcmp(st1.str, st2.str) == 0;
}
ostream & operator<<(ostream & out, const String & st)
{
out << st.str;
return out;
}
istream & operator>>(istream & in, String & st)
{
char temp[String::CINLIM];
in.get(temp, String::CINLIM);
if(in)
st = temp;
while(in && in.get() != '\n')
continue;
return in;
}
int String::HowMany(void)
{
return num;
}
main.cpp
#include "String2.h"
int main(void)
{
String a("hh"), b("xswl");
cout << a << endl << b << endl;
String c = a + b;
cout << c << endl;
c.StringUp();
cout << c << endl;
cout << c.count('H');
return 0;
}
12.10.3
stock20.h
#ifndef STOCK20_H
#define STOCK20_H
#include <iostream>
#include <cstring>
using namespace std;
class Stock
{
private:
char * company;
int shares;
double share_val;
double total_val;
void set_tot(void) { total_val = shares * share_val; }
public:
Stock(void);
Stock(const char *, long n = 0, double pr = 0.0);
~Stock(void);
void buy(long, double);
void sell(long, double);
void updata(double);
const Stock & topval(const Stock &) const;
friend ostream & operator<<(ostream &, const Stock &);
};
#endif
stock20.cpp
#include "stock20.h"
Stock::Stock(void)
{
company = new char[8];
strcpy(company, "no name");
shares = share_val = total_val = 0;
}
Stock::Stock(const char * co, long n, double pr)
{
company = new char[strlen(co) + 1];
strcpy(company, co);
if(n < 0)
{
cout << "Number of shares can't be negative; "
<< company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
Stock::~Stock(void)
{
delete [] company;
}
void Stock::buy(long num, double price)
{
if(num < 0)
cout << "Number of shares can't be negative. "
<< "Transaction is aborted.\n";
else
{
share_val = price;
shares += num;
set_tot();
}
}
void Stock::sell(long num, double price)
{
if(num < 0)
cout << "Number of shares can't be negative. "
<< "Transaction is aborted.\n";
else if(num > shares)
cout << "You can't sell more than you have! "
<< "Transaction is aborted.\n";
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::updata(double price)
{
share_val = price;
set_tot();
}
const Stock & Stock::topval(const Stock & s) const
{
if(s.total_val > total_val)
return s;
else
return *this;
}
ostream & operator<<(ostream & out, const Stock & s)
{
out << "Company: " << s.company
<< " Shares: " << s.shares << '\n';
out << " Share Price: $" << s.share_val;
out << " Total Worth: $" << s.total_val << '\n';
return out;
}
main.cpp
#include "stock20.h"
int main(void)
{
Stock stocks[4] =
{
Stock("NanoSmart", 12, 20.0),
Stock("Boffo Objects", 200, 2.0),
Stock("Monolithic Obelisks", 130, 3.25),
Stock("Fleep Enterprises", 60, 6.5)
};
cout << "Stock holdings:\n";
int st;
for(st = 0; st < 4; st++)
cout << stocks[st] << endl;
const Stock * top = &stocks[0];
for(st = 1; st < 4; st++)
top = &top->topval(stocks[st]);
cout << "\nMost valuable holding:\n";
cout << *top << endl;
return 0;
}
12.10.4
Stack.h
#ifndef STACK_H
#define STACK_H
typedef unsigned long Item;
#include <iostream>
using namespace std;
class Stack
{
private:
enum { MAX = 10 };
Item * pitems;
int size;
int top;
public:
Stack(int n = MAX);
Stack(const Stack &);
~Stack(void);
bool isempty(void) const;
bool isfull(void) const;
bool push(const Item &);
bool pop(Item &);
Stack & operator=(const Stack &);
friend ostream & operator<<(ostream &, const Stack &);
};
#endif
Stack.cpp
#include "Stack.h"
Stack::Stack(int n)
{
pitems = new Item[n];
size = n;
top = 0;
}
Stack::Stack(const Stack & st)
{
pitems = new Item[st.size];
size = st.size;
top = st.top;
for(int i = 0; i < st.top; i++)
pitems[i] = st.pitems[i];
}
Stack::~Stack(void)
{
delete [] pitems;
}
bool Stack::isempty(void) const
{
return top == 0;
}
bool Stack::isfull(void) const
{
return top == size;
}
bool Stack::push(const Item & item)
{
if(isfull())
return false;
pitems[top++] = item;
return true;
}
bool Stack::pop(Item & item)
{
if(isempty())
return false;
item = pitems[top - 1];
top--;
return true;
}
Stack & Stack::operator=(const Stack & st)
{
if(this == &st)
return *this;
delete [] pitems;
pitems = new Item[st.size];
size = st.size;
top = st.top;
for(int i = 0; i < st.top; i++)
pitems[i] = st.pitems[i];
return *this;
}
ostream & operator<<(ostream & out, const Stack & st)
{
out << "{ ";
for(int i = 0; i < st.top; i++)
out << st.pitems[i] << ' ';
out << '}' << endl;
return out;
}
main.cpp
#include "Stack.h"
int main(void)
{
Stack a, b;
a.push(88);
a.push(44);
b = a;
cout << a << b;
Item cc;
a.pop(cc);
cout << a << cc;
return 0;
}
5,6题的程序练习之后删了就跳了这俩,可以参考https://www.cnblogs.com/banmei-brandy/p/11460755.html