How to create dynamic array of objects in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Book
{
public:
	void show_book(void) 	{
		cout << title << ": " << price << endl;
	}
	Book(char *title, float price);
private:
	char title[256];
	float price;
};

Book::Book(char *title, float price)
{
	strcpy(Book::title, title);
	Book::price = price;
}

int main()
{
	Book *Arr[3];

	Arr[0] = new Book("The C++ Programming Language, 4th Edition", 37.48);
	Arr[1] = new Book("The Ultimate Crash Course to Learning C++", 9.95);
	Arr[2] = new Book("C++17 By Example", 44.99);

	for (int i = 0; i < 3; i++)
		Arr[i]->show_book();

	for (int i = 0; i < 3; i++)
		delete Arr[i];

	return 0;
}


/*
run:

The C++ Programming Language, 4th Edition: 37.48
The Ultimate Crash Course to Learning C++: 9.95
C++17 By Example: 44.99

*/

 



answered Mar 30, 2018 by avibootz

Related questions

1 answer 169 views
1 answer 106 views
1 answer 175 views
1 answer 205 views
1 answer 141 views
1 answer 196 views
...