5 Replies - 1678 Views - Last Post: 15 August 2012 - 11:45 AM Rate Topic: -----

#1 maincha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-August 12

c++ class, fuctions and pointers new and delete operators

Posted 13 August 2012 - 07:29 PM

hello ppl, i am new to c++ and the class, functions and pointers i have not got it very well so please help me out. the question is
create a class called DynamicStringArray which includes member
functions that allow it to emulate the behaviour of a vector of strings. The class must have:
• A private member variable called dynamicArray which references a dynamic array of type
string
• A private member variable called size which holds the number of entries in the array
• A default constructor which sets the size 0 and the dynamicArray to NULL
• A function to return size
• A function named addEntry that takes a string as input. The function should create a new
dynamic array one element larger than dynamicArray, copy all the elements from
dynamicArray into the new array, add the new string onto the end of the new array,
increment size, delete the old dynamicArray and then set dynamicArray to the new array
• A function named deleteEntry which takes a string as input. The function must search
dynamicArray for the string. If not found, it returns false. If found, it creates a new dynamic
array, one element smaller than dynamicArray. If must copy all elements except the input
string into the new array, delete dynamicArray, decrement size and return true. For
simplicity, if there are two strings with the same value, delete only the first value
• A function named getEntry which takes an integer as input and returns the string at that
index in dynamicArray. It must return NULL if the index is out of the range of
dynamicArray
• A destructor which frees the memory allocated to the dynamic array
i do not get this uestion very well. i am trying to write the code but unable to finish my code so far is

Dynamicstringarray.hpp
#ifndef _ DYNAMICARRAY_H_
#define _ DYNAMICARRAY_H_

#include <string>

#include <iostream>

using namespace std;

