对象数组用于创建同一个类的多个对象。声明对象数组的方法与声明标准类陷阱数组相同。以Stock类为例:
class Stock
{
private:
std::string company;
int shares;
double share_val;
double total_val;
void set_tot(){return total_val = shares * share_val;}
public:
Stock();
Stock(const std::string & c0, int n = 0, double pr = 0.0);
~Stock();
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;
};
Stock mystuff[4];//创建一个包含4个Stock对象的数组mystuff
上述创建对象数组的方式未显式初始化类对象,则调用默认构造函数。若要给对象数组的每个元素进行初始化,创建对象数组方式如下:
const int STKS = 10;
Stock stocks[STKS] = {
Stock("NanoSmart', 12.5, 20),
Stock(),
Stock("Monolithic Obelisks", 130, 3.25),
Stock("Fleep Enterprises", 60, 6.5)
};
对象数组初始化:用括号括起来、以逗号分隔的值列表,每次构造函数调用表示一个数组元素的值。初始化对象数组的方案是,首先使用默认构造函数创建数组元素,然后花括号中的构造函数将创建临时对象,然后将临时对象的内容复制到相应的元素中。
示例程序:将4个数组元素进行初始化,显式它们的内容,并找出这些元素中total_val最高的一个。
1、头文件
//stock20.h--augmented version
#pragma once
#include <string>
class Stock
{
private:
std::string company;
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
Stock();
Stock(const std::string & c0, int n = 0, double pr = 0.0);
~Stock();
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;
};
2、实现文件
//stock20.cpp--augement version
#include "stock20.h"
#include <iostream>
Stock::Stock()
{
company = "no name";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const std::string& co, int n, double pr)
{
company = co;
if (n < 0)
{
std::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 Stock::buy(long num, double price)
{
if (num < 0)
{
std::cout << "Number of shares purchased can't be nagative." << "Transacyion 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 nagative."
<< "Transaction is abored.\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()const
{
using std::cout;
using std::ios_base;
//set format #.###
ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = cout.precision(3);
cout << "Company: " << company << " Shares: " << shares << '\n';
cout << "Shares Price:$" << share_val;
//set format to #.##
cout.precision(2);
cout << " Total Worth:$" << total_val << '\n';
//restore original format
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}
const Stock & Stock::topval(const Stock & s)const
{
if (s.total_val > total_val)
return s;
else
return *this;
}
3、使用文件
//usestock2.cpp-- using the Stock class
//compilewith stock20.cpp
#include <iostream>
#include "stock20.h"
const int STKS = 4;
int main()
{
Stock stocks[STKS] = {
Stock("NanoSmart", 12, 20.0),
Stock("Boffo Objects", 200, 2.0),
Stock("Monolithic Oblisks", 130, 3.25),
Stock("Fleep Enterprises", 60, 6.5)
};
std::cout << "Stock holding:\n";
int st;
for (st = 0; st < STKS; st++)
{
stocks[st].show();
}
//set pointer to first element
const Stock* top = &stocks[0];
for (st = 0; st < STKS; st++)
top = &top->topval(stocks[st]);
//now top points to the most valuable holding
std::cout << "\nMost valuable holding:\n";
top->show();
return 0;
}
运行结果:循环展示对象数组每个元素的数据。可见total_val值最高的是stock[2].再遍历数组中的所有对象,通过topval()方法,获取对象数组中total_val值最高的对象,并显式该对象的数据。