Created
August 17, 2012 20:38
-
-
Save raarce/3382393 to your computer and use it in GitHub Desktop.
Demo of array of objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Program to demo an array of class objects | |
// Adapted from http://codeprecisely.blogspot.com/2011/09/program-to-demonstrates-array-of.html | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
class Book { | |
private: | |
string bookTitle; | |
double price; | |
public: | |
Book() {}; | |
Book(string t, float p) { | |
bookTitle = t; | |
price = p; | |
} | |
void setName(string bookName) { bookTitle = bookName; } | |
void setPrice(double cost) { price=cost; } | |
string getName() { return bookTitle; } | |
double getPrice() { return price; } | |
}; | |
const int BOOK_QTY = 3; | |
int main() { | |
Book bookCollection[BOOK_QTY] = { | |
Book("Yuyo", 12.25), | |
Book("La carreta", 18.99), | |
Book("Hunger Games", 11.99) }; | |
// example calling an object's function | |
bookCollection[1].setPrice(17.75); | |
cout << "Book Title::Price" << endl; | |
cout << "=================" << endl; | |
for(int i =0; i<BOOK_QTY; i++) { | |
cout << bookCollection[i].getName() << "::"; | |
cout << bookCollection[i].getPrice() << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment