Page 1 of 1

Question 2 Identify suitable coding modules in a simple program that implements polymorphism. In this question you have

Posted: Sat Nov 27, 2021 2:34 pm
by answerhappygod
Question 2 Identify Suitable Coding Modules In A Simple Program That Implements Polymorphism In This Question You Have 1
Question 2 Identify Suitable Coding Modules In A Simple Program That Implements Polymorphism In This Question You Have 1 (43.76 KiB) Viewed 149 times
Question 2 Identify Suitable Coding Modules In A Simple Program That Implements Polymorphism In This Question You Have 2
Question 2 Identify Suitable Coding Modules In A Simple Program That Implements Polymorphism In This Question You Have 2 (11.91 KiB) Viewed 149 times
Question 2 Identify Suitable Coding Modules In A Simple Program That Implements Polymorphism In This Question You Have 3
Question 2 Identify Suitable Coding Modules In A Simple Program That Implements Polymorphism In This Question You Have 3 (9.99 KiB) Viewed 149 times
Question 2 Identify suitable coding modules in a simple program that implements polymorphism. In this question you have to apply operator overloading which is a form of polymorphism. An instance of SimpleMatter can be represented by mass, volume and density. The specification of the three data members are as follows: mass- an integer representing the mass of the SimpleMatter object in g. volume – an integer representing the volume of the SimpleMatter object in cm^3 • density - a float representing the density of the SimpleMatter object in g/cm^3. (Note: density = mass / volume) Addition Two instances of SimpleMatter can be added using the + operator. Consider the following example matter1 = (17g, 27cm^3, 0.6296g/cm^3) matter2 = (999, 48cm^3, 2.0625g/cm^3) matter3 = matterl + matter2 = (116g, 75cm^3, 1.5467g/cm^3) More (heavier) than We can check whether the density (i.e. mass / volume) of a SimpleMatter instance is more than the density of another SimpleMatter instance by using the > operator. Consider the following example: (matteri > matter2) will retum false because the density of matteri (0.6296) is less that the density of matter2 (2.0625).

Implement a C++ class to model SimpleMatter as specified below: Declare the data members mass, volume and density. (3 marks) (6) Write the constructor and destructor. (4 marks) (c) Overload the + operator. (4 marks) (d) Overload the operator. (4 marks)

e Overload the << operator so that the statement cout << matter1 will produce the result (17g, 27cm^3, 0.6296g/cm^3) (5 marks) (f) Write the main fiunction to construct some SimpleMatter instances and demonstrate the use of the +, > and << operators. (5 marks)