/* Filename: try3.cpp Author: Br. David Carlson Date: July 5, 1998 Last Revised: November 26, 2001 This test program creates a few objects of type Product and prints their contents on the screen. It also changes the price of one product, showing the results on the screen. A copy constructor is used to create a couple of the objects. Overloaded < and == operators are used to compare objects, with the results printed on the screen. Note that one of these operators is a member of the Product class, the other is not (instead it is a friend of the class). Either operator can be set up in either way: member function or friend function. An overloaded << operator is used to do some of the printing. The class's "get" functions are used to extract the values of the three fields of one of the objects, with the results printed on the screen. The overloaded assignment operator is used to copy one object into a couple of others, with the results printed on the screen. Finally, the overloaded << operator is used to write a product's info to a text file stream, try3.txt. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux */ #include #include "product3.h" int main(void) { ShortString Result; fstream OutFile; Product ProductA("toaster, 4 slice", 1997, 91.98); Product ProductB(ProductA); // one way to call the copy constructor Product ProductC = ProductA; // another way to call the copy constructor Product ProductD("mixer, 5 speed", 1998, 32.95); cout << "ProductA:" << endl; ProductA.Print(); cout << "ProductB:" << endl; ProductB.Print(); cout << "ProductC:" << endl << ProductC; // use overloaded << cout << "ProductD:" << endl; ProductD.Print(); cout << "Press ENTER to go on" << endl; cin.get(); if (ProductA < ProductD) cout << endl << "ProductA is less than ProductD in price" << endl; else cout << endl << "ProductA is NOT less than ProductD in price" << endl; if (ProductA == ProductB) cout << "ProductA and ProductB have the same price" << endl << endl; else cout << "ProductA and ProductB do NOT have the same price" << endl << endl; ProductA.ChangePrice(87.95); cout << "After changing the price on ProductA we have:" << endl; ProductA.Print(); // Let's get the values of the three fields individually: ProductA.GetName(Result); cout << "Name of ProductA: " << Result << endl; cout << "Year made for ProductA: " << ProductA.GetYear() << endl; cout << "Price of ProductA: " << ProductA.GetPrice() << endl << endl; cout << "Press ENTER to go on" << endl; cin.get(); // Test of the overloaded assignment operator: ProductB = ProductC = ProductD; cout << "ProductB:" << endl; ProductB.Print(); cout << "ProductC:" << endl; ProductC.Print(); cout << "ProductD:" << endl; ProductD.Print(); // Let's try writing to a text file: OutFile.open("try3.txt", ios::out); if (OutFile.fail()) { cerr << "Could not create file try3.txt" << endl; exit(1); } OutFile << "This is ProductD:" << endl << ProductD; OutFile.close(); return 0; }