Did the constructor run yet???

Started by
8 comments, last by marcmassacre 18 years, 1 month ago
 //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?
Advertisement
Do you mean for 'date1' to be local to the constructor? Also, why do the two print_dob()'s have different return types?
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?
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.
There's no date1 object inside your class. You need to have one there in order for it to be accessed by the whole class, not just that function.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
i would also question why Date is a friend.
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!!! :)
Quote:Original post by marcmassacre
I have to have the date1 instance in the constructor...
Why? (Homework?)
 //class definitionclass 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();}
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
thanks!!! I rated you well.

This topic is closed to new replies.

Advertisement