Hey guys, I was just wondering when using classes to obtain a name or something like that when a pointer is used how come you don't have to use the new and delete key words.

class LibBook

{
    private:
       char *name
    public:
       LibBook( char* = '\0')
       void showBookName()

};

Thanks in advance.

Hey guys, I was just wondering when using classes to obtain a name or something like that when a pointer is used how come you don't have to use the new and delete key words.

Generally you do. Do you have a more specific example? The one you posted is not really correct. The '\0' should be 0.

I found another example:

class Date
{
 private:
	 char day[10];
	 char month[10];
	 int year;
  
public:
	

	void set(char* day, char* month, int year);
  
        void display(Date datem);
  
        char* get_day();

	char* get_month();

	int get_year();

 };

void Date::set(char* d, char* m, int y)
{

	strncpy(day, d, 9);
	strncpy(month, m, 9);
	year=y;

}

  
void Date::display(Date date)
{
	cout << "The day is ";
	cout << date.get_day();
	
	cout << "\nThe month is ";
	cout << date.get_month();
	
	cout << "\nThe year is ";
	cout << date.get_year();
	cout << endl;
}
  
char* Date::get_day()
{
	return day;
}

char* Date::get_month()
{
	return month;
}

int Date::get_year()
{
	return year;
}

What is your question in regard to pointers about your last posted code? The functions that return a pointer to the beginning of a char array that is private? Or the function parameters that receive a pointer to the beginning of a string to copy?

I was just wondering with the variables such as day and month as they are pointers how come the new and delete keywords don't have to be used.

I was just wondering with the variables such as day and month as they are pointers how come the new and delete keywords don't have to be used.

The day and month variables are declared to be arrays of length 10. You only need to use new and delete[] if the arrays have not been allocated yet.

The one you posted is not really correct. The '\0' should be 0.

Hi Dave, I was under the impression that '\0' was equal to 0?
I got the output "equal" for the following program.

#include <cstdio>

int main()
{
  if ( '\0' == 0 )
    printf( "equal" );
  getchar();
}

Or is this only restricted to my compiler (mingw)?

The day and month variables are declared to be arrays of length 10. You only need to use new and delete[] if the arrays have not been allocated yet.

Thanks for your help Dave & Dante.

Hi Dave, I was under the impression that '\0' was equal to 0?

It was more of a hint not to get into the habit of writing characters to pointers and confusing NULL and '\0'.

[edit]So yes, I overstated things when I said it wasn't quite correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.