c++ 중간고사 준비 (Class 햇갈리는 거)

Jaewoong2·2020년 10월 14일
0

C++

목록 보기
7/13
#include <iostream>

class UGClass {
private:
    int regNo;
    char section;
public:
    UGClass():regNo(0){};
    UGClass(int r, int c): regNo(r), section(c) {};
    void getReg() { std::cout << regNo << std::endl ;}
};

int main() {

    UGClass calss(); // 잘못된 표현
    UGClass cas, cass(2,'3'); // 가능

    UGClass stud = UGClass();// 이건 되는 표현
    stud.getReg();

    UGClass std = UGClass; // 이건 안되는 표현
 return 0;
};
#include <iostream>

class Server {
public:
    Server(char letterName): name(letterName) {};
    static int getTurn() { turn++; return turn;};
    void serveOne();
    static bool isStillOpen() { return newOpen; };
private:
    static int turn;
    static int lastServed;
    static bool newOpen;
    char name;
};

// static 변수를 정의할 때는, static 을 붙이지 않는다.
// 정의를 해줘야 오류가 나오지 않는다.
int Server::turn = 0; // static 이 없으면 정이 되지 않는다
int Server::lastServed = 0; // static 이 없으면 정이 되지 않는다
bool Server::newOpen = true; // static 이 없으면 정이 되지 않는다

void Server::serveOne() {
    if(newOpen && lastServed < turn) {
        lastServed++;
        std::cout << "서버 : " << name << " 돌아가는 중" << lastServed << std::endl;
    }

    if(lastServed >= turn) {
        newOpen = false;
    }
}

int main() {
    Server s1('A'), s2('B');
    int number, count;
    do {
        std::cout << "얼마나 많은 그룹이 있나요?? ";
        std::cin >> number;
        std::cout << "몇번 째 턴? ";
        for(int count = 0; count < number; count++) {
            std::cout << Server::getTurn() << ' ';
        }
        std::cout << std::endl;
            s1.serveOne();
            s2.serveOne();
    } while(Server::isStillOpen());

    std::cout << "서비스 끄기 \n ";
    return 0;
};


#include<iostream>

class HotdogStand {
public:
    HotdogStand():standNumber(0),standSoldHotdog(0){};
    HotdogStand(const int& number):standNumber(number),standSoldHotdog(0){};
    int getSoldedHotDog() const { return standSoldHotdog; };
    void justSold() { standSoldHotdog++; allStandsSoldHotdogs++; };
    static int getAllStandsSoldHotdogs() { return allStandsSoldHotdogs; };
    // static 멤버는 const를 뒤에 두지 못한다.
private:
    int standNumber;
    int standSoldHotdog;
    static int allStandsSoldHotdogs;
};

int HotdogStand::allStandsSoldHotdogs = 0;

int main() {
    HotdogStand h1(1), h2(2), h3(3);
    h1.justSold();
    h2.justSold();
    h3.justSold();

    std::cout << h1.getSoldedHotDog() << "\n";
    std::cout << h2.getSoldedHotDog() << "\n";
    std::cout << h3.getSoldedHotDog() << "\n";
    std::cout << HotdogStand::getAllStandsSoldHotdogs() << "\n";

    return 0 ;
}
#include<iostream>
#include<cstdlib>

const int MAXSIZE = 100;

class CharPair {
public:
    CharPair():sizeOfArr(10){for (int i = 0; i < sizeOfArr; i++) arr[i] = '#';};
    CharPair(int size);
    CharPair(int size, char arrValue[]);
//    CharPair(char firstValue, char secondVale) : first(firstValue), second(secondVale){};
    char& operator[](int index);
private:
//    char first, second;
    char arr[MAXSIZE];
    int sizeOfArr;
};

CharPair::CharPair(int size) {
    if(size > MAXSIZE) {
        std::cout<< "많이 큰 값 입니다.";
        exit(1);
    }
    for(int i =0; i < size; i++) {
        arr[i] = '#';
    }
}

CharPair::CharPair(int size, char arrVale[]) {
    // 배열을 매게변수로 넣는 방법 : 변수명 뒤에 [] 를 넣어준다.
  if(size > MAXSIZE) {
      std::cout << "많이 큰 값 입니다.";
      exit(1);
  }
  for(int i = 0; i < size; i++) {
      arr[i] = arrVale[i];
  }
};

char& CharPair::operator[](int index) {
    return arr[index];
};

int main() {
    CharPair charp1, charp2(12);
    for(int i = 0; i < 12; i++) {
        std::cout << charp2[i];
    }
    std::cout << "\n";
    for(int i = 0; i < 10; i++) {
        std::cout << charp1[i];
    }
    std::cout << std::endl;
    charp1[15] = 'b';
    for(int i = 0; i < 16; i++) {
        std::cout << charp1[i];
    }
    // 0 ~ 9 => #
    // 15 => b

    std::cout << std::endl;
    char charArr[3] = {'b', 'a', 'd'};
    CharPair chp(3, charArr);
    for(int i = 0; i < 3; i++) {
        std::cout << chp[i];
    }
    std::cout << std::endl;
    return 0;
}
profile
DFF (Development For Fun)

0개의 댓글