• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

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

0
ghost8067
Asked:
ghost8067
  • 3
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now