C++

명품 C++ 실습문제 5장 12,12-1번

Hs’s Coding vlog 2023. 1. 5. 18:49

12-1번

#include <iostream>
#include <string>
using namespace std;
class Dept {
    int size;
    int* scores;
public:
    Dept(int size) { this->size = size; scores = new int[size]; } //생성자
    /*Dept(const Dept& dept) {
        this->size = dept.size; 
        this -> scores = new int[size];
        for (int i = 0; i < size; i++) {
            scores[i] = dept.scores[i];
        }
    }*///복사생성자
    //~Dept() { delete[] scores; }//소멸자 소멸자를 적지 않는다면 힙메모리를 터트릴게 없어서 상관이 없어진다
   
    int getSize() { return size; }
    void read() {
        int num;
        cout << size << "개 점수 입력>>";
        for (int i = 0; i < size; i++) {
            cin >> num;
            scores[i] = num;
        }
    }
    bool isover60(int index) {
        if (scores[index] > 60) {
            return true;
        }
        else return false;
    }//60점초과 true 리탄
};

int countPass(Dept dept) {
    int count = 0;
    for (int i = 0; i < dept.getSize(); i++) {
        if (dept.isover60(i) == true) count++;
    }
    return count;
}
int main() {
    Dept com(10);
    com.read();
    int n = countPass(com);
    cout << "60점 이상은 " << n << "명";
    cout << endl;
}

 

12번

#include <iostream>
#include <string>
using namespace std;
class Dept {
    int size;
    int* scores;
public:
    Dept(int size) { this->size = size; scores = new int[size]; } //생성자
    ~Dept() { delete[] scores; }//소멸자
    int getSize() { return size; }
    void read() {
        int num;
        cout << size << "개 점수 입력>>";
        for (int i = 0; i < size; i++) {
            cin >> num;
            scores[i] = num;
        }
    }
    bool isover60(int index) {
        if (scores[index] > 60) {
            return true;
        }
        else return false;
    }//60점초과 true 리탄
};

int countPass(Dept dept) {
    int count = 0;
    for (int i = 0; i < dept.getSize(); i++) {
        if (dept.isover60(i)==true) count++;
    }
    return count;
}
int main() {
    Dept com(10);
    com.read();
    int n = countPass(com);
    cout << "60점 이상은 " << n << "명";
    cout << endl;
}