1.
(1). Rect* p;
(2). p = &r
(3). cout<< p->getWidth() << p->getHeight();
2.
(1). q = new Rect(w, h);
(2). cout << "사각형의 면적은 " << q->getArea();
(3). delete q;
3. 1 (생성자가 하나라도 선언된 클래스의 경우, 컴파일러는 기본 생성자를 자동 삽입하지 않는다.-117p)
4. 기본생성자 추가 Rect() { width = 1; height = 1; }
5.
int main()
{
Rect r[5] = { Rect(), Rect(2,3),Rect(3,4),Rect(4,5),Rect(5,6) };
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += r[i].getArea();
}
cout << "면적의 합은 " << sum;
}
6. 4
7. 4동적할당이 아니니까.
8.
기본생성자
기본생성자
기본생성자
소멸자
소멸자
소멸자
9. 1
10. delete [ ]p;
11. 3
12. 3
13.
#include <iostream>
#include <string>
using namespace std;
class Location
{
int width, height;
public:
Location() { this->width = this->height = 0; }
Location(int width, int height)
{
this->width = width; this->height = height;
}
void show();
};
void Location::show()
{
cout << this->width << this->height << endl;
}
14.
동적으로 할당받은 메모리 주소를 잃어버려 힙에 반환할수 없게 되면 메모리 누수가 일어난다
15.
(1).
void f()
{
char* p = new char[10];
strcpy(p, "abc");
delete[] p;
}
(2). 발생하지 않음
(3). 발생하지 않음
(4).
void f()
{
int* p;
for (int i = 0; i < 5; i++)
{
p = new int;
cin >> *p;
if (*p % 2 == 1)
{
delete p;
break;
}
delete p;
}
}
16. 1
17. stoi, stoi
18. 3
19.
int main()
{
string a = "My name is Jane";
char ch = a[2];
if (a == "My name is John") cout << "same";
a = a + "~~";
a[1] = 'Y';
}
'IT공부 > C++' 카테고리의 다른 글
1/19-20 명품C++프로그래밍 6단원 이론문제 (0) | 2024.01.19 |
---|---|
1/18-19 명품c++ 프로그래밍 5단원 이론문제 (0) | 2024.01.18 |
1/17 명품C++프로그래밍 04 (0) | 2024.01.17 |
1/10 명품 c++프로그래밍 03 open challenge, 연습이론문제 (0) | 2024.01.09 |
1/8 명품 C++프로그래밍 2단원 연습문제 난이도 5이상 안품 (0) | 2024.01.08 |