1 Chapter 8 Composition. 2 Outlines 8.1 Composition versus Inheritance 8.2 Using Composition 8.3 Constructing and Destroying Composed Classes 8.4 Combining.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Review of C++ Programming Part II Sheng-Fang Huang.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
February 11, 2005 More Pointers Dynamic Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 Object-Oriented Programming Using C++ CLASS 11 Honors.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Fundamentals of C++ Yingcai Xiao 09/03/08. Outline Class Definition IO Template vector C Pointer Dynamic Memory Allocation.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Composition OO Software Design and Construction Computer Science Dept Va Tech January 2000 ©2000 McQuain WD 1 Composition: Cooperating Classes Composition.
Learners Support Publications Constructors and Destructors.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Object-Oriented Design (OOD) and C++
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Chapter 5 Classes.
Constructor & Destructor
Pointers and Pointer-Based Strings
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Introduction to Classes
Constructors and Destructors
Introduction to Classes and Objects
Pointers and Pointer-Based Strings
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Types of Computer Languages
Presentation transcript:

1 Chapter 8 Composition

2 Outlines 8.1 Composition versus Inheritance 8.2 Using Composition 8.3 Constructing and Destroying Composed Classes 8.4 Combining Composition and Inheritance 8.5 Case Study: Computer System Configurator

3 8.1 Composition vs. Inheritance Code reusability can be promoted in C++ by establishing relationships between classes. A new kind of relationship between classes called composition. An AND gate is a type of gate, and a gate is a type of logic circuit. Therefore, an AND gate class is derived from a gate class and the gate class is derived from a logic circuit class. Sometimes an object is not a type or subgroup of another object.

4 AND->GATE->LC

5 Composition C++ provides another relationship between classes called composition, which is also known as a has ‑ a relationship. For example, a CPU is not a type of PC, howev­ er, a PC has a CPU as well as many other components. Therefore, a PC class could be composed of a CPU class and some other classes. A new class can be composed from as many existing classes and built ‑ in types as an application requires.

6 Has-a relationship

7 Class declaration To compose a class from existing classes, an object of each class should be declared as a member of the new class. Storage class, which is composed of the three classes Hard_Disk, RAM, and Floppy Class Storage//composed class { float capacity; Hard Disk hdst;//an object of the Hard Disk class RAM ramst;//an object of the RAM class Floppy fpst;//an object of the Floppy class Public: Storage(); ~Storage(); };

8 Composition Classes that are used to build a composed class can be referred to as subclasses and their objects as embedded objects, or subobjects. In addition to the three embedded objects, hdst, ramst, and fpst, the Storage class contains a built ‑ in type as well. The embedded objects are private members of the Storage class. They can be designed as public members as well.

9 8.2 Using Composition 1.//PROG8_1: Program demonstrates a composed class 2.// with embedded objects as public members. 3.#include 4.using namespace std; 5.class Pixel{ //subclass 6. int x, y; //x and y coordinates of a pixel 7.public: 8. Pixel(){ x=0; y=0; } 9. void set(int a, int b) { x=a; y=b; } 10. int getx() const { return x; } 11. int gety() const { return y; } 12.}; 13.class Rectangle{ //composed class as ** public member 14. int perimeter; 15.public: 16. Pixel p1, p2; //embedded objects as public members 17. Rectangle(){ perimeter=0; } 18. void getperim(); 19.};

