Question: This assignment will give you the opportunity to demonstrate your understanding of the basic principles of polymorphism in Object Oriented Programming. Using the Homework.cpp file
This assignment will give you the opportunity to demonstrate your understanding of the basic principles of polymorphism in Object Oriented Programming.
Using the Homework.cpp file that I demonstrated in our final class meeting as a starting point, you will write a C++ file that exhibits the fundamentals of polymorphism.
Homework.cpp
#include
#include
using namespace std;
//the base class
class Animal
{
public:
Animal()
{
cout <<"Calling the Animal constructor" << endl;
}
void base()
{
cout <<"My base class is animal " << endl << endl;
}
//a virtual function
virtual void speak() = 0;
string getName()
{
return name;
}
void setName(string aName)
{
name = aName;
}
private:
string name;
}; // end of class Animal
// a derived class
class Spider: public Animal
{
public:
Spider(string aName)
{
setName(aName);
}
//this Function overrides the Animal speak() function
void speak()
{
cout << "My name is " << getName() << endl;
}
//the animal base class has no food function
//the food function extends the derived class, makes it more specific
void food()
{
cout << "I eat bugs" << endl;
}
}; //End class Spider
// a derived class
class Sheep: public Animal
{
public:
//this function overrides the animal function
void speak()
{
cout << "Baaaa" << endl;
}
void classification()
{
cout << "I am in the mammal class in the animal kingdom" << endl;
}
}; // end of Sheep class
class Dragon: public Animal
{
public:
//this function overrides the animal function
void speak()
{
cout << "My name is Smaug" << endl;
}
void food()
{
cout << "I eat People" << endl;
}
}; // end of class Dragon
//Test inheritance and polymorphism
int main()
{
Spider arachnid("Charlotte");
arachnid.speak();
arachnid.food();
arachnid.base();
Sheep Dolly;
Dolly.speak();
Dolly.classification();
Dolly.base();
Dragon Smaug;
Smaug.speak();
Smaug.food();
Smaug.base();
return 0;
}
This file will have three class definitions, one base class and two derived classes. The derived classes will have an inheritance relationship (the is a relationship) with the base class. You will use base and derived classes that have not been discussed in class or demonstrated in your textbook. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and private as appropriate. The derived classes will override the virtual function(s) in the base class as necessary. The derived classes extend the base class by having at least one member that the base class does not have. The derived classes will access private members of the base class by using the base classes set and get functions. You will turn in one file.
The name of the file will be Polymorphism.cpp I cannot urge you too strongly to keep your code simple. Once you have a solution that meets the requirements above, stop. Let me remind you of something I said at the beginning of this course - the common misconception is that the project is done when all possible code has been added. This is wrong. With quality code, the project is done when all possible code has been removed and the project still meets requirements.
I will deduct points for unnecessarily complicated code.
So Here is how I wrote mine:
#include
#include
using namespace std;
//the base class
class Media
{
public:
Media()
{
cout <<"Calling the Media constructor" << endl;
}
void base()
{
cout <<"My base class is Media." << endl << endl;
}
//a virtual function
virtual void type() = 0;
string getName()
{
return name;
}
void setName(string aName)
{
name = aName;
}
private:
string name;
}; // end of class Media
// a derived class
class Broadcast: public Media
{
public:
Broadcast(string aName)
{
setName(aName);
}
//this Function overrides the Media type() function
void type()
{
cout << getName() << " is a type of communication derived from the base class." << endl;
}
//the Media base class has no purpose function
//the purpose function extends the derived class, makes it more specific
void purpose()
{
cout << "I report news, fact and fiction, lies and a little bit of truth for your entertainment." << endl;
}
}; //End class Broadcast
// a derived class
class PrintMedia: public Media
{
public:
//this function overrides the Media function
void type()
{
cout << "I print news, fact and fiction, lies and a little truth for your entertainment, in hard copy form." << endl;
}
void classification()
{
cout << "I am in the broadcast class in Media communication." << endl;
}
}; // end of Print class
class CommonCarrier: public Media
{
public:
//this function overrides the Media function
void type()
{
cout << "I am a Common Carrier." << endl;
}
void purpose()
{
cout << "I relate communications via the USPS or the telephone and now the Internet." << endl;
}
}; // end of class Carrier
//Test inheritance and polymorphism
int main()
{
Broadcast Communication("Television");
Communication.type();
Communication.purpose();
Communication.base();
PrintMedia Newspaper;
Newspaper.type();
Newspaper.classification();
Newspaper.base();
CommonCarrier Comcast;
Comcast.type();
Comcast.purpose();
Comcast.base();
return 0;
}
However, I get the "multiple definition of 'main' " error and I know it is because I overloaded the main function but that is what the example show'd. How do I fix this code to make it work with what I am trying to acheive?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
