Originally Posted by
CornedBee
That's simply not acceptable. The last three versions of Visual Studio have not even distributed the pre-standard headers. Just about every halfway recent compiler (meaning released later than 2000) either warns on their usage or fails to compile completely. They are a relic that is over 10 years old.
How can the school justify teaching with such incredibly outdated tools?
It is truly ridiculous, I myself is perplexed with the way they provide information to their students. However, I'm just thinking that maybe they are just providing the fundamentals and it's up to us to maximize the knowledge about what they've taught us. I cannot even use the book I bought because it uses the updated codings in C++. So I'm totally outdated, but, despite all of that, I research on my own and keep myself updated. I remember last week I don't even know how to use getline() I till use gets() from C. I don't even know how linker works, I don't even know how to create my own header, however, I will start researching about those stuff. I hope I could find a better book about C++.
Originally Posted by
elkvis
1. they're lazy.
2. they don't want to spend money that they think they don't have to. their likely thought process is "it says C++, it must be the same thing."
3. they don't know that there are newer standards for the language. If this is the case, you should inform them.
4. they don't care.
I think they have all of the above. It' a good thing that I joined this forum and I found a bigger world in C and C++. I never thought that there are tons of things that I have to research on my own. I guess it's up to me to find out the things in C and C++.
Code:
#include <iostream.h>
#include <string.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 = new char[ 0 ];
strcpy( productName, "" );
brandName = new char[ 0 ];
strcpy( brandName, "" );
price = 0;
}*/
Product::Product( char *p, char *b, float pr )
{
productName = new char[ strlen( p ) + 1 ];
strcpy( productName, p );
brandName = new char[ strlen( b ) + 1 ];
strcpy( brandName, b );
price = pr;
}
Product::~Product()
{
delete[] productName;
delete[] brandName;
}
void Product::setProductName( char *prodName )
{
productName = new char[ strlen( prodName ) + 1 ];
strcpy( productName, prodName );
}
char* Product::getProductName()
{
return productName;
}
void Product::setBrandName( char *brand )
{
brandName = new char[ strlen( brand ) + 1 ];
strcpy( 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( "Product1", "Brand1", 1 ),
Product( "Product2", "Brand2", 2 ),
Product( "Product3", "Brand3", 3 )};
//Product prod[ SIZE ];
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();
}
//delete[] prod
//delete[] product
//delete[] brand
cin.get();
return 0;
}
I was able to fix the problem somehow, however I cannot get the idea on how to initialize my constructor. so that I could just create an object of the class without assigning something to the constructor like: Product prod;
Without this void constructor, I cannot make any object of the class as the example above.
Code:
/*Product::Product()
{
productName = new char[ 0 ];
strcpy( productName, "" );
brandName = new char[ 0 ];
strcpy( brandName, "" );
price = 0;
}*/
This is how I fixed the problem, I don't really know how to explain it properly but as to my undersatnding the *productName before is not pointing to something: productName = prodName, am I right? because a pointer must be initialized before you can use it. The productName has dynamic allocation using the new keyword, where the size of the memory it's going to take is the size of the string that the prodName will take, plus 1 for the null that will terminate the string, is that correct? The characters that prodName received is then copied by the instance variable productName.
Code:
void Product::setProductName( char *prodName )
{
productName = new char[ strlen( prodName ) + 1 ];
strcpy( productName, prodName );
}
I was able to change the destructor for the productName and brandName pointer. I undersatnd that if you declared dynamic memory allocation using the "new with []" you will use the "delete with []" but if just the: "new without []" then you will use the "delete" without the brackets, but , honestly I don't know the difference between "new with []" and just "new without []"; "delete with []" and "delete without []". What is "new with []" and "new without []". I will still research on that.
Code:
Product::~Product()
{
delete[] productName;
delete[] brandName;
}
Please correct me if I have something wrong in my explanation and understanding.