构建一个类Book,其中含有两个私有数据成员number(数量)和price(价格),编写测试main函数, 建立一个有5个元素的数组对象,将number初始化为1~5,将price 初始化为number的10倍,显示每个对象的number*price值。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class Book
{
public:
Book(int n, double p);
Book();
~Book();
private:
int number;
double price;
};
Book::Book()
{
}
Book::Book(int n, double p)
{
number = n;
price = p;
cout << "price*number:" << price * number << endl;
}
Book::~Book()
{
}
int main()
{
Book* B = new Book[5];
for (int i = 1; i <= 5; i++)
{
Book* p = B;
p = new Book(i, i * 10); //动态创建数组元素
p++;
}
delete[] B;
}