Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-03-2006, 09:00 PM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Rep Power: 10
hoser2001 is on a distinguished road
Default A custom String Class

My entire programming experience has been based in C#, so my skills dealing with creating classes is somewhat limited, especially in C++.

I have to create a really simple string class that just does a few operations
(head and tail functions, concatenation operator, assignment operator and the input and output operators >> <<)

I also need a default constructor, and two other constructors for handling single characters and copying strings.

I want to be able to include it at the top of a program.

If anyone has any suggestions as to the best way to approach creating a class with just these simple requirements I would greatly appreciate any help.

Thanks in advance!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-04-2006, 06:59 AM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Rep Power: 10
hoser2001 is on a distinguished road
Default

Alright, I got my class put together, but I have an insane amount of compiler errors, and I don't get why most of them are there. I mostly followed examples online but for the most part I think it's right. I'm sure some of the errors are easily fixed, however I know I am missing a few major concepts here!
Code:
#include <iostream>

using namespace std;

class Cstring
{
private:
 //array that will hold the strings
 char array[50];
public: 
 //default constructor
 Cstring();   
 //constructor that turns a single character into a string  
 Cstring(char&);
 //constructor that creates a copy of a string
 Cstring(Cstring&);
//////////////////////////////////////
 //overloaded operators for strings
//////////////////////////////////////
//this operator adds a single characters to the end of a string
 friend Cstring operator + (Cstring&);
//this operator copies a string into a result string
 friend Cstring operator = (Cstring&);
//these are of course the input and output operators
 friend Cstring operator >> (Cstring&);
 friend Cstring operator << (Cstring&); 
 //function that returns the first character of the string   
 char head();
 //function that returns the string with the first character removed
 Cstring tail();
};

char Cstring::head()
{
 return array[0];
}

Cstring Cstring::tail()
{
 Cstring temp;
 for(int i=1; i<50; i++;)
 {
  temp.array[i] = array[i];     
 }
 return temp;  
}

Cstring::Cstring()
{
 for(int i=0; i<50; i++;)
 array[i]=' ';
}

Cstring::Cstring(char &this)
{
 array[0] = this;
 for (int i=1; i<50; i++;)
 array[i] = ' ';
}

Cstring::Cstring(Cstring &other)
{
 for (int i=0; i<50; i++;)
 array[i] = other.array[i];
}

Cstring Cstring::operator+(Cstring &left, char &right)
{
 Cstring temp;
 for(int i=0; i<50; i++)
   temp.array[i] = left.array[i];
         
 for(i=0; i<50; i++)
 {
  if(temp.array[i]==' ')
  {
   temp.array[i] = right;
   break
  }
 }
 return temp;           
}

Cstring::operator=(Cstring &left, Cstring &right)
{
 Cstring temp;
 temp.array[i] = right.array[i];
}

ostream&::operator<<(ostream& output, Cstring &right)
{
 for(int i=0; i<50; i++)
 {
  if(right.array[i] != ' ')
  output << right.array[i];
 }
}

istream&::operator>>(istream& input, Cstring &right)
{
 Cstring temp;
 input.getline(temp.array,50);
 return temp;
}

Last edited by hoser2001; 10-04-2006 at 11:02 AM. Reason: thought I should clarify the use of the operators more
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-04-2006, 09:04 AM
Void's Avatar   
Void Void is offline
Programming Expert
 
Join Date: Jun 2006
Posts: 411
Rep Power: 12
Void is on a distinguished road
Default

Hey, what compiler are you using?
__________________
Void
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-04-2006, 11:00 AM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Rep Power: 10
hoser2001 is on a distinguished road
Default

bloodshed dev c++ 4.9.9.1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-04-2006, 01:22 PM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Rep Power: 10
hoser2001 is on a distinguished road
Default

Ok, I started over and decided to compile little bit at a time instead of cranking stuff out and trying to debug the whole thing... This is how far Ive gotten so far. I commented out the overloading of the = sign because I have no idea whats wrong with that, and I left in the main function because I dont know how to compile it without it, however the rest of it works until I try adding the method for overloading the + operator at which time I get this error message: "..... must take either zero or one argument"
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

class Cstring
{
private:
 //array that will hold the strings
 char array[50];
public: 
 //default constructor
 Cstring();   
 //constructor that turns a single character into a string  
 Cstring(char&);
 //constructor that creates a copy of a string
 Cstring(Cstring&);
 //////////////////////////////////
 //overloaded operators for strings
 //////////////////////////////////
 //this operator adds a single character to the end of a string
 friend Cstring operator + (Cstring&);
 //this operator stores a copy of a string in a result string
 //friend Cstring operator = (Cstring&); //wtf is wrong with this????
 //these are my input/output operators
 friend Cstring operator >> (istream&, Cstring&);
 friend Cstring operator << (ostream&, Cstring&); 
 //function that returns the first character of the string   
 char head();
 //function that returns the string with the first character removed
 Cstring tail();
};

/////////////////////////////////////
//Constructors
////////////////////////////////////

//default constructor
Cstring::Cstring()
{
 for(int i=0; i<50; i++)
 array[i]=' ';
}

//constructor that makes character a string
Cstring::Cstring(char &stuff)
{
 array[0] = stuff;
 for (int i=1; i<50; i++)
 array[i] = ' ';
}

//constructor that copies string to result string
Cstring::Cstring(Cstring &other)
{
 for (int i=0; i<50; i++)
 array[i] = other.array[i];
}

/////////////////////////////////////
//Functions
/////////////////////////////////////

//this is the head function that returns first character of string
char Cstring::head()
{
 return array[0];
}

//this is the tail function that returns the string without the first character
Cstring Cstring::tail()
{
 Cstring temp;
 for(int i=1; i<50; i++)
 {
  temp.array[i] = array[i];     
 }
 return temp;  
}

///////////////////////////////////
//Operator Overloads
///////////////////////////////////

Cstring Cstring::operator+(Cstring &left, char &right)
{
 Cstring temp;
 for(int i=0; i<50; i++)
   temp.array[i] = left.array[i];
         
 for(int j=0; j<50; j++)
 {
  if(temp.array[j]==' ')
  {
   temp.array[j] = right;
   break;
  }
 }
 return temp;           
}





int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 10-04-2006, 08:15 PM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Rep Power: 10
hoser2001 is on a distinguished road
Default

Alright, I finally got my class to compile, and I'm pretty sure everything is alright, but now I have to use this class in another program in which I perform a couple of recursive operations. RECURSION PWNS ME....

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

class Cstring
{
private:
 //array that will hold the strings
 char array[50];
public: 
 //default constructor
 Cstring();   
 //constructor that turns a single character into a string  
 Cstring(char&);
 //constructor that creates a copy of a string
 Cstring(Cstring&);
 //////////////////////////////////
 //overloaded operators for strings
 //////////////////////////////////
 //this operator adds a single character to the end of a string
 friend Cstring operator + (Cstring left, const char right);
 //this operator stores a copy of a string in a result string
 Cstring Cstring::operator = (Cstring& right);
 //these are my input/output operators
 friend istream& operator >> (istream& input, Cstring& right);
 friend ostream& operator << (ostream& output, Cstring& right); 
 //function that returns the first character of the string   
 char head();
 //function that returns the string with the first character removed
 Cstring tail();
};

/////////////////////////////////////
//Constructors
////////////////////////////////////

//default constructor
Cstring::Cstring()
{
 for(int i=0; i<50; i++)
 array[i]= ' ';
}

//constructor that makes character a string
Cstring::Cstring(char &stuff)
{
 array[0] = stuff;
 for (int i=1; i<50; i++)
 array[i] = ' ';
}

//constructor that copies string to result string
Cstring::Cstring(Cstring &other)
{
 for (int i=0; i<50; i++)
 array[i] = other.array[i];
}

/////////////////////////////////////
//Functions
/////////////////////////////////////

//this is the head function that returns first character of string
char Cstring::head()
{
 return array[0];
}

//this is the tail function that returns the string without the first character
Cstring Cstring::tail()
{
 Cstring temp;
 for(int i=1; i<50; i++)
 {
  temp.array[i] = array[i];     
 }
 return temp;  
}

///////////////////////////////////
//Operator Overloads
///////////////////////////////////

Cstring operator+ (Cstring left, char right)
{
 Cstring temp;
 for(int i=0; i<50; i++)
   temp.array[i] = left.array[i];
         
 for(int j=0; j<50; j++)
 {
  if(temp.array[j]==' ')
  {
   temp.array[j] = right;
   break;
  }
 }
 return temp;           
}

ostream& operator<<(ostream& output, Cstring &right)
{
 for(int i=0; i<50; i++)
 {
  if(right.array[i] != ' ')
  output << right.array[i];
 }
}

istream& operator>>(istream& input, Cstring &right)
{
 Cstring temp;
 input.getline(temp.array,50);
 input >> temp;
}
Cstring Cstring::operator=(Cstring& right)
{
 int i=0;
 Cstring temp;
 temp.array[i] = right.array[i];
 return temp;
}
I have 3 recursive operations I need to perform:
1.)a function that returns the reverse of a string
2.)a function that concatenates the reverse of a string onto the original string
3.)a function that tests if a certain string is the prefix of another string

If anyone can point me in some possible dirrection, or even try to explain to me exactly how it is recursion works I would appreciate the help greatly!!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-05-2006, 09:10 AM
Ronin Ronin is offline
Programming Professional
 
Join Date: Apr 2006
Posts: 299
Rep Power: 11
Ronin is on a distinguished road
Default

That is a neat function. I'm not sure I will get the syntax right here but couldn't you use this to return the reverse of a string: (I'm also not sure of the array functions)

Code:
array[0] = array;
rArray[0] = array;
int count=0;

for (int i=length(array); i>=0; i--) 
{
     rArray[count] = array[i];
     count++;
}
I could be way off though.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 10-05-2006, 12:56 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

You have most of what you need for doing a reverse.
The basic idea of recursion is to break a task into many smaller steps that are identical. For instance, if you want to compute a factorial (5! = 5*4*3*2*1), one way to do it is to say n! = n*(n-1)!, and note that 0!=1.

For reversing your string, just note that MyString.rev() = MyString.tail().rev()+MyString.head(), and a single character string is its own reverse.

Once you have part 1 done, part 2 is trivial. For testing the prefix, just test the heads and tails of the strings.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 10-06-2006, 02:37 PM
Crane's Avatar   
Crane Crane is offline
Programming Expert
 
Join Date: Nov 2005
Posts: 399
Rep Power: 14
Crane is on a distinguished road
Default

that is a nice looking class. May I ask why you are recreating a string class though?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 10-07-2006, 05:31 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

It's a pretty standard programming project for classes.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue writing to file: pointer to a class which contains pointers to other classes Sheemer C and C++ 0 08-21-2007 02:17 AM
Class question in Delphi tosh5457 Pascal/Delphi 4 06-11-2007 02:46 PM
null exception problem connor7777 C# Programming 2 03-28-2007 12:37 PM
Creating a Custom Cursor ahsan16 Tutorials 2 01-13-2007 06:03 PM
Java Help Files xXHalfSliceXx Java Help 3 11-29-2006 12:30 AM


All times are GMT -5. The time now is 02:13 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads