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)).

ghost8067Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

UrosVidojevicCommented:
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&
0
ghost8067Author Commented:
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!
0
UrosVidojevicCommented:
Just this two things:

      String(const 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=(const String& strings){
          cout<<"\nAssignment Operator is called, a String object is assigned to another String object"<<endl;
            return String(strings);
      }

and add #include <cstring.h> at the beginning.

You should use main for your own testing.
When you finish delete or comment main function so that professor can use his own.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

UrosVidojevicCommented:
sorry, write #include <cstring>

instead of #include <cstring.h>
0
ghost8067Author Commented:
Wonderful! Thank you!
0
Infinity08Commented:
>> (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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.