2010 Lab-15
Lab elements include:
• Polymorphism
• Templates
Getting started --------------- Do your work in your 2010/f directory. Start with the following program. Name it 2010/f/farm.cpp //your name #include <iostream> using namespace std; class Animal { private: string name; public: void makeSound() const { std::cout << "The animal makes a sound." << std::endl; } }; class Cow : public Animal { public: void makeSound() const { std::cout << "Moo Moo - the cow" << std::endl; } }; class Chicken : public Animal { public: void makeSound() const { std::cout << "cluck cluck - the chicken" << std::endl; } }; int main() { return 0; } Follow along with Gordon to write the main() function.Modify the farm program ----------------------- 1. Add one or more animals to your farm. 2. Add an array of animals to your main() function... Animal *farm[3]; You finish the code to display each of your animals making their sound. Hints: Allocate new memory for each of your farm animals. Use a for-loop to play the sounds of each animal. Don't forget to clean-up the allocated memory! If you get an error message when cleaning up the allocated memory, follow the suggestion given by the error message. Here is some sample output..../a.out On the farm... Moo moo - says the cow Cluck cluck - says the chicken Array of animals Moo moo - says the cow Cluck cluck - says the chicken Quack quack - says the duck ??? says the pig ??? says the turkey ??? says the capybara ??? says the crow ??? says the frogWrite a templated function -------------------------- Name your program 2010/f/template_sum.cpp Start with the following program. //your name #include <iostream> using namespace std; int main() { cout << "\nLab-15 template\n" << endl; cout << "sum of integers: " << sum(1, 2, 3) << endl; cout << "sum of chars: " << sum('j', 'k', 'l') << endl; cout << "sum of chars: " << sum<int>('j', 'k', 'l') << endl; cout << "sum of floats: " << sum(1.234f, 2.345f, 3.456f) << endl; return 0; } 1. Write the templated function to make this program work. Your output might look like this..../a.out Lab-15 template sum of integers: 6 sum of chars: A sum of floats: 7.035Why is the sum of 'j', 'k', and 'l' equal to A?Add polymorphism to your mylab14.cpp program -------------------------------------------- Copy your 2010/e/mylab14.cpp to 2010/e/mylab15.cpp. Do this: cd 2010/e cp mylab14.cpp mylab15.cpp Setup Vehicle class pointers to each of your base classes. Maintain your program's original functionality.Programs to be collected... 2010/f/farm.cpp 2010/f/template_sum.cpp 2010/e/mylab15.cpp