C++ Object and Class *****

K

Kumar Anurag

Implement a class which can only have two objects e.g. obj1,obj2.Now
when obj3 is created ,then obj1 should get destroyed.. This process
will be repeated . It means at any instant we will have only two
objects.
I did is using copy constructor is it right??

#include<iostream>
using namespace std;

class A{

public:
A(A& Old){
Old.~A();//here we destroyed the old object2("b")
}

};

int main()
{
A a; //Object1
A b; //Object2
A c(b);//Suppose i want to create object3 "c"
//Here we pass that object in parameter which we want to get
//Destroyed (here i want Object2("b") to get destroyed;



return 0;
}
/*whenever i need to create new object i will pass as
Format:-> A newObject(oldObject);
*/

is it correct??/
 
A

AnonMail2005

Implement a class which can only have two objects e.g. obj1,obj2.Now
when obj3 is created ,then obj1 should get destroyed.. This process
will be repeated . It means at any instant we will have only two
objects.
I did is using copy constructor is it right??

#include<iostream>
using namespace std;

class A{

  public:
     A(A& Old){
       Old.~A();//here we destroyed the old object2("b")
       }

};

int main()
{
 A a; //Object1
 A b; //Object2
 A c(b);//Suppose i want to create object3 "c"
        //Here we pass that object in parameter which we want to get
        //Destroyed (here i want Object2("b") to get destroyed;

return 0;}

/*whenever i need to create new object i will pass as
Format:-> A newObject(oldObject);
*/

is it correct??/

At the very least, you can't do this with objects that have been
allocated on the stack. When you exit scope, I think your b instance
will be destructed a second time.

Perhaps you can do this for heap allocated objects, but I would ask
what are you really trying to accomplish?
 
K

Kumar Anurag

At the very least, you can't do this with objects that have been
allocated on the stack.  When you exit scope, I think your b instance
will be destructed a second time.

Perhaps you can do this for heap allocated objects, but I would ask
what are you really trying to accomplish?

Suppose we have a game in which only 2 players can play at a time. Now
3rd player wants to enter, then one of them should leave , and the 3rd
player will take the place. Players are here instances (objects) .How
this can be done??
 
S

Salt_Peter

Implement a class which can only have two objects e.g. obj1,obj2.Now
when obj3 is created ,then obj1 should get destroyed.. This process
will be repeated . It means at any instant we will have only two
objects.
I did is using copy constructor is it right??

#include<iostream>
using namespace std;

class A{

  public:
     A(A& Old){
       Old.~A();//here we destroyed the old object2("b")
       }

};

int main()
{
 A a; //Object1
 A b; //Object2
 A c(b);//Suppose i want to create object3 "c"
        //Here we pass that object in parameter which we want to get
        //Destroyed (here i want Object2("b") to get destroyed;

return 0;}

/*whenever i need to create new object i will pass as
Format:-> A newObject(oldObject);
*/

is it correct??/

no, thats incorrect.
Somebody somewhere needs to manage the lifetimes of the active
objects. You can track the number of objects active using a static
variable internal to the class, but that won't solve the issue.
 
V

Victor Bazarov

Kumar said:
[..]
Suppose we have a game in which only 2 players can play at a time. Now
3rd player wants to enter, then one of them should leave , and the 3rd
player will take the place. Players are here instances (objects) .How
this can be done??

Use dynamic allocation.

Create an intermediate object that would hold the "player", something like

class PlayerHolder {
Player *pPlayer;
public:
PlayerHolder() : pPlayer(0) {}
void changeTo(Player* other) {
delete pPlayer; // PlayerHolder owns the Player obj!
pPlayer = other;
}
bool empty() const { return pPlayer == 0; }
void clear() { changeTo(0); }
};

In your game you need to have N number of PlayerHolder objects. A new
player would be swapped in.

class Game {
PlayerHolder ph1, ph2;
public:
bool canAddPlayer() const { return ph1.empty() || ph2.empty(); }
void swapPlayer1(Player* p) { ph1.swap(p); }
void swapPlayer2(Player* p) { ph2.swap(p); }
...
};

You didn't state what conditions cause any particular player to be
swapped out, perhaps the score...

Dynamic allocation provides great flexibility.

V
 
N

Noah Roberts

Implement a class which can only have two objects e.g. obj1,obj2.Now
when obj3 is created ,then obj1 should get destroyed.. This process
will be repeated . It means at any instant we will have only two
objects.
I did is using copy constructor is it right??

#include<iostream>
using namespace std;

class A{

public:
A(A& Old){
Old.~A();//here we destroyed the old object2("b")
}

};

int main()
{
A a; //Object1
A b; //Object2
A c(b);//Suppose i want to create object3 "c"
//Here we pass that object in parameter which we want to get
//Destroyed (here i want Object2("b") to get destroyed;



return 0;
}
/*whenever i need to create new object i will pass as
Format:-> A newObject(oldObject);
*/

is it correct??/

It's tempting to give you the answer but this is obviously a homework
assignment so I'll have to give you hints.

In order to be able to destroy an object when you construct a new one
(and not simply when it leaves scope) you're going to need it on the
heap. You'll need to enforce this issue or hide it. This means that
either your constructor must be private or the object the user creates
must simply refer to a private object that it governs.

Which one you do will be largely governed by further requirements. Does
the user need to be able to create objects on the stack? (local
objects) When c is created is a then invalid or replaced by c?

To get the absolute most freedom yet still enforce the "only two, ever"
rule the latter option would be best. This means you'll want to look up
the handle/body or pimpl idiom.

As someone else said though, no...doesn't look like you've met the
requirements of the assignment since I could create c without using the
copy constructor and then there's three of them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top