第十章的总结:
1.如果可以通过初始化,也可以通过赋值来设置对象的值,应采取初始化,通常效率会更高。
2.在函数声明后面加上const,可以确保不能在函数内更改属性值。
3.用户自己定义的数据类型,包含了数据的封装和函数
4.数据是私有的,方法是共用的。
5.构造函数在创建对象或者显式调用构造函数的时候调用,析构函数在对象过期被注销的时候调用。
代码练习:
6.没有参数或者所有的参数都有默认值的构造函数就是默认构造函数,不需要显式调用。可以声明对象而不初始化它,还可以声明数组。
7.this就是指向类对象自身的指针,在对象内可以使用,*this就是类对象的地址。
详细内容见
//stock00.h
#ifndef _STOCK00_H_
#define _STOCK00_H_
#include <iostream>
#include <string>
using namespace std;
class Stock
{
public:
Stock();
~Stock();
Stock(const string & co, long n, double pr);
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show() const;
const Stock & topval(const Stock & s) const;
private:
string company;
long shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
};
#endif
//stock00.cpp
#include <iostream>
#include "stock00.h"
#include<string>
using namespace std;
Stock::Stock()
{
company = "no name";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::~Stock()
{
cout << "by by" << endl;
}
Stock::Stock(const string & co,long n,double pr)
{
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();
}
void Stock::buy(long num, double price)
{
if (num < 0)
{
cout << "Number of shares puchased can't be negative. "
<< "transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
if (num < 0)
{
cout << "Number of shares sold 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::update(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;
}
}
void Stock::show() const
{
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(3);
cout << "Company: " << company
<< " Shares: " << shares << endl;
cout << " Share Price: $ " << share_val
<< " Total Worth: $ " << total_val << endl;
}
//main.cpp
const int STKS = 4;
int main()
{
Stock stocks[STKS] =
{
Stock("NamoSmart",12,20.0),
Stock("Boffo Objects",200,2.0),
Stock("Monolthic",130,3.25),
Stock("Fleep Enterprises",60,6.5)
};
cout << "Stock holdings:" << endl;
int st;
for (st = 0; st < STKS; st++)
{
stocks[st].show();
}
const Stock *top = &stocks[0];
for (st = 1; st < STKS; st++)
{
top = &top->topval(stocks[st]);
}
cout << "\nMost valuable valuable holding:\n";
top->show();
return 0;
}
代码2:
#ifndef _TEST_H_
#define _TEST_H_
class Test
{
public:
Test();
Test(const char *name);
Test(const Test & other);
Test & operator=(const Test & other);
~Test();
void show();
private:
char *name_;
};
#endif
//test.cpp
#include "Test.h"
#include <iostream>
#pragma warning(disable:4996)
using namespace std;
Test::Test()
{
}
Test::Test(const char *name)
{
cout << "Test 1" << endl;
int len = strlen(name);
name_ = new char(len);
memset(name_, 0, len);
strcpy(name_, name);
}
Test::Test(const Test & other)
{
cout << "Test 2" << endl;
int len = strlen(other.name_) + 1;
name_ = new char(len);
memset(name_, 0, len);
strcpy(name_, other.name_);
}
Test::~Test()
{
//delete name_;
cout << "delete name_ " << endl;
}
Test & Test::operator=(const Test & other)
{
cout << "Test = operator" << endl;
if (this == &other)
{
return *this;
}
int len = strlen(other.name_) + 1;
name_ = new char(len);
memset(name_, 0, len);
strcpy(name_, other.name_);
return *this;
}
void Test::show()
{
cout << "Name: " << name_ << endl;
}
//main.cpp
#include "Test.h"
#include <iostream>
using namespace std;
int main()
{
//Test p("wang");
Test p1;
p1 = "hello";
p1.show();
return 0;
}
代码码3 实现stack的类
#ifndef _STACK_H_
#define _STACK_H_
typedef unsigned long Item;
class Stack
{
public:
Stack();
bool isempty() const;
bool isfull() const;
bool push(const Item & item);
bool pop(Item & item);
private:
enum{MAX = 10};
Item items[MAX];
int top;
};
#endif
#include "Stack.h"
#include <iostream>
using namespace std;
Stack::Stack()
{
top = -1;
}
bool Stack::isempty() const
{
return top == -1;
}
bool Stack::isfull() const
{
return top == MAX - 1;
}
bool Stack::push(const Item & item)
{
if(isfull() == true)
{
cout <<"stack is full!" << endl;
return false;
}
top++;
items[top] = item;
return true;
}
bool Stack::pop(Item & item)
{
if(isempty() == true)
{
cout <<" stack is empty" << endl;
return false;
}
item =items[top--];
return true;
}
//main.cpp
include “Stack.h”
include
include
using namespace std;
int main()
{
Stack st;
char ch;
unsigned long po;
cout <<”Please enter A to add a purchase order” << endl;
cout <<”P to process a PO,or Q to quit” << 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 << "Entera PO number to add: ";
cin >> po;
st.push(po);
break;
}
case 'P':
case 'p':
{
if(st.pop(po) == true)
{
cout << "PO #" << po << "popped " << endl;
}
break;
}
}
cout << "Please enterA to add purchase order\n"
<< "P to process a PO, or Q to quit." << endl;
}
cout << "Bye" << endl;
return 0;
}