10 1.void Rectangle::getperim() 2.{ 3. int x1 = p1.getx(); //Calls the embedded objects' 4. int x2 = p2.getx(); //member functions 5. int y1 = p1.gety(); 6. int y2 = p2.gety(); 7. cout<<"Top-left corner coordinates : ["; 8. cout<<x1<<','<<y1<<']'<<endl; 9. cout<<"Bottom-right corner coordinates : ["; 10. cout<<x2<<','<<y2<<']'<<endl; 11. cout<<"Perimeter = "<<(2*(x2-x1)+2*(y2-y1)); 12.} 13.int main() 14.{ 15. Rectangle r; //composed object 16. r.p1.set(5,5); //Using composed object to access 17. r.p2.set(10,10); //members of the embedded objects. 18. r.getperim(); 19. return 0; 20.}

11 Note The security of the subclass's private members is maintained, even if the embedded objects have public access in the composed class. Each level of objects or sub-objects is separated by a dot (.) separator.composed_object.subobject.subobject_m ember The composed class can only access public members of its subclass. Access to the private members of the subclass is limited to the members of that subclass. The public members of the subclass, in this case, cannot be accessed outside of the composed class through the objects of the composed class.

12

13 1.//PROG8_2: Program demonstrates composition with embedded 2.// objects as private members. 3.#include 4.using namespace std; 5.class Pixel { 6. int x, y; 7.public: 8. Pixel(){ x = 0; y = 0; } 9. void set(int a, int b) { x = a; y = b; } 10. int getx() const { return x; } 11. int gety() const { return y; } 12.}; 13.class Rectangle { 14. int perimeter; 15. Pixel p1, p2; //embedded objects as private members 16.public: 17. Rectangle(){ perimeter = 0; } 18. void getperim(); 19.};

14 1.void Rectangle::getperim() 2.{ 3. p1.set(5,5); //Calls the embedded objects' 4. p2.set(10,10); //interface functions 5. int x1 = p1.getx(); 6. int x2 = p2.getx(); 7. int y1 = p1.gety(); 8. int y2 = p2.gety(); 9. cout<<"Top-left corner coordinates : ["; 10. cout<<x1<<','<<y1<<']'<<endl; 11. cout<<"Bottom-right corner coordinates : ["; 12. cout<<x2<<','<<y2<<']'<<endl; 13. cout<<"Perimeter = "<<(2*(x2-x1)+2*(y2-y1)); 14.} 15.int main() 16.{ 17. Rectangle r; //Instantiates a composed object and 18. r.getperim(); //calls its interface function 19. return 0; 20.}

15

16 Note If the embedded objects are created as private members of the composed class, their public interface functions are used to communicate with the composed class. The interface between the classes and the rest of the program in this case is established through the public interface functions of the composed class. It makes the program more maintainable, simplifies its logic, and provides a safer interface. Safer, in this context, means that the embedded objects are hidden from the rest of the program and can only be accessed by the members of its composed class.

17 8.3CONSTRUCTING AND DESTROYING COMPOSED CLASSES If a class is composed from one or more subclasses, the subclass embedded objects will be constructed first when the composed class is instantiated. Use default constructor functions. If a subclass uses a non ‑ default constructor function with arguments, the programmer must define a composed class constructor with a constructor initialization list. If this is not done, the compiler may not be able to construct composed objects.

18 A constructor initialization list should be designed if the subclasses have constructors with arguments. The names of the embedded objects are used instead of the names of the base classes to pass values between constructors.

19 Format: composed ‑ constructor( parameter list ): subobj1(values1),subobj2(values2),...,subobjn(valuesn) { //body of the composed constructor } A parameter list of the composed class constructor should include all of the arguments needed to initialize the data members of the composed class, as well as the arguments needed to initialize its embedded objects.

20 Example Rectangle(int a, int b, int c, int d) : p1(a, b), p2(c, d) { perimeter = 0; } The constructors needed to initialize the p1 and p2 sub- objects are called before the execution of the body of the composed class constructor. It is logical because it is necessary to build the sub- objects first in order to build an overall composed class object. The order of the constructor calls is not affected by the order of the embedded objects in the constructor initialization list.

21 The same syntax can also be used if a class is composed of built ‑ in types, class Circuit { char * type; int quantity; float price; public: Circuit(char *t, int q, float p): type(t), quantity(q), price(p) };

22 1.//PROG8_3: Program demonstrates the order of the constructor and 2.// destructor calls when constructing and destroying a 3.// composed object. 4.#include 5.using namespace std; 6.class Speaker { //subclass #1 7. float impedance; 8.public: 9. Speaker(float imp) //constructor #1 10. { 11. impedance = imp; 12. cout<<"Constructing speaker."<<endl; 13. } 14. float getimp()const { return impedance; } 15. ~Speaker() //destructor #1 16. { 17. cout<<"Destroying speaker."<<endl; 18. } 19.};

23 1.class Amplifier { //subclass #2 2. float impedance; 3.public: 4. Amplifier(float imp ) //constructor #2 5. { 6. impedance = imp; 7. cout<<"Constructing amplifier."<<endl; 8. } 9. float getimp()const { return impedance;} 10. ~Amplifier() //destructor #2 11. { 12. cout<<"Destroying amplifier."<<endl; 13. } 14.};

24 1.class Stereo { //composed class 2. Speaker sp; //embedded object #1 3. Amplifier amp; //embedded object #2 4.public: 5. Stereo(float x, float y):sp(x), amp(y) 6. { //composed constructor 7. cout<<"Constructing stereo."<<endl; 8. } 9. void matching() //Compares the impedances 10. { 11. if(sp.getimp()==amp.getimp()) 12. cout<<"Impedances are matched."<<endl; 13. else 14. cout<<"Impedances are not matcheed."<<endl; 15. } 16. ~Stereo() //composed destructor 17. { 18. cout<<"Destroying stereo."<<endl; 19. } 20.};

25 1.main() 2.{ 3. Stereo st(8, 8); //composed object 4. st.matching(); 5. return 0; 6.}

26 Please note that the output of PROG8 3 will not change even if the order of the embedded objects is changed in the constructor initialization list as follows:; Stereo(float x, float y): amp(x), sp(y) { cout«"Constructing stereo."« endl; }

27 A constructor of a composed class can also take objects as arguments. Doing this results in a much shorter list of arguments, whereas the constructors of the embedded objects require many individual arguments. A composed class can contain pointers to sub- objects as members. This class may use its constructor function to dynamically allocate sub-objects and store their addresses in the member pointers. Note that the composed class destructor must free memory dynamically allocated by the constructor.

28 1.//PROG8_4: Program demonstrates a composed class containing 2.// pointers to subobjects as members. 3.#include 4.using namespace std; 5.class Speaker { //subclass #1 6. float impedance; 7.public: 8. Speaker() //constructor #1 9. { 10. cout<<"Constructing speaker."<<endl; 11. } 12. void setimp(float imp) { impedance=imp; } 13. float getimp()const { return impedance; } 14. ~Speaker() //destructor #1 15. { 16. cout<<"Destroying speaker."<<endl; 17. } 18.};

29 1.class Amplifier { //subclass #2 2. float impedance; 3.public: 4. Amplifier() //constructor #2 5. { 6. cout<<"Constructing amplifier."<<endl; 7. } 8. void setimp(float imp) { impedance=imp; } 9. float getimp()const { return impedance; } 10. ~Amplifier() //destructor #2 11. { 12. cout<<"Destroying amplifier."<<endl; 13. } 14.};

30 1.class Stereo { //composed class 2. Speaker *ptsp; //pointer to subobject #1 3. Amplifier *ptamp; //pointer to subobject #2 4.public: 5. Stereo(Speaker &s, Amplifier &a) 6. { //composed constructor 7. ptsp=new Speaker; 8. ptamp=new Amplifier; 9. *ptsp=s; 10. *ptamp=a; 11. cout<<"\tConstructing stereo."<<endl; 12. } 13. void matching() //Compares the impedances 14. { 15. if(ptsp->getimp()==ptamp->getimp()) 16. cout<<"Impedances are matched."<<endl; 17. else 18. cout<<"Impedances are not matcheed."<<endl; 19. }

31 1. ~Stereo() //composed destructor 2. { 3. delete ptsp; 4. delete ptamp; 5. cout<<"\tDestroying stereo."<<endl; 6. } 7.}; 8.int main() 9.{ 10. Amplifier a; 11. a.setimp(8); 12. Speaker s; 13. s.setimp(8); 14. Stereo st(s, a); //composed object 15. st.matching(); 16. return 0; 17.}

32 Output

COMBINING INHERITANCE AND COMPOSITION

34 1.//PROG8_5: Program demonstrates a use of inheritance //and 2.//composition combined together to build a complex class. 3.#include 4.using namespace std; 5.class Pixel { //subclass 6. int x, y; 7.public: 8. Pixel(int a, int b) //constructor 9. { 10. x = a; 11. y = b; 12. cout<<"SubConstructor"<<" x="<<x<<" y="<<y<<endl; 13. } 14. ~Pixel(){cout<<"SubDestructor"<<endl;} //destructor 15.};

35 1.class RecShape { //base class protected: 2. int lg, wd; 3.public: 4. RecShape(int l, int w) //constructor 5. { 6. lg = l; 7. wd = w; 8. cout<<"BaseConstructor"<<" lg="<<lg<<" wd="<<wd<<endl; 9. } 10. ~RecShape(){cout<<"BaseDestructor"<<endl;} //destructor 11.};

36 1.class Rectangle:public RecShape { //derived & composed class 2. int perimeter; 3. Pixel p1, p2; //subobjects 4.public: 5. Rectangle(int x1,int y1,int x2,int y2): //constructor 6. RecShape(x2-x1,y2-y1),p1(x1,y1),p2(x2,y2) 7. { 8. perimeter = 0; 9. cout<<"CombConstructor"<<" x1="<<x1<<" y1="<<y1; 10. cout<<" x2="<<x2<<" y2="<<y2<<endl; 11. } 12. void getperim() 13. { 14. cout<<"Perimeter = "<<(2*lg + 2*wd)<<endl; 15. } 16. ~Rectangle(){cout<<"CombDestructor"<<endl;} //destructor 17.};

37 int main() { Rectangle r(5,5,10,10); r.getperim(); return 0; }

38

39

40

41

42 CASE STUDY: COMPUTER SYSTEM CONFIGURATOR INPUT/OUTPUT Input: – A selection of parts from the menu options offered –A choice of one of the following options: Display the current system configuration Change a part Exit the program output: –A list of all the parts selected, including the cost of each part and the total cost.

43 //CASEST8: The program demonstrates a practical use of // composition to configure a computer system. #include using namespace std; class CPU //Subclass CPU { char *bcpu[3]; //CPU options float pcpu[3]; //costs of CPU options public: CPU() { //CPU constructor bcpu[0]="Pentium III 866 MHz "; bcpu[1]="Pentium IV 1.4 GHz "; bcpu[2]="Pentium IV 2.0 GHz "; pcpu[0]=150; pcpu[1]=270; pcpu[2]=450; } friend ostream &operator <<(ostream &out, CPU &a); void select_cpu(char *temp[1], float &cost); };

44 ostream &operator <<(ostream &out, CPU &a) { out<<"\n\n CPU \t\t\t\tCOST\n" ; out<<" \t\t\t \n"; for(int i = 0; i<3; i++) { out<<setiosflags(ios::left | ios::fixed)<<setprecision(2); out<<(i+1)<<") "<<setw(64)<<a.bcpu[i]; out<<"\t$"<<a.pcpu[i]<<endl; out<<'\n'; } return out; } void CPU::select_cpu(char *temp[1], float &cost) //Selects a CPU { int select; cout<<'\n'; do{ cout "; cin>>select; }while((select 3)); temp[0] = bcpu[select-1]; cost = pcpu[select-1]; }

45 class RAM { //Subclass RAM char *bmem[3]; //RAM options float pmem[3]; //costs of RAM options public: RAM() { //RAM constructor bmem[0]="64 MB "; bmem[1]="128 MB "; bmem[2]="256 MB "; pmem[0]=39.99; pmem[1]=68; pmem[2]=152; } friend ostream &operator <<(ostream &out, RAM b); void select_ram(char *temp[1], float &cost); }; ostream &operator <<(ostream &out, RAM b) //Displays RAM options { out<<"\n\n RAM \t\t\t\tCOST\n" ; out<<" \t\t\t \n"; for(int i=0; i<3; i++) { out<<setiosflags(ios::left | ios::fixed)<<setprecision(2); out<<(i+1)<<")"<<setw(62)<<b.bmem[i]; out<<"\t$"<<b.pmem[i]<<endl; out<<'\n'; } return out; }

46 void RAM::select_ram(char *temp[1], float &cost) //Selects a RAM { int select; cout<<"\n"; do{ cout "; cin>>select; }while((select 3)); temp[0] = bmem[select-1]; cost = pmem[select-1]; } class Hard_Drive { //Subclass Hard_Drive char *bhd[4]; float phd[4]; public: Hard_Drive() { bhd[0]="10.0 GB "; bhd[1]="15.3 GB"; bhd[2]="30.0 GB"; bhd[3]="40.0 GB"; phd[0]=110; phd[1]=124; phd[2]=174; phd[3]=220; } friend ostream &operator <<(ostream &out, Hard_Drive &c); void select_hdrive(char *temp[1], float &cost); };

47 ostream &operator <<(ostream &out, Hard_Drive &c) { out<<"\n\n HARD DRIVE \t\t\t\tCOST\n" ; out<<" \t\t\t \n"; for(int i=0; i<4; i++) { out<<setiosflags(ios::left | ios::fixed)<<setprecision(2); out<<(i+1)<<") "<<setw(62)<<c.bhd[i]; out<<"\t$"<<c.phd[i]<<endl; out<<'\n'; } return out; } void Hard_Drive::select_hdrive(char *temp[1], float &cost) { int select; cout<<'\n'; do{ cout "; cin>>select; }while((select 4)); temp[0] = bhd[select-1]; cost = phd[select-1]; }

48 class PC { //Composed class char *parts[3]; //parts selected by the user float pcost[3]; //costs of the selected parts float total; //total cost of the system CPU cpu; //subobjects RAM ram; Hard_Drive hd; public: PC(); void display_cpu() { cout<<cpu; cpu.select_cpu(& parts[0], pcost[0]); } void display_mem() { cout<<ram; ram.select_ram(& parts[1], pcost[1]); } void display_hdrive() { cout<<hd; hd.select_hdrive(& parts[2], pcost[2]); } friend ostream &operator <<(ostream &out, PC a); };

49 PC::PC() //PC constructor { total=0; for(int i=0;i<3;i++) { parts[i] = "Not Selected"; pcost[i] = 0; } ostream &operator <<(ostream &out, PC a) { //Displays the system chosen and total cost for(int i=0;i<3;i++) a.total = a.total + a.pcost[i]; out<<setiosflags(ios::left | ios::fixed)<<setprecision(2); out<<" PART TYPE \t\t\tCOST\n "; out<<" \t\t\t------"<<endl; out<<'\n'<<setw(15)<<"CPU:"<<setw(54)<<a.parts[0]; out<<"\t$"<<a.pcost[0]<<endl; out<<'\n'<<setw(15)<< "RAM:"<<setw(54)<<a.parts[1]; out<<"\t$"<<a.pcost[1]<<endl; out<<'\n'<<setw(15)<<"Hard drive:"<<setw(54)<<a.parts[2]; out<<"\t$"<<a.pcost[2]<<endl; out $"; out<<a.total<<endl; return out ; }

50 char menu() { char selection; cout<<"\n\n\n \t\t\t Computer System "<<endl; cout<<"\t\t \n\n"; cout<<"\t\ta) Select CPU \n"; cout<<"\t\tb) Select RAM \n"; cout<<"\t\tc) Select Hard Drive\n"; cout<<"\t\td) Display the system and total cost\n"; cout<<"\t\tx) Exit\n\n"; cout "; cin>> selection; return selection; }

51 int main() { char selection; PC computer; //Composed object do { selection=menu(); switch(selection) { case 'a': computer.display_cpu(); break; case 'b': computer.display_mem(); break; case 'c': computer.display_hdrive(); break; case 'd': cout<<" The system chosen : \n\n"; cout<<computer; break; case 'x': cout<<"\n\n\n\t\tEnd of program."; break; default: cout<<"\n\t Incorrect input! "; } } while(selection!='x'); return 0; }

52

53