#constructors c++

24 messages · Page 1 of 1 (latest)

void jewel
#

How do you write the specialized constructor for this class?

{
public:
// add methods
    //Car();
    //Car(const char *_pName, int a, int b);
    //Car(const Car & r);
    //Car &operator=(const Car &) ;
    //~Car();

    

    // do not add data
    Car *pNext;
    Car *pPrev;
    const char *pName;
    int a;
    int b;
};```
harsh bloomBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

little helm
#

Show us what you've tried

void jewel
#
    :a(a), b(b), pNext(nullptr), pPrev(nullptr), pName(_pName)
{
    
}```
#

this is main: Car *pA = new Car("A",11,14); Car *pB = new Car("B",12,15); Car *pC = new Car("C",13,16); but when i try to call it it says: 'pA': local variable is initialized but not referenced

austere atlas
#

Btw, why are you using new?

void jewel
#

yes im using new

austere atlas
#

Why

void jewel
#

this is for an assignment and im having problems with my constructor i dont know how to implement the constructors when it calls new

austere atlas
#

You did it

#

New makes no difference

void jewel
#

well we arent supposed to modify the main we were supposed to do a deep copy

#

and im having trouble for that too i dont know where to start

austere atlas
#

I presume you're just meant to deep copy some list, not that the copy constructor is meant to be deep?

void jewel
#

ahh yes i wasnt meant to deep copy this thank you

#
    :pName(nullptr), a(0), b(0), pNext(nullptr), pPrev(nullptr)
{

}
Car::Car(const char* _pName, int a, int b)
    :a(a), b(b), pNext(nullptr), pPrev(nullptr),pName(_pName)
{
    
}
Car::Car(const Car& r)
    :a(r.a), b(r.b), pNext(nullptr), pPrev(nullptr), pName(r.pName)
{
    
}
Car& Car::operator=(const Car& r)                                                                                                                
{
    a = r.a;
    b = r.b;
    pName = r.pName;
    return *this;
}
Car::~Car()
{
    
}```
#

this is my constructors and when i calll main ```Car *pA = new Car("A",11,14);
Car *pB = new Car("B",12,15);
Car *pC = new Car("C",13,16);

#

i tried delete [ ] pName but it does not work

austere atlas
#

You need delete pA to delete the first car, for example

void jewel
#

i see, is there a way i can do this in the destructor?

austere atlas
#

No. The destructor runs after delete (or scope exit)

harsh bloomBOT
#

@void jewel Has your question been resolved? If so, type !solved :)

void jewel
#

!solved