C++ The purpose of this programming assignment is to illustrate the concept of p
ID: 3891654 • Letter: C
Question
C++
The purpose of this programming assignment is to illustrate the concept of polymorphism by creating de rived classes that define their own operations in virtual functions. A base class contains a pure virtual function but the actual definitions are contained in the individual derived class functions.
1) Use Programming Project #5 Starter Fi le and either type in or copy and paste it into Visual Studio or the Online C++ compiler.
2) Compile and run the C++ program before the modifications. (Take a screen shot of the output! )
3) Modify the code as is indica ted by each comment section in the program .
4) C ompile and run this program again. Correct any syntax errors. (Take a screen shot of the output! )
5) Write a 1 to 2 paragraph reporting and explaining what you found before and after t he changes made to the program( in other words what is the output did it change or not and why?) You can also include in your 1 to 2 paragraphs explanation (s) about polymorphism and virtual functions and how they work. You may also include in your paragraphs sample code to make your points about Polymorphism and Virtual Functions.
/****************************************************
Explanation / Answer
Given below is the completed code for the question.
Please do rate the answer if it was helpful. Thank you
/****************************************************
*
* FileName: polymorph.cpp
* Author:
* Purpose:
* Demonstrate polymorphism by adding
* a adding a virtual function in a derived class.
*
********************************************************/
#include <iostream>
using namespace std;
/*------------------------------------------------------
Class Name: convert_base
Purpose: Provide a base class with a pure virtual function.
Other classes can be derived from this to
implement polymorphism.
---------------------------------------------------------*/
class convert_base {
protected: // data members for the base class
double initial_value;
double converted_value;
public: // functions for the base class
convert_base(double passed_value) { // constructor
initial_value = passed_value;
}
double get_initial() {
return initial_value;
}
double get_converted() {
return converted_value;
}
virtual void convert_it() = 0; // a PURE virtual function
//
// Remember!! When a descendent
// uses this function, the descendent
// MUST define the function.
}; // end of class convert_base
/*------------------------------------------------------
Class Name: kilos_miles
Purpose: Convert from kilometers to miles.
---------------------------------------------------------*/
class kilos_miles : public convert_base {
public:
kilos_miles(double passed_value) : convert_base(passed_value) { }
// the virtual function, convert_it()
// is defined HERE, because the base
// class contains a PURE function
void convert_it() {
converted_value = initial_value * 0.6;
}
}; // end of class kilos_miles
/*------------------------------------------------------
Class Name: miles_kilos
Purpose: Convert from miles to kilometers.
---------------------------------------------------------*/
class miles_kilos : public convert_base {
public:
miles_kilos(double passed_value) : convert_base(passed_value) { }
// ADD CODE HERE FOR convert_it()
// DIVIDE INITIAL VALUE BY 0.6 TO GET KILOMETERS
void convert_it() {
converted_value = initial_value / 0.6;
}
}; // end of class miles_kilos
/*------------------------------------------------------
Class Name: meters_yards
Purpose: Convert from meters to yards.
---------------------------------------------------------*/
// ADD CODE HERE TO CREATE A CLASS FOR METERS TO YARDS
// MULTIPLY INITIAL VALUE BY 39.0/36.0 TO GET YARDS.
class meters_yards : public convert_base {
public:
meters_yards(double passed_value) : convert_base(passed_value) { }
// ADD CODE HERE FOR convert_it()
// MULTIPLY INITIAL VALUE BY 39.0/36.0 TO GET YARDS.
void convert_it() {
converted_value = initial_value * 39.0/ 36.0;
}
}; // end of class miles_kilos
/*------------------------------------------------------
Class Name: yards_meters
Purpose: Convert from yards to meters.
---------------------------------------------------------*/
// ADD CODE HERE TO CREATE A CLASS FOR YARDS TO METERS
// MULTIPLY INITIAL VALUE BY 36.0/39.0 TO GET METERS.
class yards_meters : public convert_base {
public:
yards_meters(double passed_value) : convert_base(passed_value) { }
// ADD CODE HERE FOR convert_it()
// MULTIPLY INITIAL VALUE BY 36.0/39.0 TO GET METERS.
void convert_it() {
converted_value = initial_value * 36.0/ 39.0;
}
}; // end of class miles_kilos
/***************** Main Function Starts Here ***********/
int main() {
kilos_miles kilos1(100.0); // create a kilos_to_miles object
kilos1.convert_it(); // call the virtual function of that class
cout << endl;
cout << endl << "Distance in kilometers is: " << kilos1.get_initial();
cout << endl << "Distance in miles is: " << kilos1.get_converted();
cout << endl;
/* ADD CODE HERE TO:
1. CREATE A MILES TO KILOS OBJECT, SPECIFYING AN INTIAL VALUE OF 60.
2. CALL THE VIRTUAL FUNCTION FOR THAT OBJECT
3. PRINT OUT THE INITIAL AND CONVERTED VALUES.
NOTE: Refer to the preceding code for kilos to miles for an example. */
/* Complete the coding for this step BEFORE YOU PROCEED FURTHER. */
miles_kilos miles1(60.0); // create a miles_kilos object
miles1.convert_it(); // call the virtual function of that class
cout << endl;
cout << endl << "Distance in miles is: " << miles1.get_initial();
cout << endl << "Distance in kilometers is: " << miles1.get_converted();
cout << endl;
/* ADD CODE HERE TO:
1. USE THE YARDS TO METERS ROUTINE - SPECIFY AN INITIAL VALUE OF 4.
2. PRINT OUT THE INITIAL AND CONVERTED VALUES. */
/* Complete the coding for this step BEFORE YOU PROCEED FURTHER. */
yards_meters yards1(4); // create a yards_meters object
yards1.convert_it(); // call the virtual function of that class
cout << endl;
cout << endl << "Distance in yards is: " << yards1.get_initial();
cout << endl << "Distance in meters is: " << yards1.get_converted();
cout << endl;
/*
3. USE THE METERS TO YARDS ROUTINE - SPECIFY AN INITIAL VALUE OF 4.
4. PRINT OUT THE INITIAL AND CONVERTED VALUES.
See previous code for examples.
*/
meters_yards meters1(4); // create a meters_yards object
meters1.convert_it(); // call the virtual function of that class
cout << endl;
cout << endl << "Distance in meters is: " << meters1.get_initial();
cout << endl << "Distance in yards is: " << meters1.get_converted();
cout << endl;
return(0);
} // end main
output
-----
Distance in kilometers is: 100
Distance in miles is: 60
Distance in miles is: 60
Distance in kilometers is: 100
Distance in yards is: 4
Distance in meters is: 3.69231
Distance in meters is: 4
Distance in yards is: 4.33333
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.