#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
class Test {
char* name;
public:
Test() {
}
Test(char* p) {
name = p;
}
Test(const Test& p) {
name = new char[strlen(p.name) + 1];
strcpy(name, p.name);
}
void print() {
printf("%s \n", name);
}
};
void main() {
Test t1("Test1");
t1.print();
Test t2 = t1;
t2.print();
}
========================================================================
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
class Student {
char* name, *studentNum;
int kor, math, eng;
public:
Student(char* n, char* stN, int k, int m, int e) {
name = n;
studentNum = stN;
kor = k;
math = m;
eng = e;
}
Student(const Student& s) {
name = new char[strlen(s.name) + 1];
strcpy(name, s.name);
studentNum = new char[strlen(s.studentNum) + 1];
strcpy(studentNum, s.studentNum);
kor = s.kor;
math = s.math;
eng = s.eng;
}
void show() {
cout << "이름은 : " << name << ", 학번은 : " << studentNum << ", 국어는 : " << kor <<
", 수학은 : " << math << ", 영어는 : " << eng << endl;
}
/*~Student() {
delete name;
delete studentNum;
}*/
};
void main() {
Student s1("이름", "2017", 8,9,10);
Student s2 = s1;
Student s3 = s1;
s1.show();
s2.show();
s3.show();
// delete , new : 동적할당 된 곳에서만 사용 가능
// delete s1;
}
댓글 없음:
댓글 쓰기