954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[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: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.

mbayabo
Newbie Poster
2 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

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...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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
sidatra79
Junior Poster
114 posts since Feb 2008
Reputation Points: 46
Solved Threads: 8
 

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();
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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.

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
Junior Poster
114 posts since Feb 2008
Reputation Points: 46
Solved Threads: 8
 

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.

chunalt787
Junior Poster in Training
84 posts since Apr 2008
Reputation Points: 39
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You