1
#include <iostream>
using namespace std;
int main() {
int n;
cout << "몇 개의 정수를 입력합니까? ";
cin >> n;
int* p; // 포인터 선언
p = new int[n]; // 동적 메모리 할당
for (int i = 0; i < n; i++) {
cout << "정수를 입력하시오: ";
cin >> p[i]; // 동적 메모리에 정수 저장
}
cout << "입력된 정수는: ";
for (int i = 0; i < n; i++)
cout << p[i] << ", ";
delete[] p;
return 0;
}
2
#include <iostream>
using namespace std;
int main() {
int n;
cout << "얼마나 많은 이름을 입력 하시겠습니까? ";
cin >> n;
string* p; // 포인터 선언
p = new string[n]; // 동적 메모리 생성
for (int i = 0; i < n; i++) {
cout << "이름 입력 # " << i + 1 << ": ";
cin >> p[i];
}
cout << endl << "다음은 이름 목록입니다." << endl;
for (int i = 0; i < n; i++) {
cout << "이름 #" << i + 1 << ": " << p[i] << endl;
}
delete[] p;
return 0;
}
3
#include <iostream>
#include <time.h>
using namespace std;
class Circle {
public:
int r;
Circle() : r(0) {}
void setRadius(int ra) {
r = ra;
}
int getRadius() {
return r;
}
double getArea() {
return 3.14 * r * r;
}
};
int main() {
int n;
cout << "생성할 원의 개수: ";
cin >> n;
Circle* p; // 포인터 선언
p = new Circle[n];
srand(time(NULL));
for (int i = 1; i <= n; i++) {
int ans = rand() % 100 + 1;
p[i].setRadius(ans);
}
int w = 0;
for (int i = 1; i <= n; i++) {
cout << "원 " << i << "의 반지름 >> " << p[i].getRadius() << endl;
if (p[i].getArea() > 100)
w++;
}
cout << "면적이 100보다 큰 원은 " << w << "개 입니다." << endl;
return 0;
}
4
#include <iostream>
using namespace std;
class Time {
public:
int hour, min, sec;
Time() :hour(0), min(0), sec(0) {}
Time(int h, int m, int s) :hour(h), min(m), sec(s) {}
int gethour() { return hour; }
int getmin() { return min; }
int getsec() { return sec; }
void print() {
cout << "Time 객체: " << hour << ":" << min << ":" << sec << endl;
}
};
bool isEqual(Time& t1, Time& t2) {
if (t1.gethour() == t2.gethour() && t1.getmin() == t2.getmin() && t1.getsec() == t2.getsec())
return true;
else
return false;
}
bool isEqual(Time* pt1, Time* pt2) {
if (pt1->gethour() == pt2->gethour() && pt1->getmin() == pt2->getmin() && pt1->getsec() == pt2->getsec())
return true;
else
return false;
}
int main() {
Time* p;
p = new Time[2];
p[0] = Time(12, 11, 33);
p[1] = Time(12, 11, 33);
for (int i = 0; i < 2; i++)
p[i].print();
if (isEqual(&p[0], &p[1]))
cout << "시간이 일치합니다." << endl;
else
cout << "시간이 일치하지 않습니다." << endl;
if (isEqual(p[0], p[1]))
cout << "시간이 일치합니다." << endl;
else
cout << "시간이 일치하지 않습니다." << endl;
delete[] p;
return 0;
}
5
#include <iostream>
using namespace std;
class Student {
string name;
public:
Student() : name("student") {}
Student(string n) : name(n) {}
void setName(string input) {
name = input;
}
string getName() { return name; }
};
class MyClass {
string className;
Student* p;
int size;
public:
void setName(string input) {
className = input;
}
string getName() { return className; }
// Student
void setStudent() {
p = new Student[size];
}
Student* getStudent() { return p; }
void setSize(int s) {
size = s;
}
int getSize() { return size; }
};
int main() {
MyClass* special;
special = new MyClass[3];
special->setName("special");
special->setSize(3);
special->setStudent();
special->getStudent()[0].setName("홍길동");
special->getStudent()[1].setName("김철수");
special->getStudent()[2].setName("최자영");
cout << "학급 " << special->getName() << "의 학생들은 다음과 같다." << endl;
for (int i = 0; i < special->getSize(); i++)
cout << "학생 #" << i + 1 << ": " << special->getStudent()[i].getName() << endl;
delete[] special->getStudent();
delete[] special;
return 0;
}
'Programming > C++' 카테고리의 다른 글
[C++] 15. STL과 람다식 (1) | 2024.02.17 |
---|---|
[C++] 8. 포인터와 동적객체 생성 (1) | 2024.02.12 |
[어서와 C++는 처음이지!] CH6 Exercise (1) | 2024.01.30 |
[C++] 6. 객체 배열과 벡터 (1) | 2024.01.30 |
[어서와 C++는 처음이지!] CH5 Exercise (0) | 2024.01.29 |