/* Filename: try3.cpp Author: Br. David Carlson Date: July 5, 1998 Revised: November 26, 2001 and April 2, 2021 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. The overloaded << operator is used to write a product's info to a text file stream, try3.txt. Finally, the main function calls a helping function named experiment. One parameter is a Product object passed by reference. The other parameter is a Product object passes as a value parameter. This is done to show the difference in what they do. Also, a Product object that is a local variable inside the helping function. Note that, since the Product class has a destructor that prints a message when the destructor is called, this test program shows you where each Product object is destroyed. */ #include #include "product3.h" void experiment(Product & ReferenceParamProduct, Product ValueParamProduct); int main(void) { fstream OutFile; cout << "First we try the constructors and print what each of the 4 objects contains:" << endl; Product ProductA("toaster, 4 slice", 2017, 29.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", 2019, 22.95); cout << "ProductA:" << endl; ProductA.Print(); cout << "ProductB:" << endl; ProductB.Print(); cout << "ProductC:" << endl << ProductC; // use the 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; cout << "Now we try the set functions:" << endl; ProductA.SetName("dishwasher"); ProductA.SetYear(2020); ProductA.SetPrice(289.95); // Let's get the values of the three fields individually: cout << "After changing ProductA to a dishwasher we have:" << endl; cout << "Name of ProductA: " << ProductA.GetName() << endl; cout << "Year made for ProductA: " << ProductA.GetYear() << endl; cout << "Price of ProductA: " << ProductA.GetPrice() << endl << endl; cout << "Press ENTER to go on" << endl << endl; cin.get(); cout << "Testing of the overloaded assignment operator:" << endl; ProductB = ProductC = ProductD; cout << "ProductB:" << endl; ProductB.Print(); cout << "ProductC:" << endl; ProductC.Print(); cout << "ProductD:" << endl; ProductD.Print(); cout << "Test of writing ProductD to the file try3.txt" << endl; 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(); cout << "See if ProductD's data appears in the file try3.txt" << endl; cout << "Press ENTER to go on" << endl << endl; cin.get(); cout << "Let's see how object parameters and local variables behave" << endl; experiment(ProductA, ProductB); cout << "This is the first line in main after returning from function experiment" << endl; cout << "This is the last line before main does its return 0;" << endl; return 0; } /* Given: ReferenceParamProduct - a reference to an existing product in main. * ValueParamProduct - a copy of a product object in main. * Task: Create a local variable, ProductLocal, that is a Product object and * then simply print the data that is in ReferenceParamProduct, * ValueParamProduct, and ProductLocal. */ void experiment(Product & ReferenceParamProduct, Product ValueParamProduct) { Product ProductLocal("Local variable used as a test", 2021, 999.99); cout << endl << "In function experiment, ReferenceParamProduct prints out as:" << endl << ReferenceParamProduct; cout << "ValueParamProduct prints out as: " << ValueParamProduct << endl; cout << "Local variable ProductLocal prints out as:" << endl << ProductLocal; cout << "This is the last line of function experiment" << endl; }