Topics: Polymorphism, Virtual functions, vectors 1. Create an anonymous class ca
ID: 3555868 • Letter: T
Question
Topics: Polymorphism, Virtual functions, vectors
1. Create an anonymous class called Entertainment. This class should have a method called getinfo that will be overridden by any derived class and an attribute string called TypeofEntertainment
2. Create three more classes that are derived from the class Entertainment. Their names should be: TV, Movie, BallGame. Each class should be derived from the Entertainment class and should overwrite the getinfo function.
a) TV class will have members: showName, Time, and channel. Write a parameterized constructor and the getinfo() function for this class.
b) Movie class will have members: movieName, rating, time, and theater. Write a parameterized constructor and the getinfo() function for this class.
c) BallGame class will have members: typeOfSport, and teamName. Write a parameterized constructor and the getinfo() function for this class.
3. Write a main program that prompts the user for his or her choice of entertainment. (You could use integers (1-3) or chars (a-c). Create an object that has the user
Explanation / Answer
/*
* Main.cpp
*
* Created on: Apr 29, 2014
* Author:
*/
#include <vector>
#include <string>
#include <iostream>
using namespace std;
void print(vector<string>& v){
cout <<" Elements of vector are" << endl;
for (vector<string>::iterator it = v.begin(); it != v.end(); ++it)
cout << *it << endl;
cout << endl;
}
void append(vector<string>& v1, vector<string>& v2){
v1.insert( v1.end(), v2.begin(), v2.end() );
}
int main() {
vector<string> v1 = vector<string>(5, "first vector");
vector<string>::iterator it;
vector<string> v2 ;
vector<string>::iterator it2;
it2 = v2.begin();
it2 = v2.insert(v2.end(), "2-first");
it2 = v2.insert(v2.end(), "2-second");
it2 = v2.insert(v2.end(), "2-third");
print(v1);
print(v2);
append(v1, v2);
print(v1);
return 0;
}
//============================================================================
// Name : VirtualFunctions.cpp
// Author :
// Version :
// Copyright :
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
using namespace std;
class Entertainment {
protected:
string TypeofEntertainment;
public:
Entertainment() {
TypeofEntertainment = "";
}
virtual void getinfo()=0;
};
class TV: public Entertainment {
string showName;
int rating;
string time;
string channel;
public:
TV(const string & showName, int rating, string const& time, string const& channel) {
TypeofEntertainment = "TV Show";
this->showName = showName;
this->rating = rating;
this->time = time;
this->channel = channel;
}
void getinfo(void) {
cout << "Type of Entertainment : " << TypeofEntertainment << endl;
cout << "ShowName : " << showName << endl;
cout << "Rating : " << rating << endl;
cout << "Time : " << time << endl;
cout << "Channel : " << channel << endl;
}
};
class Movie: public Entertainment {
string movieName;
int rating;
string time;
string theater;
public:
Movie(const string & movieName, int rating, string const& time, string const& theater) {
TypeofEntertainment = "Movie";
this->movieName = movieName;
this->rating = rating;
this->time = time;
this->theater = theater;
}
void getinfo(void) {
cout << "Type of Entertainment : " << TypeofEntertainment << endl;
cout << "MovieName : " << movieName << endl;
cout << "Rating : " << rating << endl;
cout << "Time : " << time << endl;
cout << "Theater : " << theater << endl;
}
};
class BallGame: public Entertainment {
string typeOfSport;
string teamName;
public:
BallGame(string sportType, string teamnam) {
TypeofEntertainment = "BallGame";
typeOfSport = sportType;
teamnam = teamName;
}
void getinfo(void) {
cout << "Type of Entertainment : " << TypeofEntertainment << endl;
cout << "Sport type : " << typeOfSport << endl;
cout << "Team Name : " << teamName << endl;
}
};
void PrintChoice(Entertainment *e){
cout << "Your Choice is " <<endl;
e->getinfo();
}
int main() {
cout << "1. TV Show" << endl;
cout << "2. Movie" << endl;
cout << "3. Sport" << endl;
cout << "Enter your choice: ";
int ch = 0;
cin >> ch;
Entertainment *e = NULL;
string tvshow, time, channel, theater, sportname, sporttype, moviename;
int rating;
switch(ch){
case 1 :
cout << "Enter TV Show name : " ;
cin >> tvshow;
cout << "Enter rating : " ;
cin >> rating;
cout << "Enter Time of show : " ;
cin >> time;
cout << "Enter the channel: ";
cin >> channel;
e = new TV(tvshow, rating, time, channel);
break;
case 2 :
cout << "Enter movie name : " ;
cin >> moviename;
cout << "Enter rating : " ;
cin >> rating;
cout << "Enter Time of show : " ;
cin >> time;
cout << "Enter the theater: ";
cin >> theater;
e = new Movie(tvshow, rating, time, theater);
break;
case 3 :
cout << "Enter type of sport: " ;
cin >> sporttype;
cout << "Enter sportname : " ;
cin >> sportname;
e = new BallGame(sporttype, sportname);
break;
default:
cout << "Wrong Choice" << endl;
break;
}
PrintChoice(e);
return 0;
}