-
C++ (
http://www.daniweb.com/forums/forum8.html)
| mbayabo | Oct 10th, 2008 5:18 am | |
| [need help] Initializing Objects in Control Structures I'm having trouble initializing Objects while they're inside if else statements. I'm using Visual C++ 2008.
for instance:
class Account {
Account();
Account(int = idnum, double creditLimit = 500.00);
Account(double initBal, int idnum, double creditLimit = 100.00);
public:
somefunction();
};
if (a > 0 && b > 0) {
Account customer(a,b);
} else {
Account customer;
}
customer.somefunction();
it would me this error:
Quote: error C2065: 'customer' : undeclared identifier
error C2228: left of '.somefunction' must have class/struct/union | i know that this means, i just don't know if there's a better way to do this.
i'm required to take account information and initialize the object depending on the user's input.
i was thinking i'd use if else statements then create the object, but apparently that doesn't work. |
| ithelp | Oct 10th, 2008 7:41 am | |
| Re: [need help] Initializing Objects in Control Structures No need to have this if else , use customer(a,b) , in the constructor check if a>0 , b>0 if no set the default value. |
| ArkM | Oct 10th, 2008 10:36 am | |
| Re: [need help] Initializing Objects in Control Structures You declare local variables in if-else alternatives. Of course, they were discarded outside if-else statement:
if (a > 0 && b > 0) { // New block started
Account customer(a,b); // Local variable defined (on the stack)
} else { // Prev. bock is ended. Another block started.
Account customer; // Local variable defined
} // All customers dead... RIP...
// Alas, no customers here... |
| sidatra79 | Oct 10th, 2008 12:23 pm | |
| Re: [need help] Initializing Objects in Control Structures Hi maybe the following suits your problem:
#include <iostream>
class Account {
public:
Account() : initBal(0),idnum(0),creditLimit(0.0) {}
Account(int id): idnum(id)
{
initBal=100;
creditLimit=500.0;
}
Account(int balance,int id): initBal(balance),idnum(id)
{
creditLimit=100.0;
}
public:
void somefunction()
{
std::cout<<"Initial balance is: "<<initBal<<std::endl;
std::cout<<"id is: "<<idnum<<std::endl;
std::cout<<"credit Limit is: "<<creditLimit<<std::endl;
}
Account& operator=(const Account& other)
{
if (&other != this)
{
initBal = other.initBal;
idnum = other.idnum;
creditLimit = other.creditLimit;
}
return *this;
}
private:
int initBal;
int idnum;
double creditLimit;
};
int main()
{
Account customer;
int a,b;
std::cout<<"please enter a value for a: "<<std::endl;
std::cin>>a;
std::cout<<"please enter a value for b: "<<std::endl;
std::cin>>b;
if (a > 0 && b == 0)
{
Account alocalcustomer(a);
customer = alocalcustomer;
}
else if (a > 0 && b > 0)
{
Account anotherlocalcustomer(a,b);
customer = anotherlocalcustomer;
}
customer.somefunction();
return 0;
}
Which gives for examples following outputs depending on what u enter:
//---------1st case :
please enter a value for a:
0
please enter a value for b:
-1
Initial balance is: 0
id is: 0
credit Limit is: 0
//---------2nd case :
please enter a value for a:
1
please enter a value for b:
0
Initial balance is: 100
id is: 1
credit Limit is: 500
//---------3rd case :
please enter a value for a:
2
please enter a value for b:
320
Initial balance is: 2
id is: 320
credit Limit is: 100 |
| ArkM | Oct 10th, 2008 5:42 pm | |
| Re: [need help] Initializing Objects in Control Structures It's a good idea but not so good implementation. No need to define assignment operator: it's exactly default assignment. Keep it simpler:
Account customer;
...
if (a > 0 && b > 0)
customer = Account(a,b);
customer.somefunction(); |
| sidatra79 | Oct 10th, 2008 7:52 pm | |
| Re: [need help] Initializing Objects in Control Structures For sure but it seemed to me, that he wanted to use the "some.function" outside the local scopes of the if statements while initializing objects there.:D
So I tried to give him such a solution.
Quote: Originally Posted by ArkM (Post 709768) It's a good idea but not so good implementation. No need to define assignment operator: it's exactly default assignment. Keep it simpler:
Account customer;
...
if (a > 0 && b > 0)
customer = Account(a,b);
customer.somefunction(); | |
| chunalt787 | Oct 11th, 2008 2:43 pm | |
| Re: [need help] Initializing Objects in Control Structures Your problem is that the account objects are going out of scope when the if statement ends and their destructor's are being called. Initialized it outside of the if else statement and then just modify it as needed in the if statements. If you don't know what I mean by going out of scope just do a quick Google search. This is something every programmer should know about. |
| All times are GMT -4. The time now is 2:01 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC