Hello guys i am in level of class and objects
i need some help and guide in copy contruct i search the thread
and found 1 http://www.daniweb.com/forums/thread21662.html
but this is not what i want... ok here is the problem..

i created contruct class ... i want in program that create objects
on user input.. like how many computer you need.. if user enter 5
then compiler create 5 objects of class computer.. after that it asks
for computer specification and compiler then copy some contents to other
4 objects ... i coded the program it works perfect but the copy method copies
every contents of 1st object into others.. how can i copy some contents ..
this scenario is done with array object. please guide me if there is any better idea.

#include "iostream"
#include "string"
using namespace std;
class cmptr
{
private:
	string rm;
	string hd;
	string nme;
	string agp;
	string mothrbrd;
	int lcd;
	int pcr;
public:
	void defaultinfo()
	{
		cout<<"PC SPEC ";
		cout<<"Enter pc name: ";
		cin>>name;
		cout<<"Enter processor speed: ";
		cin>>pcr;
		cout<<"Enter ram size: ";
		cin>>ram;
		cout<<"Enter motherboard model: ";
		cin>>mothrbrd;
		cout<<"Enter graphics card size: ";
		cin>>agp;
		cout<<"Enter lcd size: ";
		cin>>lcd;
	}

	void all_other_info()
	{
		cout<<"Enter processor speed: ";
		cin>>pcr;
		cout<<"Enter ram size: ";
		cin>>ram;
		cout<<"Enter motherboard model: ";
		cin>>mothrbrd;
	}

	void display()
	{
		cout<<"pc name: "<<name;
		cout<<"processor speed:"<<pcr;
		cout<<"ram size: "<<ram;
		cout<<"motherboard model: "<<mothrbrd;
		cout<<"graphics card size: "<<agp;
		cout<<"lcd size: "<<lcd;
	}

	cmptr(cmptr &p)
	{
		name = p.name;
		agp = p.agp;
		lcd = p.lcd;
		
	}
};

void main()
{

here are two codes this one works good 

	
        cmptr pc1;
	cout<<"spec for 1 pc"<<endl;
	pc1.defaultinfo();
	cout<<"spec for 2 pc:"<<endl;
	cmptr pc2=pc1;   // here is copy 3 variables of pc1 not all


        pc2.all_other_info();  // here it takes pc2 remaining values

	cout<<"result of 1 pc"<<endl;
	pc1.display();
	cout<<"result of 2 pc"<<endl;
        pc2.display();

// here is the real code which i am facing problem

	int a;
	cout<<"How many pc you want:";  // here i will enter 2 pc
	cin>>a;
	cmptr *pc = new cmptr[a];
	pc[0].defaultinfo();
	cmptr pc[1]=pc[0];          <--------- i get error from here
                                    i want that some contents of pc1 array copy to
                                    pc2 array contents but not working..
}

Recommended Answers

All 8 Replies

cmptr pc[1]=pc[0]; // this is a redefinition
pc[1] = pc[0]; // this is what you actually wanted, assuming a was at least 2

cmptr *pc = new cmptr[100]; // this would be a = 100
pc[0].defaultinfo(); // both totally ok
pc[99].defaultinfo(); // both totally ok
// pc is a pointer to memory segment containing 100 copies of the cmptr object in sequential order
// the number you pass to the [] operator is an "offset", which adds the size of an object in bytes to the address of pointer &array[0]
// so, &pc + 100 bytes might be pc[1] if each object is 100 bytes in memory, but this is kind of semantics

// hence:
cmptr pc[1]; // is totally wrong, you already gave pc[1] a definition.

// Edit: don't forget to delete the memory pc points to when you are done using it:
delete[] pc;

Good luck!

cmptr pc[1]=pc[0]; // this is a redefinition
pc[1] = pc[0]; // this is what you actually wanted, assuming a was at least 2

cmptr *pc = new cmptr[100]; // this would be a = 100
pc[0].defaultinfo(); // both totally ok
pc[99].defaultinfo(); // both totally ok
// pc is a pointer to memory segment containing 100 copies of the cmptr object in sequential order
// the number you pass to the [] operator is an "offset", which adds the size of an object in bytes to the address of pointer &array[0]
// so, &pc + 100 bytes might be pc[1] if each object is 100 bytes in memory, but this is kind of semantics

// hence:
cmptr pc[1]; // is totally wrong, you already gave pc[1] a definition.

// Edit: don't forget to delete the memory pc points to when you are done using it:
delete[] pc;

Good luck!

