basic object/class question

This is a discussion on basic object/class question within the C++ Programming forums, part of the General Programming Boards category; What am I not understanding? Code: class Player { public: Player(); void talk(); }; void Player::talk() { cout<<"Yo"; } int ...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    basic object/class question

    What am I not understanding?
    Code:
    class Player
    {
                    public:
    	Player();
      	void talk();
    };
    
    void Player::talk()
    {
    cout<<"Yo";
    }
    
    int main()
    {
    	Player::Player();
                    Player().talk();
    return 0;
    }
    AIM: MarderIII

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You need a Player instance in main.


    Player a;

    a.Talk();
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    There is another problem.

    A class type is used just like a basic type. If you wanted to declare an int named number, you would do this:

    int number;

    Similarly, when you want to declare a variable of type Player, you do this:

    Player a_player;

    That calls the default constructor. You declared the default constructor:

    Player();

    and since you declared it, you have to define it. Since you don't want it to do anything, you would define it like this:

    Player()
    {
    }

    Typically, with a short function, you would define it inside the class like this:
    Code:
    class Player
    {
    public:
    	Player(){}
      	void talk();
    };
    The compiler provides the default constructor for you, so you don't have to declare and define it if you don't want to.

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Originally posted by nvoigt
    You need a Player instance in main.


    Player a;

    a.Talk();
    Im not sure what you mean, I tried to add that but it said that talk was not a member of player and I don't know...but the below code works I guess

    Code:
    #include <iomanip>
    #include <iostream>
    #include <limits>
    #include <fstream>
    #include <stdlib>
    #include <conio>
    #include <time>
    #include <ctype>
    
    class Player
    {
           public:
           Player::Player(int x,int y,int z)
          {
                one = x;
                two = y;
                three = z;
          }
          void talk();
    
      private:
      int one, two, three;
    };
    
    void Player::talk()
    {
    cout<<"Yo"<<one<<two<<three;
    }
    
    int main()
    {
    	int x=1,y=2,z=3;
    	Player::Player(x,y,z);
                    Player(x,y,z).talk();
    return 0;
    }
    Last edited by Noobie; 05-16-2003 at 05:45 AM.
    AIM: MarderIII

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Try again:

    Code:
    int main()
    {
     int x=1,y=2,z=3;
    
     Player p(x,y,z);
           
     p.talk();
    
     return 0;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  6. #6
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Wuts the difference between

    Code:
       Player p(x,y,z);
       p.talk();
    and

    Code:
    	Player::Player(x,y,z);
       Player(x,y,z).talk();
    AIM: MarderIII

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    using a class is similar to using a struct in many ways. You define the type/class, and then you have to declare an instance of it...which is what P is in the previous example. Player is just the name of your class...P is the variable.

    Kinda like "int g;" Int is the type, g is the variable. Here, Player is the type, P is the variable.

    Hope that helps.
    Away.

  8. #8
    Cat
    Guest
    Code:
       Player p(x,y,z);
       p.talk();
    This is the good way of doing things. You create a new Player object called p. You then call a member function of p.

    Code:
       Player::Player(x,y,z);
       Player(x,y,z).talk();
    This is bad. The first line creates a new Player object but doesn't assign it to a variable name (essentially, you just wasted stack space creating an object you can never use). The first line is completely useless except if your goal is to waste space.

    The second line creates a second new Player object, with the same parameters as the other one (it is NOT the same object), calls one method on that object, and then the object, just like the first one, is "lost on the stack" -- you can't ever touch that object again.

  9. #9
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Wuts the difference between.....
    I think you have a problem between the distinction of a class and an object.
    When you declare Player as a class you are only saying to the compiler what data(data-members) it contains and what it can do(member-functions). You do NOT allocate memory for a Player(donīt really know if this is true if the class contains static data-members). On the other hand when you declare an object you are creating an instance of a class, and memory is allocated.

    Create a player named p(define)
    Code:
    Player p(x,y,z);
    Tell the compiler what a player is(declare)
    Code:
    class Player
    {
           public:
           Player::Player(int x,int y,int z)
          {
                one = x;
                two = y;
                three = z;
          }
          void talk();
    
      private:
      int one, two, three;
    };
    Do you get the distinction???
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 03:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM

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