Thread: derived class

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

    derived class

    Hi, what is wrong with my derived class called CaseString. The purpose of the class is to convert all the characters in a string object to both uppercase and lowercase without modifying the original string object (so that means I need to make a new object with all the modifications and I need to return that object and keep the original object untouched). The CaseString class is derived from the String class and the only extra thing that it can do is that it can convert the string to both upper and lower case. I have to make this class no matter how pointless it sounds. I know I could do this function in the String class but I need to make this CaseString class. I will post the code for the derived class below and any help will be greatly appreciated. Thanks.

    Code:
    //case.h
    
    #ifndef CaseString_H
    #define CaseString_H
    
    #include <iostream>
    #include "strdrv.h"
    #include "string.h"
    
    
    class CaseString: public String
    {
    private:
    	char* lower_case;
    	char* upper_case;
        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()
    {
    	lower_case = new char[size];
    	upper_case = new char[size];
    	memset(lower_case, 0, size);
    	memset(upper_case, 0, size);
    }
    
    
    CaseString::CaseString(const char *str)
    {
        lower_case = new char[size];
    	upper_case = new char[size];
    	for (int i = 0; i <= stringlength; ++i )
    	{
    		lower_case[i] = tolower(lower_case[i]);
    		upper_case[i] = toupper(upper_case[i]);
    	}
    
    }
    
    CaseString::CaseString(const CaseString &rhs)
    {
    	lower_case = new char[size];
    	upper_case = new char[size];
    	for (int i = 0; i <= rhs.getLength(); ++i )
    	{
    		lower_case[i] = tolower(lower_case[i]);
    		upper_case[i] = toupper(upper_case[i]);
    	}
    
    }
    
    CaseString::~CaseString()
    {
    	delete []lower_case;
    	delete []upper_case;
    }
    
    CaseString& CaseString::operator =(const CaseString &rhs)
    {
    	lower_case = rhs.lower_case;
    	upper_case = rhs.upper_case;
    	return *this;
    }
    
    void CaseString::print()
    {
    	printf("%s: %s(%d)\n", name, buf, stringlength);
    	printf("%s: %s(%d)\n", name, lower_case, stringlength);
    	printf("%s: %s(%d)\n", name, upper_case, stringlength);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to call the base class constructors from the derived class constructors to initialize the base class data before you use it. For example, you are using size in your constructors, but that is part of the base class and it hasn't been initialized correctly if you haven't called the base class constructor.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    You have to call the base class constructors from the derived class constructors to initialize the base class data before you use it. For example, you are using size in your constructors, but that is part of the base class and it hasn't been initialized correctly if you haven't called the base class constructor.
    the size and stringlength variables seem to work correctly when I look at their values in the debugger.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Don't the variables initialize if I write:

    Code:
    class CaseString: public String

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    You have to call the base class constructors from the derived class constructors to initialize the base class data before you use it. For example, you are using size in your constructors, but that is part of the base class and it hasn't been initialized correctly if you haven't called the base class constructor.
    The base constructor is always called. If you do not do it yourself, the argument-less constructor (default or user-supplied) is invoked. If no such constructor exists, you get a compile time error.

    It's impossible to fail to initialize the base class. It just might not happen quite the way you wanted, is all.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by brewbuck View Post
    The base constructor is always called. If you do not do it yourself, the argument-less constructor (default or user-supplied) is invoked. If no such constructor exists, you get a compile time error.

    It's impossible to fail to initialize the base class. It just might not happen quite the way you wanted, is all.
    Do you know how I can fix my class? Thanks

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by BKurosawa View Post
    Do you know how I can fix my class? Thanks
    I have no idea what's wrong with it. You haven't told us.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> It's impossible to fail to initialize the base class. It just might not happen quite the way you wanted, is all.

    You are correct, which is why I said that the data has not been initialized correctly. For example, in the const char * constructor, the variables size and stringlength are used with the assumption that they hold the size of the array and the length of the passed in string. But how could they when the default base class constructor is called which has no knowledge of str?

    There is a big difference between data being initialized and data being initialized correctly.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    There is a big difference between data being initialized and data being initialized correctly.
    Yep. I was too lazy to read the code carefully.

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by brewbuck View Post
    I have no idea what's wrong with it. You haven't told us.
    I mean is there anything wrong with the code? Because the program crashes when I run it. When I debug it breaks it opens up a new set of code in the ostream library and it points to this:

    Code:
    		__CLR_OR_THIS_CALL ~_Sentry_base()
    			{	// destroy after unlocking
    			if (_Myostr.rdbuf() != 0)
    				_Myostr.rdbuf()->_Unlock();
    			}
    I have no idea why it does that. Any ideas? Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  4. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  5. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 05:05 PM