2013-07-09 4 views
-1

이 헤더 파일을 컴파일 중입니다. 나는 void에서 왜 getfunctions의 값을 표시하지 않는지 이해하지 못합니다. 아래 예제에서 헤더 코드를 올바르게 수행하고 있습니다. 나C++ 헤더 파일 출력

// return the player's batting average 
double Baseball::getBatAvg() 
{ 

    if (atbats == 0) 
     // average is 0.0 if player has no at bats 
     return 0.0; 
    else 
     // batting average is the number of hits 
     // divided by the number of at bats 
     return double(hits)/double(atbats); 
} 

// format and output batting statistics 
void Baseball::writeBattingStats() 
{ 
    cout << "Player" << setw(3) << uniformNo 
     << " At bats" << setw(4) << atbats 
     << " Hits" << setw(4) << hits 
     << " Average " << setreal(1,3) << getBatAvg() 
     << endl; 
} 

이 주어졌다

#ifndef STOCK_MARKET_CLASS 
#define STOCK_MARKET_CLASS 

// system defined preprocessor statement for cin/cout operations 
#include <iostream > 

// programmer defined preprocessor statement for setreal operation 
#include "textlib.h" 

// programmer defined preprocessor statement for String 
#include "tstring.h" 

class StockMarket 
{ 
    private: 
    String symbol;  // identifies the company 
    double startingPrice;  // starting price of the stock 
    double closingPrice;  // closing price of the stock 

    public: 
    // Constructor initializes the attributes that are the symbol, the starting price of the stock, and   
    // the closing price of the stock. 
    StockMarket(String sym, double sPrice, double cPrice); 

    // Takes the closing price of the stock and subtracts the starting price of the stock. Returns the 
    // amount of change in the price of the stock. 
    // You might not have any arguments. 
    double change(double sPrice, double cPrice); 

    // Returns the symbol. 
    // You might not have any arguments. 
    String getSymbol(String sym); 

    // Returns the starting price of the stock. 
    // You might not have any arguments. 
    double getStartingPrice(double sPrice); 

    // Returns the closing price of the stock. 
    // You might not have any arguments. 
    double getClosingPrice(double cPrice); 

    // Outputs the following information that is listed below. 
    // Stock Information for IBM:   <=== where IBM is the symbol 
    // Starting Price  $XXX.XX 
    // Closing Price  $XXX.XX 
    //     ------------- 
    // Difference   $XXX.XX 
    // You might not have any arguments. 
    void writeStockInfo(); 
}; 

//********************************************************************** 
//    StockMarket Class Implementation 
//********************************************************************** 

// Constructor is passed arguments sym, sPrice, and cPrice 
// Implementation of the constructor 
StockMarket::StockMarket(String sym, double sPrice, double cPrice) 
{ 
    symbol = sym; 
    startingPrice = sPrice; 
    closingPrice = cPrice; 
} 

// Function which takes the closing price of the stock and subtracts the starting price of the stock. Returns the 
// amount of change in the price of the stock. 
// Implementation of the function 
// You might not have any arguments. 
double StockMarket::change(double cPrice, double sPrice) 
{ 
    return double (cPrice) - double (sPrice); 
} 

// Function to return the symbol. 
// Implementation of the function 
// You might not have any arguments. 
String StockMarket::getSymbol(String sym) 
{ 
    return sym; 
} 


// Function to return the starting price of the stock. 
// Implementation of the function 
// You might not have any arguments. 
double StockMarket::getStartingPrice(double sPrice) 
{ 
    return sPrice; 
} 


// Function to return the closing price of the stock. 
// Implementation of the function 
// You might not have any arguments. 
double StockMarket::getClosingPrice(double cPrice) 
{ 
    return cPrice; 
} 


// Function that outputs the following information that is listed below. 
// Stock Information for IBM:   <=== where IBM is the symbol 
// Starting Price  $XXX.XX 
// Closing Price  $XXX.XX 
//     ------------- 
// Difference   $XXX.XX 
// Implementation of the function 
// You might not have any arguments. 
void StockMarket::writeStockInfo() 
{ 
    cout << "Stock Information for"<< setw(4) << getSymbol() << ":" << endl; 
} 

#endif           // STOCK_MARKET_CLASS 

예는 getSymbol 같은

#include "stdafx.h" 
// system defined header file that declares the input/output operations (cin/cout) 
#include <iostream> 
using namespace std; 
// system defined header file that declares parametric manipulators 
#include <iomanip> 
#include "Stock.h" 

int main() 
{ 
    StockMarket IBMStock("IBM", 150.00, 300.00); 

    IBMStock.writeStockInfo(); 


    system("PAUSE"); 
    return 0; 

} 
+2

"무효"는 무엇입니까 멤버 변수를 액세스 할 수 있습니까? –

+7

@ H2CO3 매우 철학적 인 질문입니다. – Salgar

+1

일반적으로 헤더를 컴파일하지 않습니다. 아마도이 코드를 어떻게 실행하는지 보여줄 수 있습니까? – juanchopanza

답변

0

귀하의 가져 오기 기능, String 형의 하나 개의 인수를 필요로 내 CPP 코드입니다.

void StockMarket::writeStockInfo() 
{ 
    cout << "Stock Information for"<< setw(4) << getSymbol() << ":" << endl; 
} 

제공된 인수가없는 getSymbol()을 호출하고 있습니다. IBMStock.writeStockInfo()가 결과를 표시하지 않는 이유가 여기에 있습니다.

그러나 다른 문제는 무시됩니다. get 함수 구현에서 단순히 전달하는 인수를 반환하는 중입니다 ... 이것은별로 의미가 없습니다. 당신이해야 할 일은 'this'포인터를 이용하는 것입니다.

String StockMarket::getSymbol() 
{ 
    return this->symbol; 
} 

내 생각 엔 StockMarket 개체에 속하는 Symbol이라는 멤버 변수 문자열이 있다고 생각합니다.

또한 C++ 측면 메모입니다. 헤더 파일과 구현 파일을 분리하는 것이 가장 좋습니다.

편집 : 명확한 설명을 위해 , 당신은뿐만 아니라이 포인터없이

String StockMarket::getSymbol() 
{ 
    return symbol; 
} 
+0

'this->'를 사용하지 않고'symbol' 변수에 접근 할 수 있습니까? –

+0

실제로 가능합니다. 기호를 StockMarket 객체에 속한 지점을 설명하기 위해 가져온 것입니다. –