I am writing a program to handle a DVD collection. There are 2 classes. The first class is a string class that has member functions that handle strings(length, compare, etc.) and store strings(dynamic array). The second class handles the actual DVD's-title, year, actors, and brief description. I'm confused on working between the classes. I don't know exactly how to access the string class so I can store the DVD information.

Recommended Answers

All 7 Replies

you have a dvd class that contains string objects. Why are you writing your own string class when there is a perfectly good std::string class?

I am not allowed to use std::string. Through error on my part they are actually c-strings. I guess I don't know how to access the string objects to store my dvd objects.

you have to use the function in string.h, such as strcpy() to copy a string, or strcmp() to compare two strings.

post the two classes so that we can be more specific.

string.h:

#ifndef STRING_H
#define STRING_H

class string
{
	private:
		char _string[100];
		//recursive functions
		int str_length(const char str[]);
		int str_compare(const char *str1, const char *str2);
		void str_reverse(char str[] , int length);
		bool str_replace_first(char str[], const char find[], const char replace[]);
		
		//iterative functions
		int length_iter(const char str[]);
		int str_compare_iter(char str1[], const char str2[]);
		void str_reverse_iter(char str[], int length);
		bool str_replace_first_iter(char str[], const char find[], const char replace[]);
	
	public:
		//Constructors
		string();
		string(char *array, int size);
		//Deconstructor
		~string();
		
		int str_length(){
			return str_length(_string);}
		int str_compare(){
			return str_compare(_string, _string);}
		void str_reverse(){
			return str_reverse(_string, str_length(_string));}
		bool str_replace_first(){
			return str_replace_first(_string, _string, _string);}
			
		int length_iter(){
			return str_length(_string);}
		int str_compare_iter(){
			return str_compare(_string, _string);}
		void str_reverse_iter(){
			return str_reverse(_string, str_length(_string));}
		bool str_replace_first_iter(){
			return str_replace_first(_string, _string, _string);}
		//Overloaded Operators
		void operator+(string &rstring);
		friend std::istream &operator >>(std::istream &is, string &string);
		friend std::ostream &operator <<(std::ostream &os, string &string);
};
#endif

string.cpp:

#include "string.h"
#include <iostream>
using std::istream;
using std::ostream;

//Constructors
string::string()
{
	_string = new char[100];
	_string[0] = '\0';
}

string::string(char *array, int size)
{
	_string = new char[size];
	for(int i=0; i < size; i++)
	{
        _string = *array;
	}
}
//Deconstructor
string::~string()
{
	delete [] _string;
}
/**
 * This function finds the length of a c-string using recursion
 * @param str[ ] a c-string variable that stores a string from user
 * @pre  in main command = "length", str[0] is not an empty string-'\0'
 * @post if str[0]='\0' then 0 is returned or str_length is called again but at the next index of str[]
 * @return the length of the c-string str[]
 */
int string::str_length(const char str[])
{
	//base case
	if (str[0] == '\0')
		return 0;
	//recursive statement
	else
		return 1 + str_length(str+1);
}

/**
 * This function finds the length of a c-string using iteration
 * @param str[ ] a c-string variable that stores a string from user
 * @pre in main command = "length_iter", str[ ] is not an empty string-'\0'
 * @post i is < 0
 * @return the length of the c-string str[]
 */
int string::length_iter(const char str[])
{
	int i=0;
	while(str[i] != '\0')
	{
		i++;
	}
	return i;
}

/**
 * This recursive function compares 2 c-strings to see if they are equal or there is a character mismatch
 * @param *str1 is a pointer to charArray[ ]
 * @param *str2 is a pointer to scharArray2[ ]
 * @pre in main command = "compare", str1[ ] and str2[ ] is not an empty string-'\0'
 * @post str1[] and str2[ ] are equal or *str1 and *str2 point to different characters
 * @return a integer vaule that is the distance between the ASCII character vaules.  If *str1 points to
a smaller character, the vaule is negative.  If *str2 points to the a smaller character the value is positive.
If both *str1 and *str2 do not point to a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned 
 */	
int string::str_compare(const char *str1, const char *str2)
{
    if (*str1 != '\0' || *str2 != '\0')
    {
        if (*str1 != *str2)
        {
            if (*str1 > *str2)
            {
                return *str1 - *str2;
            }
            else if (*str1 < *str2)
            {
                return *str1 - *str2;
            }
        }
        
    }
    else return 0; //C-strings are equal-base case
    //recursive statement
    return str_compare(str1 + 1, str2 + 1);
}	

/**
 * This iterative function compares 2 c-strings to see if they are equal or there is a character mismatch
 * @param str1[ ] is a c-string variable that stores a string from user
 * @param str2[ ] is a c-string variable that stores a string from user
 * @pre in main command = "compare_iter", str1[ ] and str2[ ]  are not an empty string-'\0'
 * @post str1[] and str2[ ] are equal or str1[i]  and str2[i]  contain different characters
 * @return a integer vaule(diff) that is the distance between the ASCII character vaules.  If str1[i] contains
a smaller character, the vaule(diff) is negative.  If str2[i] contains the a smaller character the value(diff) is positive.
If both str1[i]  and str2[i]  do not contain a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned (diff)
*/
int string::str_compare_iter(char str1[], const char str2[])
{
	for( ; *str1=*str2; *str1++, str2++)
	{
	     if(*str1 == 0)
            return 0;
	}
   
	return int(*str1-*str2); 
}

/**
 * This recursive function will reverse the c-string str
 * @param *str is a pointer to charArray[ ]
 * @param length is the length of charArray[ ]
 * @pre in main command = "reverse", *str does notpoint to an empty string-'\0'
 * @post the string in charArray[ ]  is reversed
 * @return no return value
 */
void string::str_reverse(char *str, int length)
{
	
	char temp, temp1;
    	
    if (length >= 1)
    {
         // swap first & last chars    
         temp = str[0];
         str[0] = str[length-1];
         str[length-1] = temp; 
         // recursive call--
         str_reverse(&str[1], length-2);
    }
}    

/**
 * * This recursive function will reverse the c-string str
 * @param str[ ] str1[ ] is a c-string variable that stores a string from charArray[ ]
 * @param length is the length of charArray[ ]
 * @pre in main command = "reverse_iter", str[ ]  does notpoint to an empty string-'\0'
 * @post the string in charArray[ ]  is reversed
 * @return no return value
 */	
void string::str_reverse_iter(char str[], int length)
{
	char temp;
	int starti, endi, mid;//starting and ending index
	starti = 0;
	endi = length - 1;
	mid = length / 2;
	
	while(starti < mid)
	{
		temp = str[starti];
		str[starti] = str[endi-starti];
		str[endi-starti] = temp;
		starti++;
	}
}

/**
 * This recursive function will search a c-string for a character(s) to be replaced with other character(s)
 * @param *str points to charArray[ ] the array to be searched
 * @param *find points to charArray2[ ], contains what to search for
 * @param*replace points to charArray3[ ] what to replace the characters with
 * @param is_partial_match  is true if part of the find string is found in the original string
 * @pre  in main command = "replace_first", *str[ ] , *find, *replace does nont point to an empty string-'\0'
 * @post Either the string was found and replaced in the array or *find and *replace were not equal in size thus the array was not searched
 * @return false condition if *find and *replace were not equal in size thus the array was not searched or
 true condition if *find and *replace were equal in size thus the array was searched and replaced with the contents *replace points to
 */
bool string::str_replace_first(char *str, const char *find, const char *replace)
{
	if(length_iter(find) != length_iter(replace))
	{
		cout << "ERROR: The find and replace strings are of different length." << endl;
		cout << "Please change the find and replace strings to the same length." << endl;
		return false;
	}
	
	while(*str != '\0')
    {        
            
    if(*str == *find)
		{
			*str = *replace;
			*replace++;
			*find++;
        }

	else	str_replace_first(str+1, find, replace);
}
    return true;
}

/**
 * This iteraive function will search a c-string for a character(s) to be replaced with other character(s)
 * @param str points to charArray[ ] the array to be searched
 * @param find points to charArray2[ ], contains what to search for
 * @param replace points to charArray3[ ] what to replace the characters with
  * @param is_partial_match  is true if part of the find string is found in the original string
 * @pre  in main command = "replace_first_iter", str , find, replace does nont point to an empty string-'\0'
 * @post Either the string was found and replaced in the array or find and replace were not equal in size thus the array was not searched
 * @return false condition if find and replace were not equal in size thus the array was not searched or
 true condition if find and replace were equal in size thus the array was searched and replaced with the contents replace points to
 */
