[need help] Initializing Objects in Control Structures

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 2
Reputation: mbayabo is an unknown quantity at this point 
Solved Threads: 0
mbayabo mbayabo is offline Offline
Newbie Poster

[need help] Initializing Objects in Control Structures

 
0
  #1
Oct 10th, 2008
I'm having trouble initializing Objects while they're inside if else statements. I'm using Visual C++ 2008.

for instance:

  1. class Account {
  2. Account();
  3. Account(int = idnum, double creditLimit = 500.00);
  4. Account(double initBal, int idnum, double creditLimit = 100.00);
  5. public:
  6. somefunction();
  7. };
  8.  
  9. if (a > 0 && b > 0) {
  10. Account customer(a,b);
  11. } else {
  12. Account customer;
  13. }
  14.  
  15. customer.somefunction();

it would me this error:

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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,854
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: [need help] Initializing Objects in Control Structures

 
0
  #2
Oct 10th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: [need help] Initializing Objects in Control Structures

 
0
  #3
Oct 10th, 2008
You declare local variables in if-else alternatives. Of course, they were discarded outside if-else statement:
  1. if (a > 0 && b > 0) { // New block started
  2. Account customer(a,b); // Local variable defined (on the stack)
  3. } else { // Prev. bock is ended. Another block started.
  4. Account customer; // Local variable defined
  5. } // All customers dead... RIP...
  6. // Alas, no customers here...
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: [need help] Initializing Objects in Control Structures

 
0
  #4
Oct 10th, 2008
Hi maybe the following suits your problem:

  1. #include <iostream>
  2.  
  3. class Account {
  4. public:
  5. Account() : initBal(0),idnum(0),creditLimit(0.0) {}
  6. Account(int id): idnum(id)
  7. {
  8. initBal=100;
  9. creditLimit=500.0;
  10. }
  11. Account(int balance,int id): initBal(balance),idnum(id)
  12. {
  13. creditLimit=100.0;
  14. }
  15. public:
  16. void somefunction()
  17. {
  18. std::cout<<"Initial balance is: "<<initBal<<std::endl;
  19. std::cout<<"id is: "<<idnum<<std::endl;
  20. std::cout<<"credit Limit is: "<<creditLimit<<std::endl;
  21. }
  22. Account& operator=(const Account& other)
  23. {
  24. if (&other != this)
  25. {
  26. initBal = other.initBal;
  27. idnum = other.idnum;
  28. creditLimit = other.creditLimit;
  29. }
  30. return *this;
  31. }
  32. private:
  33. int initBal;
  34. int idnum;
  35. double creditLimit;
  36. };
  37.  
  38. int main()
  39. {
  40. Account customer;
  41. int a,b;
  42.  
  43. std::cout<<"please enter a value for a: "<<std::endl;
  44. std::cin>>a;
  45. std::cout<<"please enter a value for b: "<<std::endl;
  46. std::cin>>b;
  47.  
  48. if (a > 0 && b == 0)
  49. {
  50. Account alocalcustomer(a);
  51. customer = alocalcustomer;
  52. }
  53. else if (a > 0 && b > 0)
  54. {
  55. Account anotherlocalcustomer(a,b);
  56. customer = anotherlocalcustomer;
  57. }
  58. customer.somefunction();
  59. return 0;
  60. }

Which gives for examples following outputs depending on what u enter:

  1. //---------1st case :
  2. please enter a value for a:
  3. 0
  4. please enter a value for b:
  5. -1
  6. Initial balance is: 0
  7. id is: 0
  8. credit Limit is: 0
  9. //---------2nd case :
  10. please enter a value for a:
  11. 1
  12. please enter a value for b:
  13. 0
  14. Initial balance is: 100
  15. id is: 1
  16. credit Limit is: 500
  17. //---------3rd case :
  18. please enter a value for a:
  19. 2
  20. please enter a value for b:
  21. 320
  22. Initial balance is: 2
  23. id is: 320
  24. credit Limit is: 100
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: [need help] Initializing Objects in Control Structures

 
0
  #5
Oct 10th, 2008
It's a good idea but not so good implementation. No need to define assignment operator: it's exactly default assignment. Keep it simpler:
  1. Account customer;
  2. ...
  3. if (a > 0 && b > 0)
  4. customer = Account(a,b);
  5. customer.somefunction();
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: [need help] Initializing Objects in Control Structures

 
0
  #6
Oct 10th, 2008
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.
So I tried to give him such a solution.

Originally Posted by ArkM View Post
It's a good idea but not so good implementation. No need to define assignment operator: it's exactly default assignment. Keep it simpler:
  1. Account customer;
  2. ...
  3. if (a > 0 && b > 0)
  4. customer = Account(a,b);
  5. customer.somefunction();
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 63
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Re: [need help] Initializing Objects in Control Structures

 
0
  #7
Oct 11th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC