alsav22
5434 / 4829 / 442
Регистрация: 04.06.2011
Сообщений: 13,587
|
04.12.2013, 13:50
|
|
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
| #include <iostream>
#include <windows.h>
#include <string.h>
using namespace std;
class Country
{
private:
double Area;
char* Name;
public:
Country(void);
Country(Country &Country);
Country(char* newName, double newArea);
~Country(void);
double getArea();
char* getName();
void setArea(double newArea);
void setName(char* newName);
void show();
};
Country::Country()
{
Name = NULL;
char* N;
int A;
N = new char[50];
cout<<"Создание экземпляра класса без параметров с последующим заполнением"<<endl;
cout<<"Введите название страны: ";
cin>>N;
setName(N);
cout<<"Введите площадь страны: ";
cin>>A;
setArea(A);
cout<<endl<<"Информация о стране: "<<endl<<"Название: "<<Name<<endl<<"Площадь: "<<Area<<endl;
delete N;
}
Country::Country(char* newName, double newArea)
{
Name = new char[strlen(newName) + 1];
strcpy_s(Name, strlen(newName) + 1, newName);
Area = newArea;
}
Country::Country(Country &Country)
{
Name = new char[strlen(Country.Name) + 1];
strcpy_s(Name, strlen(Country.Name) + 1, Country.Name);
Area = Country.Area;
}
Country::~Country()
{
delete[] Name;
}
char* Country::getName()
{
return Name;
}
double Country::getArea()
{
return Area;
}
void Country::setName(char* newName)
{
if(Name)
delete[] Name;
Name = new char[strlen(newName) + 1];
strcpy_s(Name, strlen(newName) + 1, newName);
}
void Country::setArea(double newArea)
{
Area = newArea;
}
void Country::show()
{
cout<<"Название страны: "<<getName()<<endl;
cout<<"Площадь страны: "<<getArea()<<endl;
}
int main()
{
setlocale(LC_ALL,"Russian");
Country c;
c.show();
Country s("Ukraine", 534.5);
cout<<endl<<"Создание экземпляра класса с вызовом конструктора с параметрами"<<endl;
s.show();
Country a(c);
cout<<endl<<"Создание копии экземпляра класса"<<endl;
a.show();
system("pause");
return 0;
} |
|
Лучше стандартными функциями пользоваться, а не придумками microsoft, меньше путаницы будет.
1
|