Sign in to follow this  
marcmassacre

Did the constructor run yet???

Recommended Posts

 //class definition
class account
{	
	friend Date;
        private:
	int month;
	
	public:
	int print_dob();
	account(); 
		
};	//end class 

account::account()
{
	month=6;
	Date date1(6, month, 1972);

}

void account::print_dob()
{
date1.print();
}
The following code won't work because date1 is an undeclared identifier to member function print_dob(). Is there anyway to have print_dob() only run if if the constructor has been called first? You know what I mean?

Share this post


Link to post
Share on other sites
Do you mean for 'date1' to be local to the constructor? Also, why do the two print_dob()'s have different return types?

Share this post


Link to post
Share on other sites
well, if you wanted it to run right after the constructor has been called, you could call the method at the last line, after all of the member variables have been initialized:

account::account()
{
month=6;
Date date1(6, month, 1972);
print_dob();
}


Is this what you had in mind?

Share this post


Link to post
Share on other sites
I think I see what you are trying to say. Try adding date1 as a private member variable and then instantiate date1 within your constructor.

Share this post


Link to post
Share on other sites
I have to have the date1 instance in the constructor...So is there anyway to tell the compiler that print_dob can't be called unless the constructor has already been called??? thanks for youre help!!! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by marcmassacre
I have to have the date1 instance in the constructor...
Why? (Homework?)

Share this post


Link to post
Share on other sites

//class definition
class account
{
friend Date;
Date* date1;
private:
int month;

public:
int print_dob();
account();

}; //end class

account::account()
{
month=6;
Date temp = new Date(6, month, 1972);
date1 = &temp;
}

void account::print_dob()
{
date1->print();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this