Class dnamicArray
{
private:
                string *dynamicArray;
                int size;       //size that holds the number of entries in the array.

public:
dynamicArray();
~dynaicArray();
 string  addEntry(int entry);	 // 
 string deleteEntry();
 string getEntry();

int nsize(); //Function to return the size

dynamicArray & operator()








Dynamicstringarray.cpp

#include <iostream>
using namespace std;
dynamicArray::dynamicArray (): size(0), dynamicArray(NULL);
{
}
 dynamicArray::~dynamicArray();
{
Size=0;
}
void dynamicArray::addEntry();
{
dynamicArray=addEntry(dynamicArray, dynamicArraySize);
for (int i=0; i<dynamicArraySize; i++)
cout << “the entry is”<<(i+1)<<” “<<endl;
cin>>dynamicArray[i];
string *addEntry(string *dynamicArray, int &size, string newEntry)
{
   // create a new dynamic array 1 element larger than dynamicArray
   string *newArray = new string[size + 1];

   // copy all elements from dynamicArray into new array
   for(int i = 0; i < size; ++i)
   {
       newArray[i] = dynamicArray[i];
   }

   // add the new entry onto the end of the new array
   newArray[size] = newEntry;
 ++size;
}
Void dynamicArray::getentry
{
String* getEntry(string *dynamicArray, int &size, string)
{
}
Void dynamicarray::deleteentry();
{
string* deleteEntry(string *dynamicArray, int &size, string entryToDelete)
{
   string *newArray = new string[size - 1];   // creates a new dynamic array, one element smaller than dynamicArray.

delete [] dynamicArray; 

--size;  //Decrement size

return newArray;
}


int dynamicArray::nsize();
{
return size;
}











dynmain.cpp
#include “dynamicarray.hpp”
#include <iostream>
using namespace std;
int main()
{
//i really do not have any idea what goes into the main function.
return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: c++ class, fuctions and pointers new and delete operators

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: c++ class, fuctions and pointers new and delete operators

Posted 13 August 2012 - 07:49 PM

Does your code even compile without errors? You aren't even including the correct header files if we are to believe the filenames you listed in the code you posted.

My recommendation is start from scratch with just a single file. Move the class declarations to headers and the implementation to another .cpp file later. Focus on getting working code first.
Was This Post Helpful? 0
  • +
  • -

#3 maincha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-August 12

Re: c++ class, fuctions and pointers new and delete operators

Posted 13 August 2012 - 07:59 PM

View PostSkydiver, on 13 August 2012 - 07:49 PM, said:

Does your code even compile without errors? You aren't even including the correct header files if we are to believe the filenames you listed in the code you posted.

My recommendation is start from scratch with just a single file. Move the class declarations to headers and the implementation to another .cpp file later. Focus on getting working code first.

i do not exactly have idea of how to move on with this code. some suggestions or general idea would be a great help
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: c++ class, fuctions and pointers new and delete operators

Posted 13 August 2012 - 09:03 PM

This should give you starting point where the code compiles, and there is something to exercise your code to see if some parts of it work as expected. It's a cleaned up version of your code above, but with the functionality stripped back out to give you a clean slate.

#include <iostream>
#include <string>

//
//  class DynamicStringArray
//
//  Supposed to act like a std::vector<string>.
//
class DynamicStringArray
{
public:
    DynamicStringArray();
    ~DynamicStringArray();
    void addEntry(std::string entry);
    bool deleteEntry(std::string entry);
    std::string getEntry(int index);

private:
    // your private variables here
};



//
//  DynamicStringArray implementation
//

DynamicStringArray::DynamicStringArray()
{
    // your code here
}

DynamicStringArray::~DynamicStringArray()
{
    // your code here
}

void DynamicStringArray::addEntry(std::string entry)
{
    // your code here
}

bool DynamicStringArray::deleteEntry(std::string entry)
{
    // your code here. Replace the line below as necessary.
    return false;
}

std::string DynamicStringArray::getEntry(int index)
{
    // your code here. Replace the line below as necessary.
    return std::string("");
}



//
//  main()
//
//  Drives DynamicStringArray to test some methods.
//

int main()
{
    using namespace std;

    DynamicStringArray dsa;

    dsa.addEntry("The sky is blue.");
    dsa.addEntry("Diamonds are a girl's best friend.");
    dsa.addEntry("Vampires do it in the dark.");

    if (dsa.getEntry(2) == "Vampires do it in the dark.")
        cout << "Correct string returned by getEntry()." << endl;
    else
        cout << "Correct string returned by getEntry()." << endl;

    if (dsa.deleteEntry("The grass is green."))
        cout << "Incorrectly deleted an non-existent string" << endl;
    else
        cout << "Correctly ignored an non-existent string" << endl;

    if (dsa.deleteEntry("The sky is blue."))
        cout << "Correctly deleted an existing string" << endl;
    else
        cout << "Incorrectly deleted an non-existent string" << endl;

    // The following code is iffy.
    // Assignment says getEntry() should return NULL if not found.
    // As far as I know you cannot create a "null" std::string.
    // This is unless the assignment meant so say that the it was
    // supposed to be like std::vector<char *> as opposed to
    // std::vector<string>. But the first bullet point was explicit
    // about the data type.
    if (dsa.getEntry(1004) == "")
        cout << "Correct empty string returned by getEntry() for out of bounds." << endl;
    else
        cout << "Incorrect string returned by getEntry() for out of bounds." << endl;

    return 0;
}



I'll let the other C/C++ folks here discuss lines 88-98 and whether I misread the assignment specifics or if your teacher just had an oversight.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: c++ class, fuctions and pointers new and delete operators

Posted 13 August 2012 - 09:23 PM

As a side note, the parameters to addEntry() and deleteEntry() should be type const std::string &. Similarly a reference should be returned for getEntry(). I just didn't want to distract you too much from the prime focus of your assignment.
Was This Post Helpful? 0
  • +
  • -

#6 TrialLurk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-August 12

Re: c++ class, fuctions and pointers new and delete operators

Posted 15 August 2012 - 11:45 AM

can we see your completed code so far? ur first post had some little errors. just want to make sure
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1