Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classes, Arrays, and Pointers, oh my 1

Status
Not open for further replies.

Chizotz

Programmer
Oct 19, 2003
2
US
Hello,

I am new to programming C++ on Unix, but not new to programming. I program professionally in VB6 and .NET as well as C# (just moving to that). I am enrolled in a C++ class and need some help with an assignment. Now, I do not want anyone to do this assignment for me, I simply want some hopefully friendly directions and advice. My teacher is unreachable and the tutor didn't show up for his shift, and I'm in a bind. If this violates the rules here, apologies in advance.

The assignment is to create an appointment book application that has three classes (ApptBook, Date, Time).
The ApptBook object is to instantiate an array of Date objects, the Date objects in turn are to instantiate arrays of Time objects. All is to be done on the heap using pointers.

I'm lost. I'm having a conceptual problem, I think, as well as syntactical issues.

I've created the three classes that work on the stack when I instantiate a single instance. I can't seem to get it to work either when trying to create arrays of objects nor when trying to move to pointers on the stack. This could be mainly syntax problems, but I also am having trouble visualizing when to instantiate the objects and how to manipulate them once instantiated.

This is using the g++ compiler, and if it matters I'm working in vi via telnet to the school Unix system.

Any (ahem) pointers or advice appreciated.

Thanks,

Ron
 
see you must have done something like this:

class Time
{
// some data and functions
};

class Date
{

private:
Time myTime;
// some more data and functions

};

class ApptBook
{
private:
Date myDate;

// Some data and functions
};


Your assignment says that you shall not allocate the objects on the stack, and rather do it on the heap. So instead of taking members on stack, rather take their pointers on stack.

class Time
{
// some data and functions
};

class Date
{

private:
Time *myTime;
// some more data and functions

char * getTime();

};

class ApptBook
{
private:
Date *myDate;

// Some data and functions
};


Now say you know that you can have argument passed in some function which says how many members to be allocated.


Say my ApptBook constructor is

ApptBook::ApptBook(int numElement)
{
myDate = new Date[numElement]; // This allocates memory for the Date objects

}

now you can access them as

myDate[0].getTime();
myDate[1].getTime(); ... myDate[numElement - 1].getTime();

Something similar can be done for the Date class objects.
 
Thank you, that is almost exactly what I ended up doing.

My biggest problem was visualizing where the instantiation of each object was to take place. Then I realized that basically it is that the ApptBook is a container for the Dates, and the Dates are containers for the Time objects, so the Times are instantiated in the Date, the Dates are instantiated in the ApptBook. Makes sense, once you make the conceptual leap.

I ended up with code like this:

class ApptBook
{
public:
Date* ptrDate[10];
ApptBook();
~ApptBook();
};

ApptBook::ApptBook()
{
for(int j = 0; j < 10; j++)
{
ptrDate[j] = new Date();
}
}

and then can manipulate with code like

curBook->ptrDate[0]->setDate(10,10,2003);
cout << &quot;Day: &quot; << curBook->ptrDate[0]->day << endl;

I still have a fair amount of work to make this into a presentable program, but I think the hard part is handled.

Thank you for your help, its much appreciated.

Ron

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top