C++

명품 C++ 실습문제 5장 10번

Hs’s Coding vlog 2023. 1. 5. 18:47
#include <iostream>
#include <string>
using namespace std;

class Buffer {
    string text;

public:
    Buffer(string text){this->text = text; }
    void add(string next){text += next; }
    void print() { cout << text << endl; }
};
Buffer& append(Buffer& buf, string s) { //버퍼클래스 객체의 참조를 리턴하는 함수이기 때문에 자료형을 Buffer형으로 해줘야함
    buf.add(s);
    return buf;

}//이함수는 객체의 참조를 리턴해주는 함수이고 멤버 함수가 아니다 그리고 멤버함수를 쓰고 싶다면 멤버변수의 선언을통하는 것을 까먹지마라

int main() {
    Buffer buf("Hello");
    Buffer &temp = append(buf, "Guys"); //멤버함수x 그냥 함수를 써먹은것이다.
    temp.print();
    buf.print();
}