Link to home
Start Free TrialLog in
Avatar of ghost8067
ghost8067

asked on

Warnings in my string class and a couple of other questions

Hi,
Icreated the following string class and I am getting a coupleofwarnings. Not sure if this is because I have a main in in here to test. The warnings are:

(11) : warning C4996: 'strcpy' was declared deprecated
string.h(73) : see declaration of 'strcpy'
        Message: 'This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
c:\documents and settings\administrator\my documents\visual studio 2005\projects\string\string\string.cpp(17) : warning C4996: 'strcpy' was declared deprecated
string.h(73) : see declaration of 'strcpy'
        Message: 'This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'

The code is below

#ifndef strings_H
#define strings_H
#include <iostream>
using namespace std;

class String {
public:
      String(const char* strings ="", int len = 0):length(len){
          cout<<"\nConstructor is Called, new String object is created"<<endl;
            str = new char[length+1]; //allocate memory
            strcpy(str, strings);
      }

      String(String &strings): length(strings.length){
            cout<<"\nCopy Constructor is called: A String object is copied to another String object"<<endl;
            str = new char[length+1]; //allocate memory
            strcpy(str, strings.str);
        }
      String operator=(String strings){
          cout<<"\nAssignment Operator is called, a String object is assigned to another String object"<<endl;
            return String(strings);
      }
      ~String(){
            cout<<"\nDestructor is Called: Stirng object is Destroyed"<<endl;       
            delete [] str;
        }

private:
      int length;
      char* str;
};
#endif

int main(){
String str("Hello", 5);
String str1("World", 5);

String str2(str1);

str = str1;
return 0;
}

I also have a couple of other questions. Can you give me an opinion as to whetrher my program meets the following requirements?

Implement a simple string class in native C++ that holds a char* and an integer length as

private data members. Provide a constructor which takes an argument of type const char*,

and implement the copy constructor, assignment operator and destructor functions. Verify that

your class works. You will find it easiest to use the string functions from the <cstring> header

file. Create a main to test your class. All methods should be exercised (including the copy constructor and destructor (never called directly)).

Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

Warnings are shown because functions like strcpy are originally from C language, and are not recommended to use in C++. Although, DevC++ compiler for example doesn't report any warnings. But as said in assignment text, it is ok to use functions from header <cstring.h> (Include that header too.)

Everything else is ok, except:

1. You should change arguments of copy constructor  and = operator:

from String or String& to
const String&
Avatar of ghost8067
ghost8067

ASKER

Canyou give mean examle of the change? The prof alsosaid hewouldcreate his own main to test the assignment. In that case, do you think I should delete mine?
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry, write #include <cstring>

instead of #include <cstring.h>
Wonderful! Thank you!
Avatar of Infinity08
>> (11) : warning C4996: 'strcpy' was declared deprecated

What compiler is this ???? strcpy is NOT deprecated at all and is still a valid part of the C++ language.

I assume it's Visual C++ ? Sometimes, I just don't understand their logic. They're trying to write a compiler for an existing standard language, and then they declare half of the standard language libraries "deprecated" and provide their own "better" implementations.