943,667 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 686
  • C++ RSS
Oct 10th, 2008
0

[need help] Initializing Objects in Control Structures

Expand Post »
I'm having trouble initializing Objects while they're inside if else statements. I'm using Visual C++ 2008.

for instance:

C++ Syntax (Toggle Plain Text)
  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:

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mbayabo is offline Offline
2 posts
since Oct 2008
Oct 10th, 2008
0

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.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 10th, 2008
0

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:
C++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 10th, 2008
0

Re: [need help] Initializing Objects in Control Structures

Hi maybe the following suits your problem:

c++ Syntax (Toggle Plain Text)
  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:

c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008
Oct 10th, 2008
0

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:
C++ Syntax (Toggle Plain Text)
  1. Account customer;
  2. ...
  3. if (a > 0 && b > 0)
  4. customer = Account(a,b);
  5. customer.somefunction();
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 10th, 2008
0

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.
So I tried to give him such a solution.

Click to Expand / Collapse  Quote originally posted by ArkM ...
It's a good idea but not so good implementation. No need to define assignment operator: it's exactly default assignment. Keep it simpler:
C++ Syntax (Toggle Plain Text)
  1. Account customer;
  2. ...
  3. if (a > 0 && b > 0)
  4. customer = Account(a,b);
  5. customer.somefunction();
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008
Oct 11th, 2008
0

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.
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: closing a function
Next Thread in C++ Forum Timeline: Sprite/Bitmap Graphics





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC