C++ questions

Asked 2 years ago, Updated 2 years ago, 25 views

The question is a simple question that takes 10 integers and outputs how many 60 points or more. I don't know what I did wrong. If there's something wrong with this part, it'd be better to do it like thisI'd appreciate it if you could let me know if there's anything else.

#include <iostream>
using namespace std;

class Dept {
    int size;
    int *scores;
public:
    Dept(int size) {
        this->size = size;
        scores = new int[size];
    }
    ~Dept() { if(scores) delete[]scores; }
    Dept(Dept &dept);
    int getSize() { return size; }
    void read();
    bool isOver60(int index);
};
Dept::Dept(Dept &dept) {
    this->size = dept.size;
    this->scores = new int[dept.size];
}
void Dept::read() {
    cout <<size << "Enter an integer>>";
    for (int i = 0; i < size; i++)
        cin >> scores[i];
}
bool Dept::isOver60(int index) {
    if (scores[index] >= 60) {
        return true;
    }
    else {
        return false;
    }
}
int countPass(Dept dept) {
    int count = 0;
    for (int i = 0; i < dept.getSize(); i++) {
        if (dept.isOver60(i))
            count++;
    }
    return count;
}
int main() {
    Dept com(10);
    com.read();
    int n = countPass(com);
    cout << "60 points or more" <<n << "Number";
}

I don't know why there are zero people....

c++

2022-09-22 14:38

1 Answers

countPass(com);

The copy generator is called due to the (Dept def) parameter at the time the countPass is called in the main.

However, the actual value is not being copied because the copy generator only has a code that assigns an array to heap memory as shown below.

Dept::Dept(Dept &dept) {
    this->size = dept.size;
    this->scores = new int[dept.size];
}

Therefore, you can complete the copy generator implementation to achieve the desired results You can change the countPass to call by reference.

Simply change the parameter to a reference as shown below to resolve it.

int countPass(Dept &dept)


2022-09-22 14:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.