Thread: Where do I put what in the implementation file?

  1. #1
    Registered User Mini_Maggit's Avatar
    Join Date
    Mar 2004
    Posts
    3

    Where do I put what in the implementation file?

    Hey all,

    I'm having a bit of trouble understanding this all...

    I want to create a BinaryString class that holds a single array of characters (thus a single char*) and manages it as if it is a binary string that represents a number...

    It should be able to perform operations such as:
    Code:
    BinaryString * a = new BinaryString(5);
    BinaryString b = *a;
    const BinaryString * c = new BinaryString(b);
    a->getString();
    delete a;
    b.getString();
    b.getValue();
    c->getString();
    c->getValue();
    I'm just wondering: where do I put all these declarations in the implementation file & what are they actually meant to do?

    Heres what I've got so far (no idea whether I'm even on the right track):

    Code:
    //BinaryString.h
    
    #ifndef BINARYSTRING_H
    #define BINARYSTRING_H
    
    class BinaryString {
    	public:
    		BinaryString( const char * );			//constructor
    		BinaryString( unsigned int );			//constructor
    		BinaryString( const BinaryString & );		//copy constructor
    		~BinaryString();				//destructor
    		BinaryString& operator=( const BinaryString& );	//assignment
    		unsigned int getValue() const;
    	private:
    		const char * getString() const;
    };
    
    #endif /* BINARYSTRING_H */
    Code:
    //BinaryString.C
    
    #include "BinaryString.h"
    using namespace myutilities;
    
    BinaryString::BinaryString( const char * a ) {
    	if(this == &a)
    		return *this;
    
    	delete getString();
    
    	BinaryString *a = new BinaryString(5);
    	return *this;
    }
    
    BinaryString::BinaryString( unsigned int b ) {
    	BinaryString b = *a;
    }
    
    BinaryString::~BinaryString() {
    	delete GetString();
    }
    
    BinaryString& BinaryString::operator=( const BinaryString& c ) {
    	const BinaryString *c = new BinaryString(b);
    }
    I'm very confused...
    Can anyone help?
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well you seem to be roughly on the right track in terms of structuring the source and header files.

    However, your class lacks any data members
    Code:
    	private:
    		char *theString;
    A constructor would look like this
    Code:
    BinaryString::BinaryString( const char * a ) {
      theString = new char[ strlen(a) + 1 ];
      strcpy( theString, a );
    }
    Your BinaryString( unsigned int ); would need to turn say an integer 5, into a binary string "101", then use the above constructor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM