Constructor excercise problem


Hello,

i have a problem with an exercise:

the header is given:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef HEADER_H_
#define HEADER_H_

class Cow {
private:
char name[20];
char * hobby;
double weight;
public:
Cow();
Cow(const char * nm, const char * ho, double wt);
Cow(const Cow &c);
~Cow();
Cow & operator=(const Cow & c);
void ShowCow() const; // display all cow data
};

#endif 


Declaration of the function:
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
#include <cstdlib>
#include "Header.h"
#include <iostream>
#include <cstring>
using std::cin;
using std::cout;
using std::endl;



static int num_strings;

Cow::Cow()
{ 
	

name[0]='\0';
	hobby=new char[1];
	hobby[0]='\0';
	

	weight=0.0;
}

Cow::Cow(const char *nm, const char *ho, double wt)
{int len=0;;
int l=0;;
	int length=0;
	
		
	
	len=std::strlen(ho);
	hobby=new char[len+1];
	strcpy_s(hobby,len+1,ho); 
   
	
	
	l=std::strlen(nm);
	strcpy_s(name,nm);
	weight=wt;

	num_strings++;

}

Cow::Cow(const Cow &c)
{


	
	strcpy_s(name,c.name);
	int length=(unsigned)strlen(c.hobby);
	hobby=new char[length+1];
	hobby=c.hobby;
	weight=c.weight;


}

Cow::~Cow()
{

	delete hobby; 
	delete name; 
	
	
}

Cow&Cow::operator= (const Cow & c)
{
	if(this==&c)
		return *this;
	
	delete [] hobby;
	strcpy_s(name,sizeof (name),c.name);
	hobby=c.hobby;
	weight=c.weight;
	return *this;
 }

void Cow::ShowCow() const
{
	cout<<"Output: "<<endl;
	
	cout<<"nem: "<<name<<endl;
	cout<<"hobby: "<<hobby<<endl;
	cout<<"weight: "<<weight<<endl;
}


main:
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

#include <cstdlib>
#include "Header.h"
#include <iostream>


int main()
{using std::cin;
using std::cout;
using std::endl;


char na[]="cow";
char *hob="house";
double Gew=100.1;


Cow Kuh;


Cow Marta(na, hob, Gew);
Cow(Hansa);
Cow Tata=Hansa;
cout<<"Marta"<<endl;
	Marta.ShowCow();
cout<<"Hansa"<<endl;
Hansa.ShowCow();
cout<<"Tata"<<endl;
Tata.ShowCow();
system("pause");
	return 0;
}


The code dont run and i can not find the errors.

Can someone help me please?

P.S: please with strcpy_s.

Thank you!
Last edited on
please with strcpy_s.
As far as I know, strcpy_s() doesn’t exist in C++.

Hints:
Cow.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef HEADER_COW_H
#define HEADER_COW_H

class Cow {
private:
    char name[20];
    char* hobby;
    double weight;
public:
    Cow();
    Cow(const char* nm, const char* ho, double wt);
    Cow(const Cow& c);
    ~Cow();
    Cow& operator=(const Cow& c);
    void ShowCow() const; // display all cow data
};

#endif // HEADER_COW_H 


Cow.cpp:
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
#include "Cow.h"
#include <cstdlib>
#include <cstring>
#include <iostream>

Cow::Cow()
{ 
    name[0] = '\0';
    hobby = new char[1];
    hobby[0] = '\0';
    weight = 0.0;
}

Cow::Cow(const char* nm, const char* ho, double wt)
{
    strncpy(name, nm, 20);

    int len = std::strlen(ho);
    hobby = new char[len+1];
    strncpy(hobby, ho, len+1);

    weight = wt;
}

Cow::Cow(const Cow& c)
{
    strcpy(name, c.name);
    int length = (unsigned)strlen(c.hobby);
    hobby = new char[length+1];
    strcpy(hobby, c.hobby);
    weight = c.weight;
}

Cow::~Cow()
{
    delete[] hobby;
}

Cow& Cow::operator= (const Cow& c)
{
    if(this == &c) { return *this; }
    strcpy(name, c.name);
    delete[] hobby;
    int length = (unsigned)strlen(c.hobby);
    hobby = new char[length+1];
    strcpy(hobby, c.hobby);
    weight = c.weight;
    return *this;
}

void Cow::ShowCow() const
{
    std::cout << "Output:  nem: " << name << "; hobby: " << hobby
              << "; weight: " << weight << '\n';
}


main.cpp:
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
#include "Cow.h"
#include <cstdlib>
#include <iostream>
#include <limits>

int main()
{
    Cow Kuh;

    char na[] = "cow";
    char hob[] = "house";
    double Gew = 100.1;
    Cow Marta(na, hob, Gew);
    std::cout << "Marta:\n";
    Marta.ShowCow();

    Cow Hansa(Marta);
    std::cout << "Hansa:\n";
    Hansa.ShowCow();

    Cow Tata = Hansa;
    std::cout << "Tata:\n";
    Tata.ShowCow();
    // system("pause"); <-- http://www.cplusplus.com/forum/beginner/1988/
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}

Output:
Marta:
Output:  nem: cow; hobby: house; weight: 100.1
Hansa:
Output:  nem: cow; hobby: house; weight: 100.1
Tata:
Output:  nem: cow; hobby: house; weight: 100.1

Press ENTER to continue...

Thank you!

I do not understand why the name array do not have a destructor?
why the name array do not have a destructor?

You don't create 'name[]' by 'new', so it doesn't need to be explicitly deleted. It will die together with its class instance.
Topic archived. No new replies allowed.