class method

This is a discussion on class method within the C++ Programming forums, part of the General Programming Boards category; Header File: Code: cSTRING& Reverse(); CPP File Code: cSTRING& cSTRING::Reverse () { cout << "INSIDE REVERSE" << endl; int nCnt ...

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Question class method

    Header File:
    Code:
    cSTRING& Reverse();
    CPP File
    Code:
    cSTRING& cSTRING::Reverse ()
    {
    	cout << "INSIDE REVERSE" << endl;
    	int nCnt = strlen(pszName1)-1;
    	for (int x=0; x<strlen(pszName1); x++)
    	{
    		pszName1[x]=pszName1[nCnt];
    		nCnt+=-1;
    	}
    	for (int x=0; x<strlen(pszName1); x++)
    	cout << pszName1[x];
    	return *this;
    }
    DRIVER FILE:
    Code:
    cout << "\n\nNow reversing:::::" << endl;
    	cSTRING s5;
    	s2.Reverse();
    For some reason it does not even enter the function.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Okay, that's not helpful. It really would be nice if you posted a small, compilable program that we could cut and paste to see what you're talking about. A hands-on look at run-time problems generally makes finding a solution easier. With what you've given I can only speculate, and I don't like to speculate. The chances of being wrong are just too high.
    My best code is written with the delete key.

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    lol ok
    HEADER FILE:
    Code:
    #ifndef __cDATE_H
    #define __cDATE_H
    
    #include <iostream.h>
    #include <string.h>
    
    class cSTRING 
    {
    	protected:
    		int _nCmpValue, _num1, _num2;
    		char *pszName1, *pszName2;
    	public:
    		cSTRING();
    		
    		cSTRING(char *pname, int returnVal);
    
    		cSTRING(char *pszStr1);
    		cSTRING(cSTRING &rhs);			
    				
    		~cSTRING();
    
    		char *getName1()
    		{
    			return this->pszName1;
    		}
    		
    		char *getName2()
    		{
    			return this->pszName2;
    		}
    
    
    		void print()
    	    {
    		    cout << pszName1 << endl;
    	    }
    
    		cSTRING& operator=(const cSTRING& cStr);
    		cSTRING operator+(const cSTRING& cStr);
    		cSTRING& operator+=(const cSTRING& cStr);
    		int operator==(const cSTRING& cStr);
    		int operator!=(const cSTRING& cStr);
    		cSTRING& Reverse();
    		
    		friend ostream &operator<<(ostream &o, cSTRING& cStr);
    		friend ostream &operator>>(ostream &o, cSTRING& cStr);
    		//cSTRING& strrev(cSTRING& cStr);
    		//int palindrome(const cSTRING& cStr);
    		
    };
    
    #endif
    CPP FILE:
    Code:
    #include "cDATE.h"
    #include <iostream.h>
    #include <string.h>
    
    cSTRING::cSTRING(char *name, int returnVal)
    {
    	_nCmpValue = returnVal;
    	pszName1=0;
    	pszName2=0;
    	
    	pszName1 = new char[strlen(name) + 1];
    	pszName2 = new char[strlen(name) + 1];
         //Allocates an character array object
    	strcpy(pszName1, name);
    	strcpy(pszName2, name);
    
    	cout << "cSTRING Default Constructor Called" << endl;
    }
    
    
    cSTRING::cSTRING(char *name1)
    {
    	this->pszName1 = 0;
    	this->pszName2 = 0;
    
    	pszName1 = new char[strlen(name1) + 1];
    	pszName2 = new char[strlen(name1) + 1];
    	pszName1 = strcpy(pszName1, name1);
    	for (int x=0; x<6; x++)
    		cout << name1[x];
    	cout << "Names copied" << endl;
    }
    
    
    cSTRING& cSTRING::operator=(const cSTRING& other)
    {
    	cout << "inside =" << endl;
    	
    	if (this!=&other)  // check for self-assignment
    	{
    		// deallocate old memory
    		delete [] pszName1;
    		delete [] pszName2;
    
    		// allocate new memory
    		pszName1 = new char[strlen(other.pszName1) + 1];
    
    		// copy
    		strcpy(pszName1, other.pszName1);
    	
    	}
    	return *this;
    }
    cSTRING cSTRING::operator+(const cSTRING& rhs)
    {
    	cout << "inside +" << endl;
    	char tmp[50];	//temporary variable
    	char *pszTemp=tmp;
    	
    	
    	//pszTemp = strcpy(pszTemp, temp);
    		
    	//getting size for new string
    	int nAlloc = (strlen(pszName1) + strlen(rhs.pszName1) +1 );
    	cout << nAlloc << endl;
    	
    	//backing-up
    	strcpy(pszTemp, pszName1);
    	
    	//deallocating old memory
    	delete [] pszName1;
    	delete [] pszName2;
    	
    		//allocating new memory
    	pszName1 = new char [nAlloc];
    	pszName2 = new char [nAlloc];
    	
    	strcat(pszTemp, rhs.pszName1);  
    	strcpy(pszName1,pszTemp);
    	
    	return *this;    
    }
    
    
    cSTRING& cSTRING::operator+=(const cSTRING& rhs)
    {	
    	char tmp[50];	//temporary variable
    	char *pszTemp=tmp;
    	
    	cout << "inside +=" << endl;
    	
    	//getting size for new string
    	int nAlloc = (strlen(pszName1) + strlen(rhs.pszName1) +1 );
    	cout << nAlloc << endl;
    	
    	//backing-up
    	strcpy(pszTemp, pszName1);
    	
    	//deallocating old memory
    	delete [] pszName1;
    	delete [] pszName2;
    	
    	//allocating new memory
    	pszName1 = new char[nAlloc];
    	pszName2 = new char[nAlloc];
    	
    	pszName1 = strcpy(pszName1,pszTemp);
        pszName1 = strcat(pszName1, rhs.pszName1);
        
    	return *this;
    }
    
    int cSTRING::operator==(const cSTRING& rhs)
    {	
    	//comparing strings
    	if (strcmp(pszName1, rhs.pszName1))
    		return 1;		//returns 1 when not equal
    	else
    		return 0;		//returns 0 when equal
    }
    
    int cSTRING::operator!=(const cSTRING& rhs)
    {	
    	//comparing strings
    	if (strcmp(pszName1, rhs.pszName1))
    		return 0;		//returns 1 when equal
    	else
    		return 1;		//returns 0 when not equal
    }
    
    cSTRING& cSTRING::Reverse ()
    {
    	cout << "INSIDE REVERSE" << endl;
    	int nCnt = strlen(pszName1)-1;
    	for (int x=0; x<strlen(pszName1); x++)
    	{
    		pszName1[x]=pszName1[nCnt];
    		nCnt+=-1;
    	}
    	for (int x=0; x<strlen(pszName1); x++)
    	cout << pszName1[x];
    	return *this;
    }
    
    ostream &operator<<(ostream &o, cSTRING& rhs)
    {
    	
        o << rhs.pszName1 << " (" <<strlen(rhs.pszName1)<<") ";
        return o;
    }
    ostream &operator>>(ostream &o, cSTRING& rhs)
    {
    	cout << "INSIDE cin" << endl;
        //o >> rhs.pszName1;
        return o;
    }
    /*int palindrome(const cSTRING pszName1)
    {
    
      int result;
    
      cout << "Inside PALINDROME: "<< endl;
      
       /* Reverse string and compare (ignore case): */
    /*   result = _stricmp( pszName1, _strrev( _strdup( pszName1 ) ) );
       if( result == 0 )
       		cout << "Is a palindrome:: " << endl;
       else
       		cout << "is not a palindrome " << endl;
       return result;
    }*/
    
    cSTRING::cSTRING(cSTRING &rhs)
    {
    	pszName1 = 0;
    	pszName2 = 0;
    	
        pszName1 = new char[strlen(rhs.getName1()) + 1];
        strcpy(pszName1,rhs.pszName1);
    	
    	pszName2 = new char[strlen(rhs.getName2()) + 1];
        strcpy(pszName2,rhs.pszName2);
    }
    
    cSTRING::~cSTRING()
    { 
    	if (pszName1)	
    		delete[] pszName1;
    	if (pszName2)	
    		delete[] pszName2;
    	cout << "Destructor called" << endl; 
    };
    
    
    
    cSTRING::cSTRING()
    {
    	pszName1=0;
    	pszName2=0;
    }
    DRIVER FILE:
    Code:
    #include "cDATE.h"
    #include <iostream.h>
    #include <string.h>
    
    int main(void)
    {
    	char szStr1[] = "Hello";
    	char szStr2[] = "Hell";
    		
    	int returnVal;
    
    	cSTRING setName1(szStr1);
    	cSTRING setName2(szStr2);
    		
    	cout << "\n\nNow Equalling...." << endl;
    	cSTRING s2 = setName2;
    	cout << s2;
    
    	cout << "\n\nNow Adding...." << endl;
    	cSTRING s3 = setName1;
    	s3 = (s3 + setName2);
    	cout << s3;
    	
    	cout << "\n\nNow +=   " << endl;
    	cSTRING s4 = setName1;
    	s4 += setName2;
    	cout << s4;
    		
    	cout << "\n\nNow Comparing == ...." << endl;
    	returnVal = (setName1 == setName2);
    	cout << endl;
    	cout << returnVal << endl;
    
    	cout << "\n\nNow Comparing != ...." << endl;
    	returnVal = (setName1 != setName2);
    	cout << endl;
    	cout << returnVal << endl;
    	return (0);
    	
    	cout << "\n\nNow reversing:::::" << endl;
    	cSTRING s5;
    	s2.Reverse();
    	/*
    	cout << "\n\nNow palindrome : " << endl;
    	returnVal = palindrome(pszName1);
    	*/
    }
    while you are at it, i would also like to know how would i overload operator>> i got the operator<< to work but this one is tricky
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  4. #4
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    nevermind i figuered it out:
    Code:
    	cout << "\n\nNow Comparing != ...." << endl;
    	returnVal = (setName1 != setName2);
    	cout << endl;
    	cout << returnVal << endl;
    	//return (0);  <-----------------------------------------------
    	
    	cout << "\n\nNow reversing:::::" << endl
    that wa s pretty dumb
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The reverse routine was wrong in that you neglected to swap the characters. You simply overwrote the one at index x. This works better:
    Code:
    cSTRING& cSTRING::Reverse ()
    {
      cout << "INSIDE REVERSE" << endl;
      int nCnt = strlen(pszName1)-1;
      for (int x=0; x<nCnt; x++, nCnt--)
      {
        char save = pszName1[x];
        pszName1[x]=pszName1[nCnt];
        pszName1[nCnt] = save;
      }
      for (int x=0; x<strlen(pszName1); x++)
        cout << pszName1[x];
      return *this;
    }
    For operator>>, you made the same mistake I have a tendency to make. ostream is output, istream is input:
    Code:
    friend ostream &operator<<(ostream &o, cSTRING& cStr);
    friend istream &operator>>(istream &i, cSTRING& cStr);
    ...
    ostream &operator<<(ostream &o, cSTRING& rhs)
    {
      return o << rhs.pszName1 << " (" <<strlen(rhs.pszName1)<<") ";
    }
    istream &operator>>(istream &i, cSTRING& rhs)
    {
      cout << "INSIDE cin" << endl;
      return i >> rhs.pszName1;
    }
    You also have a fun little issue with this line:
    Code:
    s3 = s3 + setName2;
    I'll leave that for you to work on. It can be more instructive than me just telling you the problem.
    My best code is written with the delete key.

  6. #6
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    thanks for reply
    looking @ this code:
    Code:
    s3 = s3+setName2;
    i do not see anything wrong, the code works fine and does it job....
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 11:56 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Replies: 7
    Last Post: 05-26-2005, 11:48 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21