using classes

This is a discussion on using classes within the C++ Programming forums, part of the General Programming Boards category; Im have trouble with creating classes. I got the private part down packed but the public part is where I ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    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;
    		}
    	}
    };

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    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;
    }

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Code:
    for(Divisor3 = 1)
    Assignment, not a comparison.

    Code:
    if(Sum2 = Answer1){
    Assignment, not a comparison.

    Code:
    &Answer1&
    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?
    Last edited by rags_to_riches; 04-18-2011 at 04:45 PM.

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    And how to write classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    arnell22,

    Isn't this a re-post of the Equation Wrong thread: Equation wrong? It looked like it ran in that thread.... Abstracting the operation into an object (class) could be a legitimate next learning objective.

    Best Regards,
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  2. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 03:36 AM
  3. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  4. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  5. Classes inside Classes
    By LostGeneration in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2003, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21