课程操作

时间:2013-10-28 04:28:14

标签: c++ class exception throw

我对完成这个程序有点怀疑

评估要求我们根据下面提供的信息,实现RockConcert和TicketHolder类。 TicketHolder类代表支付特定音乐会入场费的人。这个TicketHolder类应该跟踪它的成本和座位所在的位置。

我已经构建了大部分代码,给出了提供的驱动程序,但我对如何编写方法RockConcert :: sellTicket和RockConcert :: getTicket

非常感到困惑

以下是代码,

非常感谢!

TicketHolder.h

#ifndef TicketHolder_h
#define TicketHolder_h
#include <string>
#include <iostream>

using namespace std;

class TicketHolder {

public:
    TicketHolder();
    TicketHolder( std::string seat, double cost );

    std::string getSeat() const;
    double getCost() const;

    friend std::ostream& operator<< (std::ostream& os, const TicketHolder & t);

private:
    std::string my_Seat;
    double my_Cost;
};

#endif

TicketHolder.cpp

#include "TicketHolder.h"


TicketHolder::TicketHolder(){

}
TicketHolder::TicketHolder( std::string seat, double cost ){
    my_Seat = seat;
    my_Cost = cost;
}

std::string TicketHolder::getSeat() const{
    return (my_Seat);
}

double TicketHolder::getCost() const{
    return (my_Cost);

}

std::ostream& operator<< (std::ostream& os, const TicketHolder & t){
    return os;
}

RockConcert.h

#ifndef RockConcert_h
#define RockConcert_h
#include <string>
#include <iostream>
#include <stdexcept>
#include "TicketHolder.h"
#include "SeatAlreadySold.h"
#include "SeatNotFound.h"

class RockConcert {

public:
    public:
    RockConcert( std::string date, std::string location );

    void sellTicket( std::string seat, double cost ) throw (SeatAlreadySold);
    TicketHolder getTicket( std::string seat ) throw (SeatNotFound);
    double getProceeds( );

    friend std::ostream& operator<< ( std::ostream& out, const RockConcert & r );


private:
        TicketHolder myTickets[ 20 ];
        int myNumberOfTicketsSeenSoFar;
        std::string my_When;
        std::string my_Where;

    };

#endif

RockConcert.cpp

    #include "RockConcert.h"
    #include "TicketHolder.h"

    RockConcert::RockConcert( std::string date, std::string location ) {
        my_When = date;
        my_Where = location;
    }

    void RockConcert::sellTicket( std::string seat, double cost ) throw (SeatAlreadySold){

    }

    TicketHolder RockConcert::getTicket( std::string seat ) throw(SeatNotFound) {


    }

    double RockConcert::getProceeds( ){
        return (myNumberOfTicketsSeenSoFar);

    }

    std::ostream& operator<< ( std::ostream& out, const RockConcert & r ){
        return out;
    }

Driver.CPP

// driver
// function main
#include "TicketHolder.h"
#include "RockConcert.h"
#include <iostream>
#include <stdexcept>

int main(int argc, const char * argv[])

{
RockConcert megaDeath( "Sept 1", "Staples Center" );
megaDeath.sellTicket( "Row Q Seat 20", 120.00 );
megaDeath.sellTicket( "Row Q Seat 21", 120.00 );
megaDeath.sellTicket( "Row SSS Seat 12", 50.00 );
TicketHolder ticket = megaDeath.getTicket( "Row Q Seat 21" );

// Print out the concert and its tickets
std::cout << megaDeath << std::endl;


try {


    megaDeath.sellTicket( "Row T Seat 30", 65.00 );

} catch( SeatAlreadySold sas ) {

    std::cout << "An error occurred here for the reason:";
    std::cout << sas.what(std::endl:endl;
}

// Print out the library and its book - notice the difference
std::cout << megaDeath << std::endl;
// Let's try to throw some errors...
try {
    // can't sell a ticket that has already been sold
    megaDeath.sellTicket( "Row Q Seat 21", 120.00 );
    std::cout << "An error occurred here..." ;
} catch( SeatAlreadySold sas ) {
    sas.what();
}

try {
    // can't get a ticket that has not been sold
    megaDeath.getTicket( "Row A Seat 1" );
    std::cout << "An error occurred here..." ;
} catch( SeatNotFound snf ) {
    snf.what();
}

}

1 个答案:

答案 0 :(得分:1)

我不确定这里的问题究竟是什么,但我倾向于创建一个std::map映射一个票证ID(它可能是一个像“Row ## Seat ##”这样的字符串,如同只要你一致)到TicketHolder个实例。您的TicketHolder类(我认为更好地命名为Ticket)应该包含一个布尔“已售出”属性。

getTicket()方法中,std::map中的简单查找就可以了,而且非常快。

您的sellTicket()方法应首先调用getTicket(),然后检查返回的故障单的“已售出”属性(如果已售出则抛出异常)。如果没有,请设置TicketHolder::sold = true。您还应该将票证成本添加到目前为止的累计收益中。

其他可能有用的一般规则:

  • 当您可以
  • 时,在构造函数中使用初始化列表
  • 避免使用C ++中的异常说明符,它们可能不会按您的想法进行操作
  • 除非你真的知道你在做什么,否则总是按值抛出异常,并通过const引用捕获。
  • 避免在函数参数
  • 中复制std::string和其他复杂对象
  • 同样,请仔细考虑您的方法返回类型何时应该是副本,什么时候应该是引用,什么时候应该是const引用,什么时候它们应该是(智能)指针。