我不能打印我的书名,但可以打印其他所有内容

时间:2014-09-23 15:22:09

标签: c++ header-files

我的cpp文件,出于某种原因,我可以在书的名称旁边打印其他所有内容,我认为它是我的主文件。我构建它时没有任何错误,但是当我运行它时,小窗口会出现并消失

#include "BookRecord.h"

BookRecord::BookRecord()
{
    m_sName[128]=NULL;
    m_lStockNum=0;
    m_iClassification = 0;
    m_dCost = 0.0;
    m_iCount = 0;
}
BookRecord::BookRecord(char *name, long sn, int cl, double cost)
{
    m_iCount=1;
    m_sName[128]=*name;
    m_lStockNum=sn;
    m_iClassification=cl;
    m_dCost=cost;
}
BookRecord::~BookRecord()
{
}

void BookRecord::getName(char *name)
{
    strcpy(name, m_sName);
}

void BookRecord::setName(char *name)
{
    strcpy(m_sName, name);
}
long BookRecord::getStockNum()
 {
     return m_lStockNum;
 }
void BookRecord::setStockNum(long sn)
{
    m_lStockNum = sn;
}
void BookRecord::getClassification(int& cl)
{
    cl=m_iClassification;
}
void BookRecord::setClassification(int cl)
{
    m_iClassification= cl;
}
void BookRecord::getCost(double *c)
{
    *c = m_dCost;
}
void BookRecord::setCost(double c)
    { 
        m_dCost = c;
    }
int BookRecord::getNumberInStock()
 {
     return m_iCount;
 }
 void BookRecord::setNumberInStock(int count)
 {
     count = m_iCount;
 }
void BookRecord::printRecord()
 {
     //cout << "Name: " <<m_sName <<"\n";
     printf("Name: %s", m_sName);
     cout<<endl;
     cout<<"Stock Number: "<<m_lStockNum<<"\n";
     cout<<"Class: " <<m_iClassification<<"\n";
     cout<<"Cost: $"<<m_dCost<<"\n";
     cout<<"In Stock: "<<m_iCount<<"\n";
 }

我的主要文件

#include "BookRecord.h"

int main()
{
    char *bName="C Plus Plus";
    BookRecord *Ana = new BookRecord(bName, 1, 1, 50.9);
    /*char * bookName="";
    int clNo;
    double c;
    Ana->getClassification(clNo);
    Ana->getCost(&c);
    Ana->getName(bookName);*/
    cout << "Printing using printRecord() Function" << endl;
    Ana->printRecord();

    /*cout << endl << "Printing using getter properties" <<endl;
    cout << bookName << endl;
    cout<< clNo << endl;
    cout << c << endl;
    cout << Ana->getNumberInStock() << endl;*/
    return 0;
}

1 个答案:

答案 0 :(得分:2)

您好我会做的有点不同,不使用char m_sName [128],我宁愿使用std :: string或指针,只是在析构函数中将其销毁。我的代码基于你上面已经要求的内容,我尽可能简单。我知道在这里可以做得更好,但是你走了:

编辑:有点重构,问题也在增长。

BookRecord.h

#ifndef __BOOKRECORD_H__
#define __BOOKRECORD_H__

#ifndef MAX_NAME_SIZE
#define MAX_NAME_SIZE 128
#endif

class BookRecord
{
private:
    char m_sName[MAX_NAME_SIZE] = {}; // I would have used a pointer or std:string and not char
    long m_lStockNum = 0;
    int m_iClassification = 0;
    double m_dCost = 0.0;
    int m_iCount = 0;

public:
    BookRecord();
    BookRecord(char* name, long sn, int cl, double cost);
    ~BookRecord();
    // EDIT: Your question grew
    char* GetName();
    void SetName(char *name);
    long GetStockNum();
    void SetStockNum(long sn);
    int GetClassification();
    void SetClassification(int cl);
    double GetCost();
    void SetCost(double c);
    int GetNumberInStock();
    void SetNumberInStock(int count);
    void PrintRecord();
};
#endif

BookRecord.cpp

#include <stdlib.h>
#include <iostream>
#include <stdexcept>
#include "BookRecord.h"
using namespace std;

BookRecord::BookRecord()
{
}

BookRecord::BookRecord(char* name, long sn, int cl, double cost) : 
    m_iCount(1), m_lStockNum(sn), m_iClassification(cl), m_dCost(cost)
{
    if (name == NULL)
        throw std::invalid_argument("Name of book is null");
    if (strlen(name)>MAX_NAME_SIZE)
        throw std::invalid_argument("Name of book is to long");

    memset(&m_sName, 0, sizeof(char)*MAX_NAME_SIZE);
    memcpy(&m_sName, name, strlen(name)*sizeof(char));
}

BookRecord::~BookRecord()
{
}

// Edit: Question grew
char* BookRecord::GetName()
{
    return static_cast<char*>(m_sName);
}

void BookRecord::SetName(char *name)
{
    strcpy(m_sName, name); // TODO: handle null, and sizechecks here to your likings
}

long BookRecord::GetStockNum()
{
    return m_lStockNum;
}

void BookRecord::SetStockNum(long sn)
{
    m_lStockNum = sn;
}

int BookRecord::GetClassification()
{
    return m_iClassification;
}

void BookRecord::SetClassification(int cl)
{
    m_iClassification = cl;
}

double BookRecord::GetCost()
{
    return m_dCost;
}

void BookRecord::SetCost(double c)
{
    m_dCost = c;
}

int BookRecord::GetNumberInStock()
{
    return m_iCount;
}

void BookRecord::SetNumberInStock(int count)
{
    m_iCount = count;
}

void BookRecord::PrintRecord()
{
    cout << static_cast<const char*>(m_sName) << endl;
    cout << "Stock Number: " << m_lStockNum << endl;
    cout << "Class: " << m_iClassification << endl;
    cout << "Cost: $" << m_dCost << endl;
    cout << "In Stock: " << m_iCount << endl;
}

如果您想知道异常抛出,请查看此post

希望有所帮助