Array Object - Terminating Compiler
I have been practicing C++ for a month now. I'm in the pace of programming array of objects. The compiler is successfully compiling the program but the problem is after I input the first element for the array of object the compiler is terminated. I end up reopening the compiler and debug whatever the logical error is. I've spent a week debugging the issue but I can't understand what's happening.
Code:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class Product
{
private: //private by default
char *productName;
char *brandName;
float price;
public:
Product( void );
~Product( void );
Product( char *p, char *b, float pr );
void setProductName( char *prodName );
char *getProductName();
void setBrandName( char *brand );
char *getBrandName();
void setPrice( float p );
float getPrice();
void displayProducts();
};
Product::Product()
{
productName = "";
brandName = "";
price = 0;
}
Product::Product( char *p, char *b, float pr )
{
productName = p;
brandName = b;
price = pr;
}
Product::~Product()
{
delete productName;
delete brandName;
}
void Product::setProductName( char *prodName )
{
productName = prodName;
}
char* Product::getProductName()
{
return productName;
}
void Product::setBrandName( char *brand )
{
brandName = brand;
}
char* Product::getBrandName()
{
return brandName;
}
void Product::setPrice( float p )
{
price = p;
}
float Product::getPrice()
{
return price;
}
void Product::displayProducts()
{
cout << "Product Name: " << getProductName() << endl;
cout << "Brand Name : " << getBrandName() << endl;
cout << "Price : " << getPrice() << endl;
}
//MAIN FUNCTION
int main( void )
{
const char SIZE = 3;
char product[ 30 ];
char brand[ 30 ];
float price;
Product prod[ SIZE ] = { Product( "TestProduct1", "TestBrand1", 1 ),
Product( "TestProduct2", "TestBrand2", 2 ),
Product( "TestProduct3", "TestBrand3", 3 ) };
clrscr();
/*for( int counter = 0; counter < SIZE; counter++ )
{
cout << "Product: "; cin.getline( product, 30 );
cout << "Brand : "; cin.getline( brand, 30 );
cout << "Price : "; cin >> price;
prod[ counter ] = Product( product, brand, price );
cout << endl;
}*/
/*for( int counter1 = 0; counter1 < SIZE; counter1++ )
{
cout << "Record [ " << (counter1+1) << " ]:\n\n ";
prod[ counter1 ].displayProducts();
}*/
prod[ 0 ].displayProducts();
prod[ 1 ].displayProducts();
prod[ 2 ].displayProducts();
delete[] prod;
delete[] product;
delete[] brand;
//getch();
cin.get();
return 0;
}
Even if you remove the comment to the loops you will still have the same problem. I hope you could help and explain to me what's really happening. thanks in advance forum members.
The compiler that I'm using is pretty old so it doesn't have any updated header.
Compiler: Turbo C++ 3.0