C++ Assignment Question (Member Function)

Do you understand what this person is asking about?

Page 1 of 1

5 Replies - 4234 Views - Last Post: 02 May 2008 - 05:02 PM Rate Topic: -----

#1 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

C++ Assignment Question (Member Function)

Posted 01 May 2008 - 12:25 PM

I have an assignment in which I need to make a program that prints out date to the user, but I have to use a class with it. The only part of this assignment I do not understand is when I am asked to create a member function called initialize

I understand what initialize means but in this program I don't understand the purpose of it. Can anyone make any sense of what it dose based on the assignment I was asked below. I have all the rest of the program built accept this member function.

1. Create a class - ASCIICode - that allocates an array of characters of a given size using the new operator and initializes each element in the character array with symbol that has the ASCII code as its subscript. For example, the 98th element (with 97 as its subscript) of the array should contain 'a'. The class should have at least the following data members and member functions:

* a data member -size - that represents the size of the array.
* a static data member - count - that is used as a 'global shared variable' to count how many ASCIICode objects are created in a program.
* a constructor that accepts an integer for the size of the character array.
* a constructor that accepts no argument. (In this case, default the character array with size 255.)
a member function - initialize - that performs the initialization
* an overloaded constant member function - print - that prints an element or all elements in the character array.
* a member function - showSize - that displays the size of the array.
* a member function - showCount - that displays the count.
* a destructor that destroys the dynamically allocated character array.

2. Create a proxy class - ASCIICodeCover - to hide private data members of ASCIICode.

3. Create a driver program that tests above classes.

This post has been edited by lockdown: 01 May 2008 - 12:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Assignment Question (Member Function)

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: C++ Assignment Question (Member Function)

Posted 01 May 2008 - 12:45 PM

You constructors will set the size of the array using the new operator, but initialize is the one that is going to load the character data into the array. It is the one that will have the loop that loops through the character set and load each char of the array.

In case you haven't figured it out, the constructor will do the "new" and then call initialize to load it up.

:)

This post has been edited by Martyr2: 01 May 2008 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Re: C++ Assignment Question (Member Function)

Posted 01 May 2008 - 01:00 PM

That make sense, I actually did that in my constructor. I made my modifications so should it look something like this? It worked correctly but just wanted to make sure it looks correct ha.

// LINKER FILE

#include <iostream>
using namespace std;
#include "ASC2.h"

int ASC2Code::count = 0;

ASC2Code::ASC2Code(int arraySize)
{
	size = (arraySize > 0 ? arraySize : 255);
	ptrA = new char[size];

	initalize();
}

void ASC2Code::initalize()
{
	for( int i = 0; i < size; i++)
	{
		ptrA [ i ] = i;
		count++;
	}

}

void ASC2Code::showSize()
{
	cout << "Size: " << size <<endl;
}

void ASC2Code::showCount()
{
	cout << "Count: " << count <<endl;
}

void ASC2Code::print()
{
	for( int i = 0; i < size; i++)
	cout << ptrA [ i ] <<endl;
}

ASC2Code::~ASC2Code()
{
	delete [ ] ptrA;
}



Also thanks a ton for clearing that up. It has been driving me crazy for the last 3 days.

This post has been edited by lockdown: 01 May 2008 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: C++ Assignment Question (Member Function)

Posted 01 May 2008 - 03:06 PM

That certainly looks fine to me. :)
Was This Post Helpful? 0
  • +
  • -

#5 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Re: C++ Assignment Question (Member Function)

Posted 01 May 2008 - 03:12 PM

Ok cool thanks again. Got it all working.
Was This Post Helpful? 0
  • +
  • -

#6 fwesselhoft  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-May 08

Re: C++ Assignment Question (Member Function)

Posted 02 May 2008 - 05:02 PM

Hi, I have a big problem. I have my last assignment due tomorrow at midnight, I was sure that was due next week. I was looking in the internet for a problem like the one I has and I found that you asked for help about this excercise before. I would like to see if you would help me with at least question number 1. Please...

The exercise you wrote is this:

I have an assignment in which I need to make a program that prints out date to the user, but I have to use a class with it. The only part of this assignment I do not understand is when I am asked to create a member function called initialize

I understand what initialize means but in this program I don't understand the purpose of it. Can anyone make any sense of what it dose based on the assignment I was asked below. I have all the rest of the program built accept this member function.

1. Create a class - ASCIICode - that allocates an array of characters of a given size using the new operator and initializes each element in the character array with symbol that has the ASCII code as its subscript. For example, the 98th element (with 97 as its subscript) of the array should contain 'a'. The class should have at least the following data members and member functions:

* a data member -size - that represents the size of the array.
* a static data member - count - that is used as a 'global shared variable' to count how many ASCIICode objects are created in a program.
* a constructor that accepts an integer for the size of the character array.
* a constructor that accepts no argument. (In this case, default the character array with size 255.)
a member function - initialize - that performs the initialization
* an overloaded constant member function - print - that prints an element or all elements in the character array.
* a member function - showSize - that displays the size of the array.
* a member function - showCount - that displays the count.
* a destructor that destroys the dynamically allocated character array.

2. Create a proxy class - ASCIICodeCover - to hide private data members of ASCIICode.

3. Create a driver program that tests above classes.


I hope you can help me, I am so worried... :blink:

My e-mail is fwesselhoft@comcast.net

Thanks a lot

Fritz
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1