Please help me out!!!!!!!

PLEASE HELP ME BY TELLING THE DIFFERNCES IN THE TWO CODES IM SUBMITTING BELOW
IN THE FIRST CODE I GET A GARBAGE VALUE,BUT IN THE SECOND CODE I GET THE REQUIRED RESULT.

***First code:
#include<iostream>
using namespace std;

class AddData //Base Class
{
protected:
int subjects[3], i;
public:
void accept_details();

};
//Class Total – Derived Class. Derived from class AddData and Base class of class Percentage
class Total : public AddData
{
protected:
int total;
public:
void total_of_three_subjects();

};
class Percentage : public Total //Class Percentage – Derived Class. Derived from class Total
{
private:
float per;
public:
void calculate_percentage();
void show_result();

};

void AddData :: accept_details()
{
for(i=0;i<3;i++)
{
cin>>subjects[i];

}
}
\\**IM FACING PROBLEM IN THIS PART
void Total :: total_of_three_subjects()
{
for(i=0;i<3;i++)
{
total= total+subjects[i];

}

}

void Percentage :: calculate_percentage()
{
per= total/3;

}

void Percentage :: show_result()
{
cout<<"Percentage of a Student: "<<per;
}
int main()
{
Percentage p;
p.accept_details();
p.total_of_three_subjects();
p.calculate_percentage();
p.show_result();
return 0;
}

\\**Second Code

#include<iostream>
using namespace std;

class AddData //Base Class
{
protected:
int subjects[3], i;
public:
void accept_details();

};
//Class Total – Derived Class. Derived from class AddData and Base class of class Percentage
class Total : public AddData
{
protected:
int total;
public:
void total_of_three_subjects();

};
class Percentage : public Total //Class Percentage – Derived Class. Derived from class Total
{
private:
float per;
public:
void calculate_percentage();
void show_result();

};

void AddData :: accept_details()
{
for(i=0;i<3;i++)
{
cin>>subjects[i];

}
}

void Total :: total_of_three_subjects()
{
for(i=0;i<3;i++)
{
total= subjects[0] + subjects[1]+ subjects[2];

}

}

void Percentage :: calculate_percentage()
{
per= total/3;

}

void Percentage :: show_result()
{
cout<<"Percentage of a Student: "<<per;
}
int main()
{
Percentage p;
p.accept_details();
p.total_of_three_subjects();
p.calculate_percentage();
p.show_result();
return 0;
}
Your asking why this one doesn't work:
1
2
3
4
void Total :: total_of_three_subjects() {
    for (i = 0; i < 3; i++)
        total = total + subjects[i];
}

and this one does work:
1
2
3
4
void Total :: total_of_three_subjects() {
    for (i = 0; i < 3; i++)
        total = subjects[0] + subjects[1]+ subjects[2];
}


The first one never initializes total to 0, so the first "total + subjects[i]" start with total at an arbitrary value ("garbage").

The second one just sets total to the total of the subjects. The for loop does absolutely nothing (except waste a little time by doing the exact same calculation 3 times).

It should be like one of the following:

1
2
3
4
5
6
7
8
9
void Total :: total_of_three_subjects() {
    total = 0;
    for (i = 0; i < 3; i++)
        total += subjects[i];
}

void Total :: total_of_three_subjects() {
    total = subjects[0] + subjects[1]+ subjects[2];
}


BTW, it's a bad idea to make i a member variable of the class in order to use it as an arbitrary loop index.
Last edited on
Topic archived. No new replies allowed.