• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

추상화와 클래스

21.09.27 09:15 작성 조회수 133

2

#include <iostream>

 

using namespace std;

 

class Stock

 

{

public:

void acquire(string&, int, float);      //string뒤에 엠퍼센드가 붙으면 오류가 뜨네요.. 엠퍼센드가 붙은 이유가 뭔가요??

void buy(int, float);

void sell(int, float);

void updata(float);

void show();

 

Stock();

~Stock();

 

private:

string name;

int shares;

float share_val;

double total_val;

void  set_total() { total_val = shares*share_val; }

};

 

 

void Stock::acquire(string& co, int n, float pr)

{

name = co;

shares = n;

share_val = pr;

}

void Stock::buy(int n, float pr)

{

shares += n;

share_val = pr;

set_total();

}

void Stock::sell(int n, float pr)

{

shares -= n;

share_val = pr;

set_total();

}

void Stock::updata(float pr)

{

share_val = pr;

set_total();

}

void Stock::show()

{

cout << "회사 명 :" << name << endl;

cout << "주식 수 :" << shares << endl;

cout << "주가 :" << share_val << endl;

cout << "주식 총 가치 :" << total_val << endl;

}

 

 

Stock::Stock()

{

}

 

Stock::~Stock()

{

}

int main() {

 

Stock temp;

temp.acquire("Panda", 100, 1000);

temp.show();                                                          //이 부분 출력 될 때  주식 총 가치 :-9.25596e+61 라고 출력이 되는데 이유를 모르겠습니다.

temp.buy(10, 1200);

temp.show();

temp.sell(5, 800);

temp.show();

 

return 0;

}

답변 1

답변을 작성해보세요.

0

1. 

앗.. 혹시 제가 강의에서 앰퍼센드를 사용했나요?

어느강의인지 말씀해주시면 확인해보겠습니다.

 

2. 

acquire에서 마지막에 set_total()을 호출해주세요~

Lyw님의 프로필

Lyw

질문자

2021.09.27

추상화와 클래스 강의입니다.

지금 확인했습니다..!

죄송합니다. 오타인데 오타를 수정하는 부분이 편집상 제거된 것 같습니다 ㅠㅠ!

강의마다 같이 올려드는 코드를 확인하시면서 들으시면 좋을 것 같습니다.