인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

Lyw's profile image
Lyw

asked

[10 minutes a day | C++] Introduction to C++ programming that anyone can easily learn

추상화와 클래스

Resolved

Written on

·

186

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;

}

C++

Answer 1

0

pandacoding님의 프로필 이미지
pandacoding
Instructor

1. 

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

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

 

2. 

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

Lyw님의 프로필 이미지
Lyw
Questioner

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

pandacoding님의 프로필 이미지
pandacoding
Instructor

지금 확인했습니다..!

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

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

Lyw's profile image
Lyw

asked

Ask a question