Some One Please explain to me...

It wont compile.. it gives me`'setNum1,2,3,4' was not declared in this scope...

[code]
#ifndef NUMBERFUN_H
#define NUMBERFUN_H

class NumberFun
{
public:
NumberFun(int val1,int val2,int val3,int val4);
void setValues(int m, int r, int i, int k);

void setNum1 (int n);
void setNum2 (int n);
void setNum3 (int n);
void setNum4 (int n);

int getNum1();
int getNum2();
int getNum3();
int getNum4();

int sum();
int average();
int product();
int smallest();
int largest();

private:
int num1, num2, num3, num4;
};

#endif // NUMBERFUN_H


[code]
#include <iostream>
#include "numberfun.h"
using std::cin;
using std::cout;
using std::endl;

int main()
{
int val1, val2, val3, val4;
cout<< "Kindly enter 4 numbers: ";
cin>> val1>>val2>>val3>>val4 ;

NumberFun fish(val1, val2, val3, val4);
cout<< " Num1 has the value: " <<fish.getNum1() <<endl;
cout<< " Num2 has the value: " <<fish.getNum2() <<endl;
cout<< " Num3 has the value: " <<fish.getNum3() <<endl;
cout<< " Num4 has the value: " <<fish.getNum4() <<endl;
cout<< " The sum of the 4 numbers are: " <<fish.sum() <<endl;
cout<< " The average of the 4 numbers are: " <<fish.average() <<endl;
cout<< " The smallest of the 4 numbers are: "<<fish.smallest() <<endl;
cout<< " The largest of the 4 numbers are: "<<fish.largest() <<endl;
cout<< " The product of the 4 numbers are: "<<fish.product() <<endl;

cin.get();
cin.get();

return 0;
}


[code]
#include "numberfun.h"
using namespace std;

int num1,num2,num3,num4;

NumberFun::NumberFun (int val1,int val2,int val3,int val4)
{
setValues(val1,val2,val3,val4);
}

void setValues(int m, int r, int i, int k)
{
setNum1(m);
setNum2(r);
setNum3(i);
setNum4(k);
}

void setNum1 (int n)
{
num1 = n;
}
void setNum2 (int n)
{
num2 = n;
}
void setNum3 (int n)
{
num3 = n;
}
void setNum4 (int n)
{
num4 = n;
}

int getNum1()
{
return num1;
}
int getNum2()
{
return num2;
}
int getNum3()
{
return num3;
}
int getNum4()
{
return num4;
}
int sum()
{
return num1 + num2 + num3 + num4;

}
int average()
{
return (sum()/4);
}
int product()
{
return num1 * num2 * num3 * num4;
}
int smallest()
{
int smallestNum = num1;
if (num2 < smallestNum)
smallestNum = num2;
if (num3 < smallestNum)
smallestNum = num3;
if (num4 < smallestNum)
smallestNum = num4;
return smallestNum;

}
int largest()
{
int largestNum = num1;
if (num2 > largestNum)
largestNum = num2;
if (num3 > largestNum)
largestNum = num3;
if (num4 > largestNum)
largestNum = num4;
return largestNum;


}
Not sure if this is what's causing the problem, but I would get rid of void setValues() . You already have your set functions to change the variables individually, so you don't need another function for all of them together.


And your constructor should look like this:

1
2
3
4
5
6
7
8
9
10
11
 NumberFun::NumberFun (int val1,int val2,int val3,int val4)
{
num1 = val1;
num2 = val2;
num3 = val3;
num4 = val4;
}

//The constructor is "inside" the class, so it is able to access the private variables
//directly. You don't have to use the set functions here.
 
Last edited on
I could make sure that your class function definitions are written like:

1
2
3
4
5
6
void NumberFun::setNum1 (int n)
{
num1 = n;

}
Topic archived. No new replies allowed.