Contents
Exercise:
1) What do the following programs output ? Explain.
File: ex4.cpp
#include<iostream> using namespace std; class A { int x; public: void setX(int i) {x = i;} void print() { cout << x; } }; class B: virtual public A { public: B() { setX(10); } }; class C: virtual public A { public: C() { setX(20); } }; class D: public B, public C { }; int main() { D d; d.print(); return 0; }2)
File: ex5.cpp
#include<iostream> using namespace std; class A { int x; public: A(int i) { cout << "A Constructor.\n" ; x = i; } void print() { cout << x; } }; class B: virtual public A { public: B():A(10) { } }; class C: virtual public A { public: C():A(11) { } }; class D: public B, public C { public: D() : A(12) { } }; int main() { D d; d.print(); return 0; }3)
File: ex6.cpp
#include<iostream> using namespace std; class A { int x; public: A() { cout << "A Constructor.\n" ; } virtual void print() { cout << "A's print() method." << endl ; method1() ; } virtual void method1() { cout << "A's method1." << endl ; method2() ; } void method2() { cout << "A's method2." << endl ; } }; class B: public A { public: virtual void print() { cout << "B's print() method." << endl ; method1() ; } void method2() { cout << "B: method2" << endl ; } }; class C: public B { public: void method1() { cout << "C: method1" << endl ; method2() ; } void method2() { cout << "C: method2" << endl ; } }; int main() { A* objectToA = new C(); objectToA->print(); delete objectToA ; return 0; }4) You are tasked with designing a real estate site that sells houses such as "zillow" . Design a hierarchy of classes with data members using inheritance. You can start with the base class of "Property" and then go from there. Do not worry about data methods for now. Derive some classes from "Property" and so on. You can look at the zillow site to see what sort of houses, properties currently exists.
5) Case Study Fast food menu.
File: mac1.cpp
#include <iostream> #include <vector> #include <iomanip> using namespace std; class MenuItem { public: int numberOfItems ; string name = "" ; double price ; std::string to_string_with_precision(double value, int precision) { std::ostringstream oss; oss << std::fixed << std::setprecision(precision) << value; return oss.str(); } MenuItem( string nameP, int numberOfItemsP , double priceP ) { name = nameP ; numberOfItems = numberOfItemsP ; price = priceP ; } virtual string getName() { string formatted_string = to_string_with_precision(price, 2); if ( price == 0 ) return name ; else return name + " " + formatted_string ; } virtual float getPrice() { return price ; } }; class Menu { public: vector< MenuItem* > holderOfItems ; int noOfCols ; //int noItems = 0 ; virtual ~Menu() { for( int i1=0 ; i1< holderOfItems.size() ; i1++ ) { delete holderOfItems[i1] ; } //for } void addItem( MenuItem* ptr) { holderOfItems.push_back( ptr ) ; } virtual void display () { int index = 0 ; int leftItems = holderOfItems.size() ; for ( int i1=0 ; i1<holderOfItems.size() ; ) { int noOfColsCurrentRow = 0 ; if (( leftItems - noOfCols ) > 0 ) { noOfColsCurrentRow = noOfCols ; leftItems -= noOfColsCurrentRow ; } else { noOfColsCurrentRow = leftItems ; leftItems = 0 ; } i1 += noOfColsCurrentRow ; for( int k1=0 ; k1 < noOfColsCurrentRow ; k1++ ) { printf( "%-2d: %-20s ", index+1 , holderOfItems[index]->getName().c_str() ) ; // sprintf( "%2d: %10s ", index , holderOfItems[index]->getName().c_str() ) ; index++ ; } cout << endl ; } //for cout << "Enter your selection:" ; } virtual int getSelection() { int x1 ; cin >> x1 ; return x1 ; } }; class OrderMenu : public Menu { public: OrderMenu() { noOfCols = 2 ; MenuItem* ptrItem = new MenuItem("Burger", 0 , 1 ) ; addItem ( ptrItem ) ; ptrItem = new MenuItem("Fries", 0 , 2 ) ; addItem ( ptrItem ) ; ptrItem = new MenuItem("Drink", 0 , 1.50 ) ; addItem ( ptrItem ) ; ptrItem = new MenuItem("Finish Order", 0 , 0 ) ; addItem ( ptrItem ) ; } void display() { while( true ) { Menu::display() ; int input = getSelection() ; cout << "Selected: " << input << endl ; if ( input == holderOfItems.size() ) { showOrder() ; break ; } else { cout << "Enter number of items:" ; int no = 0 ; cin >> no ; if ( no != 0 ) addToOrder( input, no ) ; } } //while( true ) } //If the customer stated some number for the same item then we //increment. void addToOrder( int inputP, int noP ) { holderOfItems[inputP-1]->numberOfItems += noP ; } void showOrder() { cout << endl << endl ; cout << "Current Order Details." << endl ; float total = 0.0 ; for( int i1=0 ; i1 < holderOfItems.size()-1 ; i1++ ) { if ( holderOfItems[i1]->numberOfItems > 0 ) { cout << holderOfItems[i1]->getName() << " " << holderOfItems[i1]->numberOfItems << endl ; total += holderOfItems[i1]->numberOfItems * holderOfItems[i1]->getPrice() ; } } //for //cout << "Your total is: " << total << endl ; printf( "Your total is %.2f\n" , total ) ; } }; int main () { Menu* OrderMenuObj1 = new OrderMenu() ; OrderMenuObj1->display() ; delete OrderMenuObj1 ; cout << "End of main." << endl ; return 0; }
We are designing a menu for a fast food joint. We have organized the classes as below: Menu Purpose to draw the manu into rows and columns, index each item, get the selected item.Works with a list of MenuItems . OrderMenu derived Menu When the user orders an item ask the user how many items the user would like. Add that selection to the current order. When the order is finished print the order items and the total. MenuItem Represents each item in the menu with price, number of items the customer has ordered, name. The management is introducing a new item "onion rings" and giving a promotion. If you buy from 2 to 5 onion rings you get 20% off and more than 5 you get 50% off. Incorporate this new change in your program. One way is to modify the code in "OrderMenu/ShowOrder" .
File: mac2.cpp
Solutions:
$ g++ ex4.cpp ; ./a.exe 20 $ g++ ex5.cpp ; ./a.exe A Constructor. 12 $ g++ ex6.cpp ; ./a.exe A Constructor. B's print() method. C: method1 C: method2