Classes

This is a discussion on Classes within the C++ Programming forums, part of the General Programming Boards category; Yes, this is a newbie question, and yes, I do expect to get flamed for it. I'm not really understanding ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    12

    Question Classes

    Yes, this is a newbie question, and yes, I do expect to get flamed for it. I'm not really understanding the whole structure of classes, declaring variables inside of them, etc. I know that this source code is (duh) incorrect and won't compile. I would like if someone could rewrite the code so it is correct and compiles, and to further advance my learning of the language.

    Code:
    #include <stdafx.h>
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    class box
    {
    	static food;
    	food = 4
    }
    
    int main ()
    {
    	cout<<"The box has "<< box::food<< "pieces of food in it";
    
    	return 0;
    
    }
    Fire away.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    Mind helping?

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Bumping after 10 minutes is pretty weak.

    Here's a start:
    Code:
    class box
    {
            // Members of classes are private by default, so this is inaccessible outside the class.
    	static food; // food does not have a data type
    	food = 4 // no terminating semi-colon
    } // no terminating semi-colon

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Yes, you might get flamed. Not because of the question but because of:
    1. The stupid unreadable green text
    2. For asking a rewrite of the code rather than find out what the problem is
    3. Not providing us with the actual error message the compiler throws
    4. Bumping way too quickly

    If you wouldn't do those four points I'd actually be writing a solution now. And probably dozens of others would have, already.

    So I suggest... Try again in the same thread. If it's decent I bet you'll get answer within a couple of hours.

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    food = 4 // you can't instantiate static members inside the class definition. move it outside and make sure to qualify it as box::food and don't forget the data type again.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    The basic concept behind classes is that the program relies more to data than to actions! In other words, you can take two objects of the same class and provide them with data that'll make them react differently.

    Consider a variable like int or float to be a class ( although it isn't ). Let's declare some integers:

    Code:
    int a;
    int b(0);
    int c = 123;
    int d(b);
    int e = d;
    Each of the above are independant from each other, each holding it's own value and performing it's own operations.

    A class is like a species, like us humans!
    An instance of a class is like me, you, each one who reads this topic etc. We are all built the same way, but given different data that make us respond differently to the same actions!!! ( I know that's a rough example but it'll do )

    That's how a class is defined:

    Code:
    class someClass
    {
        public: // Everything under this can be used by anyone
          someClass(); // Default Constructor, optional
          ~someClass(); //Destructor, optional
          
          /* Here public functions and variables are declared */
         
        protected: // Everything under this can be used only by this class's members and descendants   
        
         /* Here protected functions and variables are declared */
     
        private: // Everything under this can be used only by this class's members
          
        /* Here private functions and variables are declared */  
    };
    You cannot make use of any operation inside the braces of a class and out the braces of a member function!

    Get started with the above AND understand them before you go on. OOP is very exciting once you get the hand of it!

    PS: Why do you include <cmath> and <stdafx.h> ??

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sipher View Post
    A class is like a species, like us humans!
    An instance of a class is like me, you, each one who reads this topic etc. We are all built the same way, but given different data that make us respond differently to the same actions!!! ( I know that's a rough example but it'll do )
    Yeah, it is a slightly weak analogy in the sense that a class is a clearly defined real thing which can produce an object, whereas humans are more "procedural": being human is an abstraction of the individual human, and not vice versa. Human instances are created by other human instances.

    Classes make it easier to organize your code by providing a framework so you do not have to think about "how" so much -- you just do. This generic framework also makes it easier for other people to understand your code.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by MK27 View Post
    being human is an abstraction of the individual human, and not vice versa. Human instances are created by other human instances.
    I really haven't thought of that! My logic is that we are all built the same way, by the same materials, and our unique data are what our parents and our society give us as standards!

    The church way: God is the class, and we are the instances!!!!!

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    Quote Originally Posted by Sipher View Post
    I really haven't thought of that! My logic is that we are all built the same way, by the same materials, and our unique data are what our parents and our society give us as standards!
    You believe in delivery by stork too?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by Sipher View Post
    The church way: God is the class, and we are the instances!!!!!
    Err, no. That would imply we are "gods" or "god", but clearly, even by church philosophy, we are not. No?
    Human is the class and individuals are us is a better analogy.
    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.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by grumpy View Post
    You believe in delivery by stork too?
    What do you mean by that??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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