mardi 22 mars 2022

The value of a must be 56 but when i run this program i am getting 0

#include <iostream>
using namespace std;
class CSR {
private:
    int complaintsResolved;
    static int totalComplaintsResolved;

public:
    void setComplaintsResolved(int cpsResolved)
    {
        if (cpsResolved < 0)
            return;
        complaintsResolved = cpsResolved;
    }
    static void setTotalCpsResolved(int totalCpsResolved)
    {
        totalComplaintsResolved = totalCpsResolved;
    }
    int getComplaintsResolved()
    {
        return complaintsResolved;
    }
    static int getTotalCpsResolved()

    {
        return totalComplaintsResolved;
    }
};
int CSR::totalComplaintsResolved = 0;

void calcTotalComplaints(CSR employees[7])
{
    int a = 0;
    for (int i = 0; i < 7; i++)
        a += employees[i].getTotalCpsResolved();
    cout << a;
    CSR::setTotalCpsResolved(a);
}
int main()
{
    CSR employees[7];
    for (int i = 0; i < 7; i++) {

        employees[i].setComplaintsResolved((i + 1) * 2);
    }
    calcTotalComplaints(employees);
}

Test Cases

TEST(Q5, second)
{
    CSR employees[7];
    for (int i = 0; i < 7; i++) {
        employees[i].setComplaintsResolved((i + 1) * 2);
    }
    calcTotalComplaints(employees);
    ASSERT_EQ(56, CSR::getTotalCpsResolved());

I am trying to test this program on google test but test cases fails. This code is supposed to give me 56 in the output but i am getting 0 in output kindly look into this and tell me where is the mistake? Also tell me about the scope of static variables like how can i access private static variable outside the class

Aucun commentaire:

Enregistrer un commentaire