类中的各个变量和函数 默认是private 所以 一般可以不用写 如果是public 或者protected的话要显式的声明
如果定义了非默认的构造函数,那么必须要在定义一个默认构造函数
定义默认构造函数的方法有两种,一种是参数全部给默认值,一种不传参()
两种方法只能选其一不能同时用 不然会有二义性
举例:
Stock(const string& co = "Error", int n =0);
Stock();
析构函数
~Stock();
成员名和形参:
关于析构函数:
创建对象的两个方法 一种是直接创建,另一种是穿件临时兑现然后复制到指定的对象变量所指的内存中
如果是第二种的话在释放临时对象时调用析构函数
这个释放时间根据编译器而有所不同。
上示例代码
stock10.h
#ifndef STOCK00_H_
#define STOCK00_H_
#include <string>
class Stock
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val;}
public:
Stock();
Stock(const std::string & co, long n = 0, double pr = 0.0);
~Stock();
void acquire(const std::string & co, long n, double pr);
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
};
#endif
stock10.cpp
#include <iostream>
#include "stock10.h"
Stock::Stock()
{
std::cout << "Default constructor called\n";
company = "no name";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const std::string & co, long n, double pr)
{
std::cout << "Constructor using " << co << " called\n";
company = co;
if (n < 0)
{
std::cout << "Number of shares can't be negative; "
<< company << " shares set to 0.\n";
shares = 0;
}
else
shares = 1;
share_val = pr;
set_tot();
}
Stock::~Stock()
{
std::cout << "Bye, " << company << "!\n";
}
void Stock::buy(long num, double price)
{
if (num < 0)
{
std::cout << "Number of shares purchased can't be negative. "
<< "Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
using std::cout;
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();
}
void Stock::show()
{
// std::cout << "Company: " << company
// << " Shares: " << shares << '\n'
// << " shares Price: $" << share_val
// << " Total Worth: $" << total_val << '\n';
using std::cout;
using std::ios_base;
ios_base::fmtflags orig =
cout.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = cout.precision(3);
cout << "Company: " << company
<< " Shares: " << shares << '\n'
<< " shares Price: $" << share_val;
cout.precision(2);
cout << " Total Worth: $" << total_val << '\n';
cout .setf(orig, ios_base::floatfield);
cout.precision(prec);
}
usestock2.cpp
#include <iostream>
#include "stock10.h"
int main()
{
{
using std::cout;
cout << "Using constructors to create new objects\n";
Stock stock1("NanoSmart", 12, 20.0);
stock1.show();
Stock stock2 = Stock("Boffo Objects", 2, 2.0);
stock2.show();
cout << "Assigning stock1 to stock2:\n";
stock2 = stock1;
cout << "Listing stock1 and stock2:\n";
stock1.show();
stock2.show();
cout << "Using a constructor to reset an object\n";
stock1 = Stock("Nifty Foods", 10, 50.0);
cout << "Revised stock1:\n";
stock1.show();
cout << "Done\n";
}
return 0;
}
运行结果
main()的大括号
对象间赋值
对象重新初始化
对象也能够使用列表初始化 只要顺序对
关于对象函数的const
最后说个 对象初始化可以这样(针对构造函数只有1个形参):
Bozo tubby = 32;
但是可能会出问题 所以 在后面会介绍如何关闭这种特性
完结 明天继续