Имеется задача: Создать класс Животное, содержащий в качестве параметров классификацию(строка),число конечностей,количество потомков. Определить конструкторы,деструктор и функцию печати. Создать public-производный класс- домашнее животное, имеющий дополнительный параметр-кличку. Определить конструкторы по умолчанию и с разным числом параметров, деструткор, функцию печати. Определить функции переназначения названия клички и числа потомков.
Вот код этой задачи:
C++ |
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
134
| / ConsoleApplication1.cpp: главный файл проекта.
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <windows.h>
#include "string"
using namespace std;
class animal {
protected:
char* classification;
char* habitat;
int limb;
int children;
public:
animal() {
char a[] = "none";
classification = new char[strlen(a) + 1];
strcpy(classification, a);
habitat = new char[strlen(a) + 1];
strcpy(habitat, a);
limb = 0;
children = 0;
}
animal(char* classification, char* habitat, int limb, int children) {
this->classification = new char[strlen(classification) + 1];
strcpy(this->classification, classification);
this->habitat = new char[strlen(habitat) + 1];
strcpy(this->habitat, habitat);
this->limb = limb;
this->children = children;
}
void print() {
cout << "Классификация: " << classification << endl;
cout << "Ареал обитания: " << habitat << endl;
cout << "Число конечностей: " << limb << endl;
cout << "Число потомков: " << children << endl;
}
~animal() {
if (classification)
delete[]classification;
if (habitat)
delete[]habitat;
}
};
class pet :public animal {
private:
char* name;
public:
pet() {
char a[] = "unnamed";
name = new char(strlen(a) + 1);
strcpy(name, a);
}
pet(char* name) {
this->name = new char(strlen(name) + 1);
strcpy(this->name, name);
}
pet(char* classification, char* habitat, int limb, int children, char* name) :animal(classification, habitat, limb, children) {
this->name = new char(strlen(name) + 1);
strcpy(this->name, name);
}
~pet() {
if (name)
delete[]name;
}
void petprint() {
animal::print();
cout << "Кличка: " << name << endl;
}
void namechange(char* cname) {
delete[]name;
name = new char(strlen(cname) + 1);
strcpy(name, cname);
}
void childrenchange(int number) {
children = number;
}
};
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
char a[] = "Собака";
char c[] = "Вода";
int b = 4;
int d = 8;
char e[] = "Моряк";
char f[] = "Морпех";
pet s(a, c, b, d, e);
s.petprint();
cout << endl << endl << "Смена клички и числа потомков " << endl << endl;
s.namechange(f);
s.childrenchange(9);
s.petprint();
system("pause");
return 0;
} |
|
По примеру этой задачи помогите реализовать мою задачу:
Создать класс Машина, содержащий в качестве параметров марку(строка), число цилиндров, мощность. Определить конструкторы, деструктор и функцию печати. Создать public-производный класс - Грузовик, имеющий дополнительный вещественный параметр - грузоподъёмность кузова. Определить конструкторы по умолчанию и с разным числом параметров, деструктор, функцию печати. Определить функции переназначения марки и грузоподъёмности.
вот мой код( но я запутался там немного, не работает он):
C++ |
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
| #include "stdafx.h"
#include <iostream>
#include <cstring>
#include <windows.h>
#include <string.h>
using namespace std;
class car {
protected:
char* marka;
int cilindr;
int power;
public:
car() {
char a[] = "none";
marka = new char[strlen(a) + 1];
strcpy(marka, a);
cilindr = 0;
power = 0;
}
car(char* marka,int cilindr, int power) {
this->marka = new char[strlen(marka) + 1];
strcpy(this->marka, marka);
this->cilindr = cilindr;
this->power = power;
void print() {
cout << "Марка: " << marka << endl;
cout << "Число цилиндров: " << cilindr << endl;
cout << "Мощность: " << power << endl;
}
~car() {
if (marka)
delete[]marka;
}
};
class pet :public car {
private:
int gruz;
public:
pet() {
char a[] = "ungruzed";
gruz = new char(strlen(a) + 1);
strcpy(gruz, a);
}
pet(char* gruz) {
this->gruz = new char(strlen(gruz) + 1);
strcpy(this->gruz, gruz);
}
pet(char* marka,int cilindr, int power, char* gruz) :car(marka, cilindr, power) {
this->gruz = new char(strlen(gruz) + 1);
strcpy(this->gruz, gruz);
}
~pet() {
if (gruz)
delete[]gruz;
}
void petprint() {
car::print();
cout << "Грузоподьемность: " << gruz<< endl;
}
void markachange(char* cmarka) {
delete[]marka;
marka = new char(strlen(cmarka) + 1);
strcpy(marka, cmarka);
}
void gruzchange(int number) {
gruz = number;
}
};
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
char a[] = "Man";
int b = 8;
int d = 420;
char e[] = "21";
char f[] = "25";
pet s(a, c, b, d, e);
s.petprint();
cout << endl << endl << "Смена марки и грузоподьемности " << endl << endl;
s.gruzchange(f);
s.markachange(a);
s.petprint();
system("pause");
return 0;
} |
|
напишите полный код, пожалуйста.