pc[1] = pc[0]; // this is what you actually wanted, assuming a was at least 2

pc[0] whole contents are copied to pc[1] ... but i want 3 contents to be copy. i mean agp, name of pc, and lcd just copy to pc[1]
please look what i am talking about
and thanks for the advice and guide for deleting array memory ...

You did not read my post.
You had a compiler error because you redefined the array:

cmptr pc[1]=pc[0]; // this is a redefinition

You defined the copy constructor yourself, so if you want it to do something different, then change it.

cmptr(cmptr &p)
	{
		name = p.name;
		agp = p.agp;
		lcd = p.lcd;
		
	}
// you didn't add most of the variables below
class cmptr
{
private:
	string rm;
	string hd;
	string nme;
	string agp;
	string mothrbrd;
	int lcd;
	int pcr;

...?

unimportant ... i dont know how to copy array contents to other array using constructor... i will try my best...

No no, you already did it.

cmptr(cmptr &p)
{
  name = p.name;
  agp = p.agp;
  lcd = p.lcd;
}

The code above is from your code.

constructors are defined like this:

classname( args ) : class_initialization() { // post initialization code }
// in this case
cmptr( cmptr &p ) {
// means that if you construct this object with the address of this object as an argument, do whatever is where this comment is
}
// so when you create an instance of the object like this
cmptr a(); // default constructor
cmptr OTHER(a); // copy constructor, OTHER is now constructed based on the constructor that accepts an address (or pointer to) another object of the same type
// when you invoke the assignment operator '=' and you have not defined an
// overload for assignment, by default the copy constructor is invoked if the
// type is the same.
// Here we have an assignment:
cmptr pc_a();
cmptr pc_b = pc_a;
// which invokes the copy constructor

Good luck!

Edit: I changed my mind. Based on what you are doing, the assignment overload is necessary, as pc[1] would be initialized already by the default constructor when you create it with new, as you did not pass any construction arguments.

cmptr & operator=( const cmptr p ) {
  name = p.name;
  agp = p.agp;
  lcd = p.lcd;
  return *this;
}

That's an example of how to overload your assignment operator.

unimportant thanks for the help and guide ... these codes works perfect.. but you know
these are just objects that created, but my actual work is in array object.

comptr *pc = new comptr[49]; //range 1-50
as when i code this
pc[0]. blah blah();

pc[1]=pc[0];        <---- this copy all variables from pc[0] array.
comptr pc[1]=pc[0]; <------- this give me (IntelliSense: initialization with '{...}'
                                 expected for aggregate object)

i think there is no way to get array1 objects specific items and put into array 2 objects.
i search everywhere but no use so i will change my mind and code into simple 2 objects not array objects.
i really appreciate your help...

No, I told you from the start, this line of code is just wrong:
comptr pc[1] = pc[0]; // You can't do this.

Your compiler is telling you that's impossible.

pc[1] is already comptr
you are making it comptr "again"
comptr pc[1] <--- is bad because:
comptr *pc = new comptr[50]; // range 0 - 49, --> this is 50 comptr objects

This code also works:

comptr *pc; // note no new yet
pc = new comptr[50]; // there are 50 comptr objects now
// *pc is not a computer, it is a -pointer-
// it points to the beginning of the array (&comptr[0])
// pc[1] means, &comptr[0] + size in bytes of comptr (&comptr[1])

// so when you do this
pc[0].defaultinfo();
pc[1] = pc[0];
pc[2] = pc[1];
pc[3] = pc[0];

// all objects from pc[0] to pc[3] are equal to the value of pc[0]
// they are separate objects
pc[1].defaultinfo(); // now you can change pc[1]
// pc[0], pc[2], and pc[3] are still the same, they are their own object

comptr pc[4]; // ERROR. YOU ALREADY DEFINED PC[4], it is a comptr object -already-

You said:
"i think there is no way to get array1 objects specific items and put into array 2 objects."
and that's wrong, I am showing you how to do that.

ohhhhhhhh GOD i did it !!!
here what i was talking unimportant

pc[1].copydefault(pc[1] , pc[0]);

copydefault function will send two objects
and then object_1 (2 strings name agp) and (1 int lcd) value are copied to object_2
while object_1 contains 5 strings 2 int.

void copydefault(automobile &pc_1, automobile &pc_O) {
		pc_1.name = pc_O.name;
		pc_1.agp = pc_O.agp;
		pc_1.lcd = pc_O.lcd;
}

unimportant thanks about contruct and pointer actually i directly jumped
to OOP without clearing my concept of pointers & references.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.