C++ 由于某些原因,只能将一个项目插入到我的队列中,而我的显示功能不';行不通
我需要创建一个队列,其中包含从父类(媒体)派生的两种类型的对象(视频和音乐)。我的打印功能不工作,我的排队功能也不工作 这是保存媒体类的文件。“媒体”是“视频和音乐”两个子类的父类C++ 由于某些原因,只能将一个项目插入到我的队列中,而我的显示功能不';行不通,c++,queue,C++,Queue,我需要创建一个队列,其中包含从父类(媒体)派生的两种类型的对象(视频和音乐)。我的打印功能不工作,我的排队功能也不工作 这是保存媒体类的文件。“媒体”是“视频和音乐”两个子类的父类 #include <string> #include <iostream> using namespace std; #ifndef Lab6Final_Media_h #define Lab6Final_Media_h class Media { public: //
#include <string>
#include <iostream>
using namespace std;
#ifndef Lab6Final_Media_h
#define Lab6Final_Media_h
class Media
{
public:
//public member functions
Media(){}// Class constructor
string getTitle(); //returns the title
int getLength(); //returns the length
void setTitle(string title);
void setLength(float length);
virtual void display(){} //prints the title and length info*/
protected:
float length;
string title;
};
void Media::setTitle(string input)
{
title = input;
}
void Media::setLength(float input)
{
length = input;
}
string Media::getTitle()
{
return title;
}
int Media::getLength()
{
return length;
}
#endif
#包括
#包括
使用名称空间std;
#ifndef LAB6最终媒体
#定义Lab6Final_Media_h
班级媒体
{
公众:
//公职人员职能
媒体(){}//类构造函数
字符串getTitle();//返回标题
int getLength();//返回长度
无效设置标题(字符串标题);
空隙设置长度(浮动长度);
virtual void display(){}//打印标题和长度信息*/
受保护的:
浮子长度;
字符串标题;
};
void Media::setTitle(字符串输入)
{
标题=输入;
}
无效介质::设置长度(浮点输入)
{
长度=输入;
}
字符串媒体::getTitle()
{
返回标题;
}
int媒体::getLength()
{
返回长度;
}
#恩迪夫
此文件保存子类音乐
#include <iostream>
#include "Media.h"
#include <string>
using namespace std;
#ifndef Lab6Final_Music_h
#define Lab6Final_Music_h
class Music: public Media
{
public:
Music();
void display();
};
Music::Music()
{
}
void Music::display()
{
cout<<"Song: "<<title<<" "<< length;
}
#endif
#包括
#包括“Media.h”
#包括
使用名称空间std;
#ifndef LAB6最终音乐
#定义LAB6最终音乐
课堂音乐:公共媒体
{
公众:
音乐();
void display();
};
音乐
{
}
void Music::display()
{
cout在这里,我有一系列类可以满足您的要求。Media类是一种抽象类型,音乐和视频都是从中派生出来的。有一个队列类只执行队列应该执行的操作。然后我引入了一个新的类MediaManager,它基本上是队列成员变量的包装器,但您可以还向该类添加了其他管理媒体对象的功能,而无需修改队列类。AudioManager无需实现队列对象或容器的结构和功能。我还重新构造了头文件保护,将其定义为文件名本身,但我封装了所有与命名空间中的实验室工作相关的类。我不建议在全局空间甚至在主函数中使用#命名空间std;
的概念!这被认为是一种不好的做法,因为它会在以后引起很多麻烦,所以任何必须处理标准库的内容我都会使用范围解析为我还使用了诸如C++代码的概念,如 NulLPTR < /C> >空指针,对于仅包含1个参数的构造函数,或者具有多个参数的构造函数,默认值可以导致单个参数被传入,<代码>密封< /代码>在不会继承的类名之后,重写
从抽象数据类型继承的派生类中的纯虚方法、成员初始值设定项列表(如果适用)以及许多其他方法
stdafx.h
#ifndef STDAFX_H
#define STDAFX_H
#include <tchar.h>
#include <conio.h>
#include <memory>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
}; // ReturnCode
#endif // STDAFX_H
#ifndef MEDIA_H
#define MEDIA_H
namespace lab6 {
class Media {
public:
enum MediaType {
MT_MUSIC = 0,
MT_VIDEO,
}; // MediaType
protected:
std::string m_title;
float m_length;
private:
MediaType m_type;
public:
void setTitle( const std::string& title );
std::string getTitle() const;
void setLength( float length );
float getLength() const;
MediaType getType() const;
virtual void display() = 0; // Purely Virtual Each Derived Class Must Implement Their Own
protected:
explicit Media( MediaType type ); // Protected Constructor
Media( MediaType, const std::string& strTitle, float length = 0 );
}; // Media
} // namespace lab6
#endif // MEDIA_H
#ifndef MUSIC_H
#define MUSIC_H
#include "Media.h"
namespace lab6 {
class Music : public Media {
public:
Music();
explicit Music( const std::string& strTitle, float length = 0 );
virtual void display() override;
}; // Music
} // namespace lab6
#endif // MUSIC_H
#ifndef QUEUE_H
#define QUEUE_H
namespace lab6 {
// ----------------------------------------------
// Queue - Cicular Array Style Queue
template<class T>
class Queue {
public:
static const int s_maxSize = 100;
private:
T m_queue[s_maxSize];
int m_front;
int m_back;
public:
Queue();
T front();
T back();
void push( T item );
void pop();
bool isEmpty() const;
bool isFull() const;
}; // Queue
#include "Queue.inl"
} // namespace lab6
#endif // QUEUE_H
#ifndef MEDIA_MANAGER_H
#define MEDIA_MANAGER_H
#include "Queue.h"
namespace lab6 {
class Media;
class MediaManager sealed {
private:
Queue<Media*> m_queue;
public:
MediaManager();
void addToQueue( Media* item );
void removeFromQueue();
Media* getFront();
Media* getBack();
bool isEmpty() const;
bool isFull() const;
void displayQueue();
}; // MediaManager
} // namespace lab6
#endif // MEDIA_MANAGER_H
Media.h
#ifndef STDAFX_H
#define STDAFX_H
#include <tchar.h>
#include <conio.h>
#include <memory>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
}; // ReturnCode
#endif // STDAFX_H
#ifndef MEDIA_H
#define MEDIA_H
namespace lab6 {
class Media {
public:
enum MediaType {
MT_MUSIC = 0,
MT_VIDEO,
}; // MediaType
protected:
std::string m_title;
float m_length;
private:
MediaType m_type;
public:
void setTitle( const std::string& title );
std::string getTitle() const;
void setLength( float length );
float getLength() const;
MediaType getType() const;
virtual void display() = 0; // Purely Virtual Each Derived Class Must Implement Their Own
protected:
explicit Media( MediaType type ); // Protected Constructor
Media( MediaType, const std::string& strTitle, float length = 0 );
}; // Media
} // namespace lab6
#endif // MEDIA_H
#ifndef MUSIC_H
#define MUSIC_H
#include "Media.h"
namespace lab6 {
class Music : public Media {
public:
Music();
explicit Music( const std::string& strTitle, float length = 0 );
virtual void display() override;
}; // Music
} // namespace lab6
#endif // MUSIC_H
#ifndef QUEUE_H
#define QUEUE_H
namespace lab6 {
// ----------------------------------------------
// Queue - Cicular Array Style Queue
template<class T>
class Queue {
public:
static const int s_maxSize = 100;
private:
T m_queue[s_maxSize];
int m_front;
int m_back;
public:
Queue();
T front();
T back();
void push( T item );
void pop();
bool isEmpty() const;
bool isFull() const;
}; // Queue
#include "Queue.inl"
} // namespace lab6
#endif // QUEUE_H
#ifndef MEDIA_MANAGER_H
#define MEDIA_MANAGER_H
#include "Queue.h"
namespace lab6 {
class Media;
class MediaManager sealed {
private:
Queue<Media*> m_queue;
public:
MediaManager();
void addToQueue( Media* item );
void removeFromQueue();
Media* getFront();
Media* getBack();
bool isEmpty() const;
bool isFull() const;
void displayQueue();
}; // MediaManager
} // namespace lab6
#endif // MEDIA_MANAGER_H
Media.cpp
#include "stdafx.h"
#include "stdafx.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type ) :
m_type( type ),
m_length( 0 ) {
} // Media
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type, const std::string& strTitle, float length ) :
m_type( type ),
m_title( strTitle ),
m_length( length ) {
} // Media
// ----------------------------------------------------------------------------
// getType()
Media::MediaType Media::getType() const {
return m_type;
} // getType
// ----------------------------------------------------------------------------
// setTitle()
void Media::setTitle( const std::string& title ) {
m_title = title;
} // setTitle
// ----------------------------------------------------------------------------
// getTitle()
std::string Media::getTitle() const {
return m_title;
} // getTitle
// ----------------------------------------------------------------------------
// setLength()
void Media::setLength( float length ) {
m_length = length;
} // setLength
// ----------------------------------------------------------------------------
// getLength()
float Media::getLength() const {
return m_length;
} // getLength
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Music()
Music::Music() :
Media( MT_MUSIC ) {
} // Music
// ----------------------------------------------------------------------------
// Music()
Music::Music( const std::string& strTitle, float length ) :
Media( MT_MUSIC, strTitle, length ) {
} // Music
// ----------------------------------------------------------------------------
// display()
void Music::display() {
std::cout << "Song: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Video.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Video()
Video::Video() :
Media( MT_VIDEO ) {
} // Video
// ----------------------------------------------------------------------------
// Video()
Video::Video( const std::string& strTitle, float length ) :
Media( MT_VIDEO, strTitle, length ) {
} // Video
// ----------------------------------------------------------------------------
// display()
void Video::display() {
std::cout << "Video: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Queue.h"
namespace lab6 {
} // namespace lab6
#include "stdafx.h"
#include "MediaManager.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// MediaManager()
MediaManager::MediaManager() {
} // MediaManager
// ----------------------------------------------------------------------------
// addToQueue()
void MediaManager::addToQueue( Media* item ) {
m_queue.push( item );
} // addToQueue
// ----------------------------------------------------------------------------
// removeFromQueue()
void MediaManager::removeFromQueue() {
m_queue.pop();
} // removeFromQueue
// ----------------------------------------------------------------------------
// isEmpty()
bool MediaManager::isEmpty() const {
return m_queue.isEmpty();
} // isEmpty()
// ----------------------------------------------------------------------------
// isFull()
bool MediaManager::isFull() const {
return m_queue.isFull();
} // isFull
// ----------------------------------------------------------------------------
// getFront()
Media* MediaManager::getFront() {
return dynamic_cast<Media*>( m_queue.front() );
} // getFront
// ----------------------------------------------------------------------------
// getBack()
Media* MediaManager::getBack() {
return dynamic_cast<Media*>( m_queue.back() );
} // getBack
// ----------------------------------------------------------------------------
// displayQueue() -- Written For Array Style
void MediaManager::displayQueue() {
if ( isEmpty() ) {
std::cout << "The Queue is empty!/n";
return;
}
while ( !m_queue.isEmpty() ) {
m_queue.front()->display();
m_queue.pop();
}
} // displayQueue
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
#include "Video.h"
#include "MediaManager.h"
int main() {
using namespace lab6;
try {
// Create MediaManager First
std::unique_ptr<MediaManager> pMediaManager;
pMediaManager.reset( new MediaManager );
// Create A Few Media Items
Music m1( "Wouldn't it Be Nice", 3.40f );
Music m2( "My Eyes Adore You", 4.20f );
Music m3( "Comfortably Numb", 6.8f );
Video v1( "Shawshank Redemption", 142.5f );
Video v2( "Matrix", 148.2f );
Video v3( "Lord of the Rings: Fellowship Of the Ring", 190.5f );
Video v4( "Lord of the Rings: The Two Towers", 188.4f );
Video v5( "Lord of the Rings: Return of the King", 191.2f );
// Now Add these to the Manager which encapsulates the queue
pMediaManager->addToQueue( dynamic_cast<Media*>( &v3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v4 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v5 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v1 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m1 ) );
// Now as A Test Will Get Both The Front Then The Back
std::cout << "Display Front & Back Of Queue" << std::endl;
pMediaManager->getFront()->display();
pMediaManager->getBack()->display();
std::cout << std::endl << std::endl;
// Now Will Will Show Full Queue Which Will Also Pop The Queue As It Goes Through the List
std::cout << "Display Entire Queue: " << std::endl;
pMediaManager->displayQueue();
std::cout << std::endl << std::endl;
// Now Our Queue Should Be Empty
std::cout << "Is Queue Empty: " << std::endl;
std::cout << (pMediaManager->isEmpty() ? "true" : "false") << std::endl;
std::cout << std::endl << std::endl;
pMediaManager.reset();
} catch( const std::string& str ) {
std::cout << "Exception Thrown: " << str << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
} catch( ... ) {
std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
}
return RETURN_OK;
} // main
音乐.h
#ifndef STDAFX_H
#define STDAFX_H
#include <tchar.h>
#include <conio.h>
#include <memory>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
}; // ReturnCode
#endif // STDAFX_H
#ifndef MEDIA_H
#define MEDIA_H
namespace lab6 {
class Media {
public:
enum MediaType {
MT_MUSIC = 0,
MT_VIDEO,
}; // MediaType
protected:
std::string m_title;
float m_length;
private:
MediaType m_type;
public:
void setTitle( const std::string& title );
std::string getTitle() const;
void setLength( float length );
float getLength() const;
MediaType getType() const;
virtual void display() = 0; // Purely Virtual Each Derived Class Must Implement Their Own
protected:
explicit Media( MediaType type ); // Protected Constructor
Media( MediaType, const std::string& strTitle, float length = 0 );
}; // Media
} // namespace lab6
#endif // MEDIA_H
#ifndef MUSIC_H
#define MUSIC_H
#include "Media.h"
namespace lab6 {
class Music : public Media {
public:
Music();
explicit Music( const std::string& strTitle, float length = 0 );
virtual void display() override;
}; // Music
} // namespace lab6
#endif // MUSIC_H
#ifndef QUEUE_H
#define QUEUE_H
namespace lab6 {
// ----------------------------------------------
// Queue - Cicular Array Style Queue
template<class T>
class Queue {
public:
static const int s_maxSize = 100;
private:
T m_queue[s_maxSize];
int m_front;
int m_back;
public:
Queue();
T front();
T back();
void push( T item );
void pop();
bool isEmpty() const;
bool isFull() const;
}; // Queue
#include "Queue.inl"
} // namespace lab6
#endif // QUEUE_H
#ifndef MEDIA_MANAGER_H
#define MEDIA_MANAGER_H
#include "Queue.h"
namespace lab6 {
class Media;
class MediaManager sealed {
private:
Queue<Media*> m_queue;
public:
MediaManager();
void addToQueue( Media* item );
void removeFromQueue();
Media* getFront();
Media* getBack();
bool isEmpty() const;
bool isFull() const;
void displayQueue();
}; // MediaManager
} // namespace lab6
#endif // MEDIA_MANAGER_H
音乐.cpp
#include "stdafx.h"
#include "stdafx.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type ) :
m_type( type ),
m_length( 0 ) {
} // Media
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type, const std::string& strTitle, float length ) :
m_type( type ),
m_title( strTitle ),
m_length( length ) {
} // Media
// ----------------------------------------------------------------------------
// getType()
Media::MediaType Media::getType() const {
return m_type;
} // getType
// ----------------------------------------------------------------------------
// setTitle()
void Media::setTitle( const std::string& title ) {
m_title = title;
} // setTitle
// ----------------------------------------------------------------------------
// getTitle()
std::string Media::getTitle() const {
return m_title;
} // getTitle
// ----------------------------------------------------------------------------
// setLength()
void Media::setLength( float length ) {
m_length = length;
} // setLength
// ----------------------------------------------------------------------------
// getLength()
float Media::getLength() const {
return m_length;
} // getLength
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Music()
Music::Music() :
Media( MT_MUSIC ) {
} // Music
// ----------------------------------------------------------------------------
// Music()
Music::Music( const std::string& strTitle, float length ) :
Media( MT_MUSIC, strTitle, length ) {
} // Music
// ----------------------------------------------------------------------------
// display()
void Music::display() {
std::cout << "Song: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Video.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Video()
Video::Video() :
Media( MT_VIDEO ) {
} // Video
// ----------------------------------------------------------------------------
// Video()
Video::Video( const std::string& strTitle, float length ) :
Media( MT_VIDEO, strTitle, length ) {
} // Video
// ----------------------------------------------------------------------------
// display()
void Video::display() {
std::cout << "Video: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Queue.h"
namespace lab6 {
} // namespace lab6
#include "stdafx.h"
#include "MediaManager.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// MediaManager()
MediaManager::MediaManager() {
} // MediaManager
// ----------------------------------------------------------------------------
// addToQueue()
void MediaManager::addToQueue( Media* item ) {
m_queue.push( item );
} // addToQueue
// ----------------------------------------------------------------------------
// removeFromQueue()
void MediaManager::removeFromQueue() {
m_queue.pop();
} // removeFromQueue
// ----------------------------------------------------------------------------
// isEmpty()
bool MediaManager::isEmpty() const {
return m_queue.isEmpty();
} // isEmpty()
// ----------------------------------------------------------------------------
// isFull()
bool MediaManager::isFull() const {
return m_queue.isFull();
} // isFull
// ----------------------------------------------------------------------------
// getFront()
Media* MediaManager::getFront() {
return dynamic_cast<Media*>( m_queue.front() );
} // getFront
// ----------------------------------------------------------------------------
// getBack()
Media* MediaManager::getBack() {
return dynamic_cast<Media*>( m_queue.back() );
} // getBack
// ----------------------------------------------------------------------------
// displayQueue() -- Written For Array Style
void MediaManager::displayQueue() {
if ( isEmpty() ) {
std::cout << "The Queue is empty!/n";
return;
}
while ( !m_queue.isEmpty() ) {
m_queue.front()->display();
m_queue.pop();
}
} // displayQueue
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
#include "Video.h"
#include "MediaManager.h"
int main() {
using namespace lab6;
try {
// Create MediaManager First
std::unique_ptr<MediaManager> pMediaManager;
pMediaManager.reset( new MediaManager );
// Create A Few Media Items
Music m1( "Wouldn't it Be Nice", 3.40f );
Music m2( "My Eyes Adore You", 4.20f );
Music m3( "Comfortably Numb", 6.8f );
Video v1( "Shawshank Redemption", 142.5f );
Video v2( "Matrix", 148.2f );
Video v3( "Lord of the Rings: Fellowship Of the Ring", 190.5f );
Video v4( "Lord of the Rings: The Two Towers", 188.4f );
Video v5( "Lord of the Rings: Return of the King", 191.2f );
// Now Add these to the Manager which encapsulates the queue
pMediaManager->addToQueue( dynamic_cast<Media*>( &v3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v4 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v5 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v1 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m1 ) );
// Now as A Test Will Get Both The Front Then The Back
std::cout << "Display Front & Back Of Queue" << std::endl;
pMediaManager->getFront()->display();
pMediaManager->getBack()->display();
std::cout << std::endl << std::endl;
// Now Will Will Show Full Queue Which Will Also Pop The Queue As It Goes Through the List
std::cout << "Display Entire Queue: " << std::endl;
pMediaManager->displayQueue();
std::cout << std::endl << std::endl;
// Now Our Queue Should Be Empty
std::cout << "Is Queue Empty: " << std::endl;
std::cout << (pMediaManager->isEmpty() ? "true" : "false") << std::endl;
std::cout << std::endl << std::endl;
pMediaManager.reset();
} catch( const std::string& str ) {
std::cout << "Exception Thrown: " << str << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
} catch( ... ) {
std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
}
return RETURN_OK;
} // main
Video.cpp
#include "stdafx.h"
#include "stdafx.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type ) :
m_type( type ),
m_length( 0 ) {
} // Media
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type, const std::string& strTitle, float length ) :
m_type( type ),
m_title( strTitle ),
m_length( length ) {
} // Media
// ----------------------------------------------------------------------------
// getType()
Media::MediaType Media::getType() const {
return m_type;
} // getType
// ----------------------------------------------------------------------------
// setTitle()
void Media::setTitle( const std::string& title ) {
m_title = title;
} // setTitle
// ----------------------------------------------------------------------------
// getTitle()
std::string Media::getTitle() const {
return m_title;
} // getTitle
// ----------------------------------------------------------------------------
// setLength()
void Media::setLength( float length ) {
m_length = length;
} // setLength
// ----------------------------------------------------------------------------
// getLength()
float Media::getLength() const {
return m_length;
} // getLength
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Music()
Music::Music() :
Media( MT_MUSIC ) {
} // Music
// ----------------------------------------------------------------------------
// Music()
Music::Music( const std::string& strTitle, float length ) :
Media( MT_MUSIC, strTitle, length ) {
} // Music
// ----------------------------------------------------------------------------
// display()
void Music::display() {
std::cout << "Song: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Video.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Video()
Video::Video() :
Media( MT_VIDEO ) {
} // Video
// ----------------------------------------------------------------------------
// Video()
Video::Video( const std::string& strTitle, float length ) :
Media( MT_VIDEO, strTitle, length ) {
} // Video
// ----------------------------------------------------------------------------
// display()
void Video::display() {
std::cout << "Video: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Queue.h"
namespace lab6 {
} // namespace lab6
#include "stdafx.h"
#include "MediaManager.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// MediaManager()
MediaManager::MediaManager() {
} // MediaManager
// ----------------------------------------------------------------------------
// addToQueue()
void MediaManager::addToQueue( Media* item ) {
m_queue.push( item );
} // addToQueue
// ----------------------------------------------------------------------------
// removeFromQueue()
void MediaManager::removeFromQueue() {
m_queue.pop();
} // removeFromQueue
// ----------------------------------------------------------------------------
// isEmpty()
bool MediaManager::isEmpty() const {
return m_queue.isEmpty();
} // isEmpty()
// ----------------------------------------------------------------------------
// isFull()
bool MediaManager::isFull() const {
return m_queue.isFull();
} // isFull
// ----------------------------------------------------------------------------
// getFront()
Media* MediaManager::getFront() {
return dynamic_cast<Media*>( m_queue.front() );
} // getFront
// ----------------------------------------------------------------------------
// getBack()
Media* MediaManager::getBack() {
return dynamic_cast<Media*>( m_queue.back() );
} // getBack
// ----------------------------------------------------------------------------
// displayQueue() -- Written For Array Style
void MediaManager::displayQueue() {
if ( isEmpty() ) {
std::cout << "The Queue is empty!/n";
return;
}
while ( !m_queue.isEmpty() ) {
m_queue.front()->display();
m_queue.pop();
}
} // displayQueue
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
#include "Video.h"
#include "MediaManager.h"
int main() {
using namespace lab6;
try {
// Create MediaManager First
std::unique_ptr<MediaManager> pMediaManager;
pMediaManager.reset( new MediaManager );
// Create A Few Media Items
Music m1( "Wouldn't it Be Nice", 3.40f );
Music m2( "My Eyes Adore You", 4.20f );
Music m3( "Comfortably Numb", 6.8f );
Video v1( "Shawshank Redemption", 142.5f );
Video v2( "Matrix", 148.2f );
Video v3( "Lord of the Rings: Fellowship Of the Ring", 190.5f );
Video v4( "Lord of the Rings: The Two Towers", 188.4f );
Video v5( "Lord of the Rings: Return of the King", 191.2f );
// Now Add these to the Manager which encapsulates the queue
pMediaManager->addToQueue( dynamic_cast<Media*>( &v3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v4 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v5 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v1 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m1 ) );
// Now as A Test Will Get Both The Front Then The Back
std::cout << "Display Front & Back Of Queue" << std::endl;
pMediaManager->getFront()->display();
pMediaManager->getBack()->display();
std::cout << std::endl << std::endl;
// Now Will Will Show Full Queue Which Will Also Pop The Queue As It Goes Through the List
std::cout << "Display Entire Queue: " << std::endl;
pMediaManager->displayQueue();
std::cout << std::endl << std::endl;
// Now Our Queue Should Be Empty
std::cout << "Is Queue Empty: " << std::endl;
std::cout << (pMediaManager->isEmpty() ? "true" : "false") << std::endl;
std::cout << std::endl << std::endl;
pMediaManager.reset();
} catch( const std::string& str ) {
std::cout << "Exception Thrown: " << str << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
} catch( ... ) {
std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
}
return RETURN_OK;
} // main
MediaManager.h
#ifndef STDAFX_H
#define STDAFX_H
#include <tchar.h>
#include <conio.h>
#include <memory>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
}; // ReturnCode
#endif // STDAFX_H
#ifndef MEDIA_H
#define MEDIA_H
namespace lab6 {
class Media {
public:
enum MediaType {
MT_MUSIC = 0,
MT_VIDEO,
}; // MediaType
protected:
std::string m_title;
float m_length;
private:
MediaType m_type;
public:
void setTitle( const std::string& title );
std::string getTitle() const;
void setLength( float length );
float getLength() const;
MediaType getType() const;
virtual void display() = 0; // Purely Virtual Each Derived Class Must Implement Their Own
protected:
explicit Media( MediaType type ); // Protected Constructor
Media( MediaType, const std::string& strTitle, float length = 0 );
}; // Media
} // namespace lab6
#endif // MEDIA_H
#ifndef MUSIC_H
#define MUSIC_H
#include "Media.h"
namespace lab6 {
class Music : public Media {
public:
Music();
explicit Music( const std::string& strTitle, float length = 0 );
virtual void display() override;
}; // Music
} // namespace lab6
#endif // MUSIC_H
#ifndef QUEUE_H
#define QUEUE_H
namespace lab6 {
// ----------------------------------------------
// Queue - Cicular Array Style Queue
template<class T>
class Queue {
public:
static const int s_maxSize = 100;
private:
T m_queue[s_maxSize];
int m_front;
int m_back;
public:
Queue();
T front();
T back();
void push( T item );
void pop();
bool isEmpty() const;
bool isFull() const;
}; // Queue
#include "Queue.inl"
} // namespace lab6
#endif // QUEUE_H
#ifndef MEDIA_MANAGER_H
#define MEDIA_MANAGER_H
#include "Queue.h"
namespace lab6 {
class Media;
class MediaManager sealed {
private:
Queue<Media*> m_queue;
public:
MediaManager();
void addToQueue( Media* item );
void removeFromQueue();
Media* getFront();
Media* getBack();
bool isEmpty() const;
bool isFull() const;
void displayQueue();
}; // MediaManager
} // namespace lab6
#endif // MEDIA_MANAGER_H
\ifndef媒体经理\u H
#定义媒体管理器
#包括“Queue.h”
名称空间lab6{
班级媒体;
类媒体管理器{
私人:
队列m_队列;
公众:
MediaManager();
无效addToQueue(媒体*项);
void removeFromQueue();
媒体*getFront();
媒体*getBack();
bool isEmpty()常量;
bool isFull()常量;
void displayQueue();
};//媒体管理器
}//名称空间lab6
#endif//MEDIA\u MANAGER\u H
MediaManager.cpp
#include "stdafx.h"
#include "stdafx.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type ) :
m_type( type ),
m_length( 0 ) {
} // Media
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type, const std::string& strTitle, float length ) :
m_type( type ),
m_title( strTitle ),
m_length( length ) {
} // Media
// ----------------------------------------------------------------------------
// getType()
Media::MediaType Media::getType() const {
return m_type;
} // getType
// ----------------------------------------------------------------------------
// setTitle()
void Media::setTitle( const std::string& title ) {
m_title = title;
} // setTitle
// ----------------------------------------------------------------------------
// getTitle()
std::string Media::getTitle() const {
return m_title;
} // getTitle
// ----------------------------------------------------------------------------
// setLength()
void Media::setLength( float length ) {
m_length = length;
} // setLength
// ----------------------------------------------------------------------------
// getLength()
float Media::getLength() const {
return m_length;
} // getLength
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Music()
Music::Music() :
Media( MT_MUSIC ) {
} // Music
// ----------------------------------------------------------------------------
// Music()
Music::Music( const std::string& strTitle, float length ) :
Media( MT_MUSIC, strTitle, length ) {
} // Music
// ----------------------------------------------------------------------------
// display()
void Music::display() {
std::cout << "Song: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Video.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Video()
Video::Video() :
Media( MT_VIDEO ) {
} // Video
// ----------------------------------------------------------------------------
// Video()
Video::Video( const std::string& strTitle, float length ) :
Media( MT_VIDEO, strTitle, length ) {
} // Video
// ----------------------------------------------------------------------------
// display()
void Video::display() {
std::cout << "Video: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Queue.h"
namespace lab6 {
} // namespace lab6
#include "stdafx.h"
#include "MediaManager.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// MediaManager()
MediaManager::MediaManager() {
} // MediaManager
// ----------------------------------------------------------------------------
// addToQueue()
void MediaManager::addToQueue( Media* item ) {
m_queue.push( item );
} // addToQueue
// ----------------------------------------------------------------------------
// removeFromQueue()
void MediaManager::removeFromQueue() {
m_queue.pop();
} // removeFromQueue
// ----------------------------------------------------------------------------
// isEmpty()
bool MediaManager::isEmpty() const {
return m_queue.isEmpty();
} // isEmpty()
// ----------------------------------------------------------------------------
// isFull()
bool MediaManager::isFull() const {
return m_queue.isFull();
} // isFull
// ----------------------------------------------------------------------------
// getFront()
Media* MediaManager::getFront() {
return dynamic_cast<Media*>( m_queue.front() );
} // getFront
// ----------------------------------------------------------------------------
// getBack()
Media* MediaManager::getBack() {
return dynamic_cast<Media*>( m_queue.back() );
} // getBack
// ----------------------------------------------------------------------------
// displayQueue() -- Written For Array Style
void MediaManager::displayQueue() {
if ( isEmpty() ) {
std::cout << "The Queue is empty!/n";
return;
}
while ( !m_queue.isEmpty() ) {
m_queue.front()->display();
m_queue.pop();
}
} // displayQueue
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
#include "Video.h"
#include "MediaManager.h"
int main() {
using namespace lab6;
try {
// Create MediaManager First
std::unique_ptr<MediaManager> pMediaManager;
pMediaManager.reset( new MediaManager );
// Create A Few Media Items
Music m1( "Wouldn't it Be Nice", 3.40f );
Music m2( "My Eyes Adore You", 4.20f );
Music m3( "Comfortably Numb", 6.8f );
Video v1( "Shawshank Redemption", 142.5f );
Video v2( "Matrix", 148.2f );
Video v3( "Lord of the Rings: Fellowship Of the Ring", 190.5f );
Video v4( "Lord of the Rings: The Two Towers", 188.4f );
Video v5( "Lord of the Rings: Return of the King", 191.2f );
// Now Add these to the Manager which encapsulates the queue
pMediaManager->addToQueue( dynamic_cast<Media*>( &v3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v4 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v5 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v1 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m1 ) );
// Now as A Test Will Get Both The Front Then The Back
std::cout << "Display Front & Back Of Queue" << std::endl;
pMediaManager->getFront()->display();
pMediaManager->getBack()->display();
std::cout << std::endl << std::endl;
// Now Will Will Show Full Queue Which Will Also Pop The Queue As It Goes Through the List
std::cout << "Display Entire Queue: " << std::endl;
pMediaManager->displayQueue();
std::cout << std::endl << std::endl;
// Now Our Queue Should Be Empty
std::cout << "Is Queue Empty: " << std::endl;
std::cout << (pMediaManager->isEmpty() ? "true" : "false") << std::endl;
std::cout << std::endl << std::endl;
pMediaManager.reset();
} catch( const std::string& str ) {
std::cout << "Exception Thrown: " << str << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
} catch( ... ) {
std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
}
return RETURN_OK;
} // main
#包括“stdafx.h”
#包括“MediaManager.h”
#包括“Media.h”
名称空间lab6{
// ----------------------------------------------------------------------------
//MediaManager()
MediaManager::MediaManager(){
}//媒体管理器
// ----------------------------------------------------------------------------
//addToQueue()
void MediaManager::addToQueue(媒体*项){
m_队列推送(项目);
}//addToQueue
// ----------------------------------------------------------------------------
//removeFromQueue()
void MediaManager::removeFromQueue(){
m_queue.pop();
}//removeFromQueue
// ----------------------------------------------------------------------------
//isEmpty()
bool MediaManager::isEmpty()常量{
返回m_queue.isEmpty();
}//isEmpty()
// ----------------------------------------------------------------------------
//isFull()
bool MediaManager::isFull()常量{
返回m_queue.isFull();
}//已满
// ----------------------------------------------------------------------------
//getFront()
媒体*MediaManager::getFront(){
返回动态_cast(m_queue.front());
}//getFront
// ----------------------------------------------------------------------------
//回来
媒体*MediaManager::getBack(){
返回动态_cast(m_queue.back());
}//回来
// ----------------------------------------------------------------------------
//displayQueue()--为数组样式编写
void MediaManager::displayQueue(){
if(isEmpty()){
std::cout display();
m_queue.pop();
}
}//显示队列
}//名称空间lab6
main.cpp
#include "stdafx.h"
#include "stdafx.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type ) :
m_type( type ),
m_length( 0 ) {
} // Media
// ----------------------------------------------------------------------------
// Media()
Media::Media( MediaType type, const std::string& strTitle, float length ) :
m_type( type ),
m_title( strTitle ),
m_length( length ) {
} // Media
// ----------------------------------------------------------------------------
// getType()
Media::MediaType Media::getType() const {
return m_type;
} // getType
// ----------------------------------------------------------------------------
// setTitle()
void Media::setTitle( const std::string& title ) {
m_title = title;
} // setTitle
// ----------------------------------------------------------------------------
// getTitle()
std::string Media::getTitle() const {
return m_title;
} // getTitle
// ----------------------------------------------------------------------------
// setLength()
void Media::setLength( float length ) {
m_length = length;
} // setLength
// ----------------------------------------------------------------------------
// getLength()
float Media::getLength() const {
return m_length;
} // getLength
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Music()
Music::Music() :
Media( MT_MUSIC ) {
} // Music
// ----------------------------------------------------------------------------
// Music()
Music::Music( const std::string& strTitle, float length ) :
Media( MT_MUSIC, strTitle, length ) {
} // Music
// ----------------------------------------------------------------------------
// display()
void Music::display() {
std::cout << "Song: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Video.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// Video()
Video::Video() :
Media( MT_VIDEO ) {
} // Video
// ----------------------------------------------------------------------------
// Video()
Video::Video( const std::string& strTitle, float length ) :
Media( MT_VIDEO, strTitle, length ) {
} // Video
// ----------------------------------------------------------------------------
// display()
void Video::display() {
std::cout << "Video: " << m_title << " Length: " << m_length << std::endl;
} // display
} // namespace lab6
#include "stdafx.h"
#include "Queue.h"
namespace lab6 {
} // namespace lab6
#include "stdafx.h"
#include "MediaManager.h"
#include "Media.h"
namespace lab6 {
// ----------------------------------------------------------------------------
// MediaManager()
MediaManager::MediaManager() {
} // MediaManager
// ----------------------------------------------------------------------------
// addToQueue()
void MediaManager::addToQueue( Media* item ) {
m_queue.push( item );
} // addToQueue
// ----------------------------------------------------------------------------
// removeFromQueue()
void MediaManager::removeFromQueue() {
m_queue.pop();
} // removeFromQueue
// ----------------------------------------------------------------------------
// isEmpty()
bool MediaManager::isEmpty() const {
return m_queue.isEmpty();
} // isEmpty()
// ----------------------------------------------------------------------------
// isFull()
bool MediaManager::isFull() const {
return m_queue.isFull();
} // isFull
// ----------------------------------------------------------------------------
// getFront()
Media* MediaManager::getFront() {
return dynamic_cast<Media*>( m_queue.front() );
} // getFront
// ----------------------------------------------------------------------------
// getBack()
Media* MediaManager::getBack() {
return dynamic_cast<Media*>( m_queue.back() );
} // getBack
// ----------------------------------------------------------------------------
// displayQueue() -- Written For Array Style
void MediaManager::displayQueue() {
if ( isEmpty() ) {
std::cout << "The Queue is empty!/n";
return;
}
while ( !m_queue.isEmpty() ) {
m_queue.front()->display();
m_queue.pop();
}
} // displayQueue
} // namespace lab6
#include "stdafx.h"
#include "Music.h"
#include "Video.h"
#include "MediaManager.h"
int main() {
using namespace lab6;
try {
// Create MediaManager First
std::unique_ptr<MediaManager> pMediaManager;
pMediaManager.reset( new MediaManager );
// Create A Few Media Items
Music m1( "Wouldn't it Be Nice", 3.40f );
Music m2( "My Eyes Adore You", 4.20f );
Music m3( "Comfortably Numb", 6.8f );
Video v1( "Shawshank Redemption", 142.5f );
Video v2( "Matrix", 148.2f );
Video v3( "Lord of the Rings: Fellowship Of the Ring", 190.5f );
Video v4( "Lord of the Rings: The Two Towers", 188.4f );
Video v5( "Lord of the Rings: Return of the King", 191.2f );
// Now Add these to the Manager which encapsulates the queue
pMediaManager->addToQueue( dynamic_cast<Media*>( &v3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v4 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v5 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v2 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m3 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &v1 ) );
pMediaManager->addToQueue( dynamic_cast<Media*>( &m1 ) );
// Now as A Test Will Get Both The Front Then The Back
std::cout << "Display Front & Back Of Queue" << std::endl;
pMediaManager->getFront()->display();
pMediaManager->getBack()->display();
std::cout << std::endl << std::endl;
// Now Will Will Show Full Queue Which Will Also Pop The Queue As It Goes Through the List
std::cout << "Display Entire Queue: " << std::endl;
pMediaManager->displayQueue();
std::cout << std::endl << std::endl;
// Now Our Queue Should Be Empty
std::cout << "Is Queue Empty: " << std::endl;
std::cout << (pMediaManager->isEmpty() ? "true" : "false") << std::endl;
std::cout << std::endl << std::endl;
pMediaManager.reset();
} catch( const std::string& str ) {
std::cout << "Exception Thrown: " << str << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
} catch( ... ) {
std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
std::cout << "Press any key to quit." << std::endl;
_getch();
return RETURN_ERROR;
}
return RETURN_OK;
} // main
#包括“stdafx.h”
#包括“Music.h”
#包括“Video.h”
#包括“MediaManager.h”
int main(){
使用名称空间lab6;
试一试{
//首先创建MediaManager
std::唯一的\u ptr pMediaManager;
pMediaManager.reset(新媒体管理器);
//创建一些媒体项目
音乐m1(“那不是很好吗”,3.40f);
音乐m2(“我的眼睛崇拜你”,4.20f);
音乐m3(“舒适麻木”,6.8f);
视频v1(《肖申克的救赎》,142.5f);
视频v2(“矩阵”,148.2f);
视频v3(“指环王: