-
using classes
Im have trouble with creating classes. I got the private part down packed but the public part is where I have a little trouble. In my program my professor wants me to do a program on amicable pairs. I have the equations I want just that Im missin a declaration which I have no idea what that means. Here's my code so far:
Code:
#include<iostream>
using namespace std;
class Apair
{
private:
int A;
int T;
int S;
int B;
int F;
int D;
public:
A = 220;
while(A <= 7000){
S = 0;
D = 1;
D = A/2;
if(A % D = 0){
S = S+ D;
}
D++;
if(S > A){
B = S;
T = 0;
for(F = 1)
if(B % F = 0){
T = T+ F;
}
if(T = A){
cout << &A& " and " &B& "are an amicable pair" << endl;
}
F++;
A = A + 1;
}
}
};
-
You can't put executable code directly inside a class.
Btw....trying to get this program done without classes would be a very nice idea. After that...i.e..if it works, you can think about classes.
Also..though you don't need classes here......what are the 6 integers doing in public ? (!) ..A pair needs only 2.
-
I didnt write my program so far in the correct way so here's a better look at it of what I mean. I know I dont need a class but my professor wants us to get use to using classes so that is why Im using one here. When you said I cant put exectable code directly inside do you mean like I have to have like void getAmicablePair right?
Code:
#include<iostream>
using namespace std;
class amicablePair
{
private:
int Answer1;
int Answer2;
int Sum1;
int Sum2;
int Divisor1;
int Divisor2;
int Divisor3;
public:
{Answer1 = 220;}
while(Answer1 <= 7000){
Sum1 = 0;
Divisor1 = 1;
Divisor2 = Answer1 / 2;
if(Answer1 % Divisor2 = 0){
Sum1 = Sum1 + Divisor2;
}
Divisor1++;
if(Sum1 > Answer1){
Answer2 = Sum1;
Sum2 = 0;
for(Divisor3 = 1)
Divisor4 = Answer2 / 2;
if(Answer2 % Divisor4 = 0){
Sum2 = Sum2 + Divisor4;
}
if(Sum2 = Answer1){
cout << &Answer1& " and " &Answer2& "are an amicable pair" << endl;
}
Divisor3++;
Answer1 = Answer1 + 1;
}
}
};
int main()
{
amicablePair;
return 0;
}
-
Assignment, not a comparison.
Code:
if(Sum2 = Answer1){
Assignment, not a comparison.
Where did you get that syntax from?
Wow, I completely missed that all that code was just thrown into the middle of the class definition. How does one even arrive at doing that, assuming one is reading a book and attending lectures?
-
Code:
int main()
{
amicablePair;
return 0;
}
A class can't be executed like that
and you have again put executable code in the public part...You are to provide a function in that part which "solves the problem" , create an object of the class in main() and call the function from that object...
Again...try it without classes first...if it works...read a tutorial or a book about object oriented programming...then just use the working code inside a class function....if you are sure that is really what your professor wants...
-
Wow! I don't even know where to start. All I can say is, you need to learn the basics first before you start with classes...
Specifically, you need to learn the difference between assignment & comparison, how to write a for loop, what & means, how to write a function, and how to call a function.
-
And how to write classes.
-
arnell22,
Isn't this a re-post of the Equation Wrong thread: http://cboard.cprogramming.com/cplus...ion-wrong.html? It looked like it ran in that thread.... Abstracting the operation into an object (class) could be a legitimate next learning objective.
Best Regards,