6 Replies - 654 Views - Last Post: 06 July 2011 - 01:14 PM Rate Topic: -----

#1 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

how to pass one instance of a class without using "new" ?

Posted 06 July 2011 - 09:59 AM

I am trying to make a simple console Tennis game. Problem is I want to create an instance of the second player without using the "new" keyword. I can perform the methods on player one, but I dont know how to write the same for the second player (which is being passed in the play() function from main().

For clarity, i have commented in the code below where I am facing the problem.. Thanks in advance.

void Tennis :: play(Tennis & playertwo) { // Gets two players declared in the main function


	bool serviceGame = 0;

	do
	{
          cout <<"\n Set: "<< set <<endl;
          cout <<"\n Game: "<<game <<endl;
          cout <<"\n Player 1: "<< name <<endl;
          cout <<"\n Player 2: "<< playertwo.name <<endl;
          
		serviceGame = ( rand()%1+0 );
			if ( serviceGame )
			{
                               cout << endl << name << " wins the point " <<endl;
                
				Tennis::winPoint( ); //player one wins the point
							if ( point == 40 ) 
							{
							point = 'A';
							}
							else if ( point == 'A' ) 
							{
								Tennis::winSet( playertwo );
							}
                            playertwo.winPoint(); //player 2 wins point

                                                      if ( playertwo.point == 40 ) 
							{
							playertwo.point = 'A';
							}
							else if ( playertwo.point == 'A' ) 
							{
	                                             playertwo.winSet( ?? ); 
          //!! ***** This is where I am facing problem. How do I pass the player one instance in this function?? 
							}

			}
	
	} while ( game != 0 );



Is This A Good Question/Topic? 0
  • +

Replies To: how to pass one instance of a class without using "new" ?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: how to pass one instance of a class without using "new" ?

Posted 06 July 2011 - 10:17 AM

Looks like a design problem... Tennis is the game; why would player be an instance of Tennis?

struct Player {
	string name;
	char point;
	
	void winPoint() {
		cout << endl << name << " wins the point " <<endl;
		/* probably some code here */
		if ( point == 40 ) {
			point = 'A'; // char and numeric?  are you sure
		} else if ( point == 'A' ) {
			winSet();
		}
	}
	void winSet();
};

struct Tennis {
	void play(Player &, Player &);
	// your stuff
}

void Tennis::play(Player &p1, Player &p2) {
	// WTF 
	// bool serviceGame = 0;
	// you don't need it anyway
	
	do {
		cout <<"\n Set: "<< set <<endl;
		cout <<"\n Game: "<< game <<endl;
		cout <<"\n Player 1: "<< p1.name <<endl;
		cout <<"\n Player 2: "<< p2.name <<endl;
          
		if ( rand()%1 == 0 ) {
			p1.winPoint();
		} else {
			p2.winPoint();
		}
	} while ( game != 0 );
}


Was This Post Helpful? 1
  • +
  • -

#3 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Re: how to pass one instance of a class without using "new" ?

Posted 06 July 2011 - 10:44 AM

Thanks.

You used structures, so is it the only way to use instances without new keyword? What would be the problem if used in classes?

I am not too fond of structures and don't know how to use it properly. I prefer classes.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: how to pass one instance of a class without using "new" ?

Posted 06 July 2011 - 10:49 AM

Structures and classes are the same thing; only the default protection level differs. C++ is sneaky that way.

You don't need new unless you must create something dynamically.
e.g.
int main() {
   Player bob, alice;
   Tennis game;
   bob.name = "Bob";
// or, if you have a class with a method
   alice.setName("Alice");
   game.play(bob, alice);



See? No new needed.
Was This Post Helpful? 1
  • +
  • -

#5 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Re: how to pass one instance of a class without using "new" ?

Posted 06 July 2011 - 12:57 PM

Hi,

I am confused about a few things. Thing is, I am using separate files for declarations and definitions.
So when declaring struct how can I give its variables a default value like I can make in classes using constructors?
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how to pass one instance of a class without using "new" ?

Posted 06 July 2011 - 01:03 PM

structs are just classes that default to a "public" modifier. So do it exactly as you would with a class.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: how to pass one instance of a class without using "new" ?

Posted 06 July 2011 - 01:14 PM

Syntax is identical.

This:
class Foo {
public:
   int n;
   Foo() : n(42) { } 
};


Is exactly the same as:
struct Foo {
   int n;
   Foo() : n(42) { } 
};



If you like, never use the term "struct" in C++ ever again and you'll be fine. Your call. In practice, programmers tend to like struct for simple things, class for something with more brains, methods, etc.

When you declare a class/struct in C++, the constructor is called. With "new", it's called at that time. With a standard declaration, it's called when stated. You can test this:
class Player {
private:
   string name;
public:
   Player(const string &n) : name(n) { cout << "Player " << name << " is here!" << endl; }
   Player() : name("Unknown") { cout << "The Player with no name is here!" << endl; }
   ~Player() { cout << name << " dies." << endl; }
};

int main() {
   Player p1("Alice"), p2, *p3;
   cout << "Ready" << endl;
   p3 = new Player("Bob");
   cout << "Set" << endl;
   delete p3;
   cout << "Oops" << endl;

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1