STRCPY

Write your question here.
for some readon compiler makes problem with sprcpy and strcpy_S
HOW TO DOLVE IT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  #include <cassert>
#include <string>
#include <iostream>
#include <string.h>
using namespace std;

class Glasses
{
protected:
	char * color;
	int n_o_l;
public:
	Glasses()
	{
		color = new char[strlen("Black") + 1];
		assert(color != 0);
		strcpy(color, "Black");
		n_o_l = 2;
		cout << "so simple Glasses connstructor \n";
	}
	Glasses(char *col, int number)
	{
		color = new char[strlen(col) + 1];
		assert(color != 0);
		strcpy(color, col);
		n_o_l = number;
		cout << "Glasses constructor\n";
	}

	Glasses(const Glasses &g)
	{
		//bad row
		color = g.color;

		//fix
		color = new char[strlen(g.color) + 1];
		assert(color != 0);

		strcpy(color, g.color);

		n_o_l = g.n_o_l;
		cout << "Another Glasses constructor \n";
	}
	void print()
	{
		cout << "so beautiful" << color << "color glasses with " << n_o_l << " lenses\n";
	}
	~Glasses()
	{
		delete[] color;
		cout << "Someone stepped over my glassess :(\n";
	}
};

class SunGlasses : public Glasses
{
protected:
	double howDark;
public:
	SunGlasses()
	{
		howDark = 2.5;
		cout << "so simple SunGlasses connstructor \n";
	}

	SunGlasses(char *col, int number, double dark) : Glasses(col, number)
	{
		howDark = dark;
		cout << "SunGalsses constructor\n";
	}

	void setHowDark(double dark)
	{
		if (dark<0)
			throw "can't change the darkness \n";
		else
			howDark = dark;
	}
	void print()
	{
		cout << "so beautiful " << color << " color glasses with " << n_o_l << " lenses, and so dark " << howDark << "\n";
	}
	~SunGlasses()
	{
		cout << "someone stopped over my SunGlassess :(\n";
	}
};

class MovieGlasses : public Glasses
{
	int dimension;
public:
	MovieGlasses()
	{
		dimension = 3;
		cout << "So simple movieGlasses constructor\n";
	}
	MovieGlasses(char *color, int number, int dim) : Glasses(color, number)
	{
		dimension = dim;
		cout << "movieglasses constructor\n";
	}
	void print()
	{
		cout << "so beautiful " << color << " color glasses with " << n_o_l << " lenses, and " << dimension << " dimension\n";
	}
	~MovieGlasses()
	{
		cout << "someone stopped over my movieglasses :(\n";
	}
};


int main()
{
	Glasses a("red", 5);
	SunGlasses b("blue", 6, 1.5);
	MovieGlasses *c = new MovieGlasses("pink", 4, 6);
	assert(c != 0);
	b.print();
	c->print();

	system("pause");
	return 0;
}
You do have:
1
2
3
4
5
6
#include <string>

class Glasses
{
protected:
	char * color;


Change that to:
1
2
3
4
5
#include <string>

class Glasses
{
	std::string color;

... and make corresponding changes to every place, where you do use the "color".



PS. You don't make use of virtuals. Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

class Base {
  virtual std::string extras() { return ""; }
public:
  void print() {
    std::cout << "Hello" << extras() << '\n';
 }

 // other code
};

class Derived : public Base {
  std::string extras() { return " world!"; }
};

int main() {
  Derived bar;
  bar.print();
  return 0;
}
Last edited on
i dont understand why to change it to string and not keeping it char?
hmmm its a quastion and i need to understand the answer he compiler will send me .
its build question from a test. i dont want to change anything in the code .
iam over 1 hour working to fix it witout any solution .sorry about my english
Last edited on
It is the easier way to use C++.

The std::string & Co. does menial chores on our behalf so that we can focus on thinking and creating higher level logical errors into our programs.
done and still not working ): man its so hard sometimes
You say "not working" and "compiler makes problem". How about posting the current code and the exact compiler error messages?
Last edited on
Error 1 error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
When we use std::string, we don't need the kinds of strcpy. You did not update your whole code to use the std::string appropriately. This site has Reference documentation. For the string too. With examples.

The std::string is not only easier to use, but also less unsafe. So many have made mistakes with the strcpy that Microsoft too tries to get you out of it. (Their strcpy_s is a non-standard variant of strcpy that tries to avoid some of its shortcomings. The std::string has many more features and is standard; available on every standard compliant platform.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <string>
#include <cassert>
#include<cstring>
using namespace std;

class Glasses
{
protected:
	std::string  *color;
	int number_of_lenses;
public:
	Glasses()
	{
		color = new std::string[strlen("Black") + 1];
		assert(color != 0);
	strcpy(color, "Black");
		number_of_lenses = 2;
		cout << "So simple Glasses constructor\n";
	}
	Glasses(std::string  *col, int number)
	{
		color = new std::string[strlen(col) + 1];
		assert(color != 0);
		strcpy(color, col);
		number_of_lenses = number;
		cout << "Glasses constructor\n";
	}
	Glasses(const Glasses &g)
	{
		color = g.color;
		number_of_lenses = g.number_of_lenses;
		cout << "Another Glasses constructor\n";
	}

	void print()
	{
		cout << "So beautiful " << color << " color glasses with "
			<< number_of_lenses << " lenses\n";
	}

	~Glasses()
	{
		delete color;
		cout << "Someone stepped over my glassess :(\n";
	}
};



class SunGlasses : public Glasses
{
protected:
	double howDark;
public:
	SunGlasses()
	{
		howDark = 2.5;
		cout << "So simple SunGlasses constructor\n";
	}

	SunGlasses(std::string  *col, int number, double dark) :
		Glasses(col, number)
	{
		howDark = dark;
		cout << "SunGlasses constructor\n";
	}

	void setHowDark(double dark)
	{
		if (dark<0)
			cout << "Can't change the darkness\n";
		else
			howDark = dark;
	}

	void print()
	{
		cout << "So beautiful " << color << " color glasses with "
			<< number_of_lenses << " lenses, and so dark "
			<< howDark << "\n";
	}

	~SunGlasses()
	{
		cout << "Someone stepped over my sunglassess :(\n";
	}
};

class MovieGlasses : public Glasses
{
	int dimension;
public:
	MovieGlasses()
	{
		dimension = 3;
		cout << "So simple movieGlasses constructor\n";
	}

	MovieGlasses(std::string  *color, int number, int dim) : Glasses(color, number)
	{
		dimension = dim;
		cout << "MovieGlasses constructor\n";
	}

	void print()
	{
		cout << "So beautiful " << color << " color glasses with "
			<< number_of_lenses << " lenses, and " << dimension
			<< " dimension\n";
	}




	~MovieGlasses()
	{
		cout << "Someone stepped over my movieGlassess :(\n";
	}
};

int main()
{
	Glasses a("red", 5);
	SunGlasses b("blue", 6, 1.5);
	MovieGlasses *c = new MovieGlasses("pink", 4, 6);
	assert(c != 0);
	b.print();
	c->print();

	return 0;
}
Your attention to detail is <unprintable>...

I wrote:
1
2
3
class Glasses
{
	std::string color;


But you did:
1
2
3
class Glasses
{
	std::string * color;

Why?
i succsed .its working for me
tnx man ..for helping
Topic archived. No new replies allowed.