bool string::str_replace_first_iter(char str[], const char find[], const char replace[])
{
	if(length_iter(find) != length_iter(replace))
	{
		cout << "ERROR: The find and replace strings are of different length." << endl;
		cout << "Please change the find and replace strings to the same length." << endl;
		return false;
	}
	int i, j, k, m, n, delta;
    int n1, n2, n3;
    char* p, *q;
    if (!str || !find)
        return 0;
    if (!replace)
        replace = "";
    n1 = str_length(str);
    n2 = str_length(find);
    n = n1 - n2 + 1;
    for (i = 0; i < n; ++i)
    {
        for (j = 0; j < n2 && find[j] == str[i+j]; ++j)
            ;
        if (j == n2) // found
        {
            n3 = str_length(replace);
            delta = n3 - n2;
            m = n1 - i - n2;
            if (delta < 0) /* move left */
            {
                p = str + (i + n2 + delta);
                q = p - delta;
                for (k = 0; k <= m; ++k)
                    p[k] = q[k];
            }
            else if (delta > 0) /* move right */
            {
                q = str + n1 - m;
                p = q + delta;
                for (k = m; k >= 0; --k)
                    p[k] = q[k];
            }
            for (k = 0; k < n3; ++k)
                str[i+k] = replace[k];
            return str + i + n3;
        }
    }
    return 0;
}

int string::str_length(){
	return str_length(_string);}

int string::str_compare(){
	return str_compare(_string, _string);}

void string::str_reverse(){
	return str_reverse(_string, str_length(_string));}

bool string::str_replace_first(){
	return str_replace_first(_string, _string, _string);}

int string::length_iter(){
	return str_length(_string);}

int string::str_compare_iter(){
	return str_compare(_string, _string);}

void string::str_reverse_iter(){
	return str_reverse(_string, str_length(_string));}

bool string::str_replace_first_iter(){
	return str_replace_first(_string, _string, _string);}
	
void string::operator+(string &rstring)
{
	//string temp;
	int len = str_length(_string) + str_length(rstring);
    string temp(_string, len);
    int i;
    for (i = 0; i < str_length(_string); i++)
         temp[i] = _string[i];
    for (int j = 0; j < str_length(rstring); j++, i++)
        temp[i] = rstring[j];
    rstring[len]='\0';
}

istream &operator >>(std::istream &is, string &string)
{
	for(int i = 0; i < str_length(string); i++)
		is >> s.string[i];
}

ostream &operator <<(std::ostream &os, string &string)
{
	for(int i = 0; i < str_length(string); i++)
		os << string[i];
	return os;
}

I dont really have anything substantial for DVD.cpp and .h but
DVD.h:

#ifndef DVD_H
#define DVD_H

class DVD
{
	class DVD : public string; 
	private:
		string actor, title, descrip;
		int year;
		
	public:
		void operator+(DVD &rstring);
		friend std::istream& operator >>(std::istream &is, DVD &DVD);
		friend std::ostream& operator <<(std::ostream &os, DVD &DVD);	
};
#endif

DVD.cpp:

#include "DVD.h"

friend std::ostream& operator >>(std::ostream &os, DVD &DVD)
{
	is.getline(artist, 100);
	is.getline(album, 100);
	is >> year;
	while(is != 'END')
	{
		is.getline(descrip, 100);
	}
	return is;
}

string.h is the name of a standard C header file. You should name your header file something else so that you can get the functions that are in the standard string.h header.

Its not necessary to pass all those parameters to those string functions, because the string class already knows about the functions. And you don't need private functions that can be easily done in the public functions, such as str_length()

int str_length()
{
    return strlen(_string);

Thats all you need in the public function.

After I do rename it, say to "AString.h", then I can access str_compare as AString.str_compare?

No -- the name of the file has little, if any, relationship to the class name(s) that are in it. The name of the header files doesn't have to be the same as the class thats in it.

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.