Thread: What is wrong with this class?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    109

    What is wrong with this class?

    I have a derived class called CaseString that is supposed to capitalize and decapitalize the characters of a string. What is wrong with the code?

    Code:
    //case.h
    
    #ifndef CaseString_H
    #define CaseString_H
    
    #include <iostream>
    #include "strdrv.h"
    #include "string.h"
    
    
    class CaseString: public String
    {
    private:
    	char* lower;
    	char* upper;
        String name;
    
    public:
    	CaseString();
        CaseString(const char *);
    	CaseString(const CaseString &rhs);
    	~CaseString();
    	CaseString operator =(const CaseString &rhs);
        void print();
    
     };
    
    #endif
    
    // case.cpp	
    #define _CRT_SECURE_NO_DEPRECATE 1	
    #include <iostream>
    #include <assert.h>
    #include <cctype>
    #include <algorithm>
    #include "case.h"
    
    using namespace std;
    
    CaseString::CaseString()
    {
    	name = NULL;
    	stringlength = 0;
    	size = stringlength + 1; 
    	buf = new char[size];
    	lower = new char[size];
    	upper = new char[size];
    	memset(buf, 0, size);
    	memset(lower, 0, size);
    	memset(upper, 0, size);
    }
    
    
    CaseString::CaseString(const CaseString &rhs)
    {
    	name = NULL;
    	stringlength = rhs.stringlength;
    	size = stringlength + 1;
    	buf = new char[size];
    	lower = new char[size];
    	upper = new char[size];
    	strcpy(buf, rhs.buf);
    	strcpy(lower, rhs.lower);
    	strcpy(upper, rhs.upper);
    	for(int i = 0; i<=stringlength ; i++)
    	{
    		lower[i] = tolower(lower[i]);
    		upper[i] = toupper(upper[i]);
    	}
    
    }
    
    
    CaseString::CaseString(const char *str)
    {
    	name = NULL;
    	stringlength = strlen(str);
    	size = stringlength + 1;
    	buf = new char[size];
    	lower = new char[size];
    	upper = new char[size];
    	strcpy(buf, str);
    	strcpy(lower, str);
    	strcpy(upper, str);
    	for(int i = 0; i <= stringlength; i++)
    	{
    		lower[i] = tolower(lower[i]);
    		upper[i] = toupper(upper[i]);
    	}
    }
    
    
    
    CaseString CaseString::operator =(const CaseString &rhs)
    {
    	if(this != &rhs)
    	{
    		delete []buf;
    		stringlength = rhs.stringlength;
    		size = stringlength + 1;
    		buf = new char[size];
    		lower = new char[size];
    		upper = new char[size];
    		strcpy(buf, rhs.buf);
    		strcpy(lower, rhs.lower);
    		strcpy(upper, rhs.upper);
    		for(int i= 0; i<stringlength; i++)
    		{
    			lower[i] = tolower(lower[i]);
    			upper[i] = toupper(upper[i]);
    		}
    	}
    	return *this;
    }
    
    CaseString::~CaseString()
    {
    
    	delete []buf;
    	delete []lower;
    	delete []upper;
    }
    
    void CaseString::print()
    {
    	printf("%s: %s(%d)\n", name, buf, stringlength);
    	printf("%s: %s(%d)\n", name, lower, stringlength);
    	printf("%s: %s(%d)\n", name, upper, stringlength);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You didn't indicate why you think the code is wrong. Is this question any different than in this thread:

    http://cboard.cprogramming.com/showthread.php?t=92517

    Also, you still didn't call the base class constructor directly. Your solution might work, but its the wrong way to do things.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    You didn't indicate why you think the code is wrong. Is this question any different than in this thread:

    http://cboard.cprogramming.com/showthread.php?t=92517

    Also, you still didn't call the base class constructor directly. Your solution might work, but its the wrong way to do things.
    How do I call the base class constructor directly?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In the initializer list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. wrong ELF class: ELFCLASS64
    By noviC in forum C Programming
    Replies: 22
    Last Post: 03-31-2009, 03:41 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM