rmvprasad
asked on
constructor inside a constructor
Can I create a constructor inside an other constructor, by passing the parameters of the first constructor to the second constructor
Sample code:
Reservation::Reservation(T rucks* tptr, Customer* cptr)
{
cout<<"\nEnter Reservation date";
cin>>Res_Date;//overloadin g required
cout<<"\nEnter Pick Date";
cin>>Pick_Date;
cout<<"\nEnter Reservation id";
cin>>Res_Id;
cout<<"\nEnter the contract time period";
cin>>Contract;
cout<<"\nEnter VIN number";
cin>> (tptr->VIN);
cout<<"\nEnter License number";
cin>>(cptr->Licence_No);
//////////now passing these values to create a onject
Reservation R=new Reservation(Res_Date,Pick_ Date, Res_Id,Contract,tptr->VIN, cptr->Lice nce_No);
}
Sample code:
Reservation::Reservation(T
{
cout<<"\nEnter Reservation date";
cin>>Res_Date;//overloadin
cout<<"\nEnter Pick Date";
cin>>Pick_Date;
cout<<"\nEnter Reservation id";
cin>>Res_Id;
cout<<"\nEnter the contract time period";
cin>>Contract;
cout<<"\nEnter VIN number";
cin>> (tptr->VIN);
cout<<"\nEnter License number";
cin>>(cptr->Licence_No);
//////////now passing these values to create a onject
Reservation R=new Reservation(Res_Date,Pick_
}
The parameters of the second constructor seem to be different from the first one ...... in that case it should not be a problem
BTW... Be careful about copy ctors and the assignment operator. Probably you ought to make them private to avoid having to think about them.
>> Can I create a constructor inside an other constructor
>> Yes, it works.
I got the impression that question and answers talk of two different things:
If you want to implement one constructor by calling a second constructor like that:
class A
{
int m_i;
public:
A(bool b) { m_i = b? 1 : 0; }
A(int i) : A(false) {}
};
you'll get an error 'illegal member initialization: 'A' is not a base or member'.
So, you could only use another constructor to construct the 'this' object by using a baseclass constructor.
Regards, Alex
>> Yes, it works.
I got the impression that question and answers talk of two different things:
If you want to implement one constructor by calling a second constructor like that:
class A
{
int m_i;
public:
A(bool b) { m_i = b? 1 : 0; }
A(int i) : A(false) {}
};
you'll get an error 'illegal member initialization: 'A' is not a base or member'.
So, you could only use another constructor to construct the 'this' object by using a baseclass constructor.
Regards, Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops, forgot to state input's return type, but you probably noticed that (Reservation *, of course).
Thanks fo accepting the answer anyway (;-).
Thanks fo accepting the answer anyway (;-).
Illustration:
--------8<--------
#include <iostream>
using std::cout;
class foo {
foo *p;
public: foo() : p(0) {
std::cout << "ctor()\n";
}
foo(int) : p(new foo) {
std::cout << "ctor(int)\n";
}
~foo() {
std::cout << "dtor (p=" << static_cast<void*>(p) << ")\n";
delete p; // Deleting a NULL pointer is OK per the standard
}
};
int main()
{
foo p(1);
}
--------8<--------