Another beginner question

This is a discussion on Another beginner question within the C++ Programming forums, part of the General Programming Boards category; This one is about object oriented programming. In java I could just add new files and have them be classes ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Another beginner question

    This one is about object oriented programming. In java I could just add new files and have them be classes so if I wanted a Player class in java I would make a file that said:

    Code:
    public class Player
    {
         public Player(int a, int b)
         {some stuff}
    
         --- some more methods
    }
    And in my driver I could say, Player p = new Player(1,2); or something.

    In C++ I'm not really sure what to do...here's what I have so far and its giving me an error about /clr options or something:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    public class Player 
    {
      public:
        double points,value;
    	string name, pos;
        Player (string,string,double);
    };
    
    Player::Player (string n, string p, double pts) 
    {
      name = n;
      pos = p;
      points = pts;
    }

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    1) You don't declare a class in C as public, it's scope is determined by either where the class was defined, or if in a header, which class has included the header.

    In C++ you don't have to dynamically allocate objects and have a reference like Java does by nature.

    Your class would be delared as you have. In the main method or wherever the class is used it can be declared as such
    Code:
    Player a("abc", "def", 12.0); //Constructs objects a
    Player* b = new Player("abc", "def", 12.0); //constructs an object dynamically as it would in java, 
    //b is only a pointer(reference) to a Playerobject
    
    //object can then be used as such
    *b.memberFunction(); //"dereferences" pointer, which means get the underlying object that b references
    b->memberFunction(); //uses the arrow op, 'b->' acts as '*b.'
    Last edited by indigo0086; 07-25-2007 at 07:55 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Change:
    Code:
    public class Player
    To:
    Code:
    class Player
    The public keyword used in this context is Java syntax, not C++ syntax.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Ok I did this, Player a(name, pos, ppoints); and I got a undeclared identifier error. Whasts the difference between doing Player a(stuff) and doing Player* a = new Player(stuff)

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the exact error.

    As to the difference between
    Player a(stuff) and doing Player* a = new Player(stuff)
    Player a(stuff) means that you create a variable a, of the type Player.

    Player *a = new Player(stuff) means that you care a variable a, that is a pointer to the type Player, which is then initialized to the memory location choosen by "new" for something of type Player.

    In many cases, the difference isn't much at all - which is "better" really depends on how you want to use the variable - it is quite a complex matter to choose between the two, as they have all sorts of different benefits and drawbacks. The typical case where you want to have pointers is when you want to store a reference to this player elsewhere - whilst it's possible to do that even when you don't create them with new, it's the most natural choice in such a case.

    --
    Mats

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Generally you want to use Player a(stuff) unless you have a good reason. Doing this allows the compiler to manage the lifetime of the object (it lasts until the end of the scope it is declared in).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Daved View Post
    Generally you want to use Player a(stuff) unless you have a good reason. Doing this allows the compiler to manage the lifetime of the object (it lasts until the end of the scope it is declared in).
    Yes, I completely agree with that.

    One of the biggest problems with modern software is that it "leaks memory", which means that if you run it for a long time, it uses more memory than it did in the beginning, without a change in the ACTUAL data being kept by the application. The cause for this is that the programmer did a "new" (or "malloc") without a corresponding "delete" (or "free"). Avoiding using pointers to things, and thus needing to use "new" is a good idea if it's possible (within reason - with experience you'll learn to know when you need one and when you need the other).

    --
    Mats

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    The typical case where you want to have pointers is when you want to store a reference to this player elsewhere - whilst it's possible to do that even when you don't create them with new, it's the most natural choice in such a case.
    To expand upon this, here's how you would use a pointer and a reference without creating a variable with new:
    Code:
    class Player a("abc", "def", 12.0); //Constructs objects a
    Player *ap = &a; //creates a pointer to type Player and puts the address of a in ap
    ap->memberFunction();
    Player &ar = a; //creates a reference and points it to a
    ar.memberFunction();
    Also, if you do use new, don't forget to use delete, or else you'll have a memory leak.
    Code:
    Player* b = new Player("abc", "def", 12.0); //constructs an object dynamically
    delete b;

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, robwhit has it right - I was tempted to write a bit about that, as well as what DaveD commented on - but tried to keep it brief. This is obviously a forum where people comment when the initial post isn't complete :-)

    --
    Mats

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    the error just says error C2065: 'Player' : undeclared identifier. I don't have Player defined in this file though, its in Player.cpp like I would do in java

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    You need to have the class definition in a header file that you then #include in any source file that needs to work with a specific instance of this class:

    class.h
    Code:
    class foo
    {
        int bar;
    public:
        foo();
    };
    class.cpp
    Code:
    #include "class.h"
    
    foo::foo() : bar(0)
    {
    }
    main.cpp
    Code:
    #include "class.h"
    
    int main()
    {
        foo myfoo;  // Create an instance of the class.
    
        ...
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Ok so I have a Player.h, a Player.cpp, and a main file. I left out some unimportant code. I'm getting a Cannot open include file: 'Player.h': No such file or directory

    Main:
    Code:
    #include <Player.h>
    int main () 
    {
    Player a(name, pos, ppoints);
    }
    Class:
    Code:
    #include <Player.h>
    Player::Player (string n, string p, double pts) 
    {
      name = n;
      pos = p;
      points = pts;
    }
    Header:
    Code:
    class Player 
    {
    	double points,value;
    	string name, pos;
    	public:
    		Player (string,string,double);
    };

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    When you #include a file, if you use <>'s around the filename, the include directory of your compiler is searched. If you use ""'s instead, then it searches the current directory, etc. etc..

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Ok I did "Player.h" instead of <Player.h> and now I have lots of errors:

    Code:
    class Player 
    {
    	double points,value;
    	string name, pos;
    	public:
    		Player (string,string,double);
    };
    Syntax error, missing ';' before identifier 'name'

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The compiler doesn't know the type "string", so you need to include the support for that.
    #include <string>

    --
    Mats

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

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