1. int *y = NULL
#include <iostream>
using namespace std;
int add(int a[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += a[i];
}
return sum;
}
int add(int a[], int size, int b[]) {
return add(a, size) + add(b,size);
}
int main() {
int a[] = { 1,2,3,4,5 };
int b[] = { 6,7,8,9,10 };
int c = add(a, 5);
int d = add(a, 5, b);
cout << c << endl;
cout << d << endl;
}
#include <iostream>
using namespace std;
int add(int x[], int size, int y[] = NULL) { // NULL 대신 nullptr로 하는 것이 바람직함
int s = 0;
for(int i=0; i<size; i++) // 배열 a의 합을 구한다.
s += x[i];
if(y == NULL) // NULL 대신 nullptr로 하는 것이 바람직함
return s;
for(int i=0; i<size; i++) // 배열 b를 합한다.
s += y[i];
return s;
}
int main() {
int a[] = {1,2,3,4,5};
int b[] = {6,7,8,9,10};
int c = add(a, 5); // 배열 a의 정수를 모두 더한 값 리턴
int d = add(a, 5, b); // 배열 a와 b의 정수를 모두 더한 값 리턴
cout << c << endl; // 15 출력
cout << d << endl; // 55 출력
}
2.
#include <iostream>
#include <string>
using namespace std;
class Person {
int id;
double weight;
string name;
public:
void show() { cout << id << ' ' << weight << ' ' << name << endl;}
Person(int id = 1, string naem = "Grace", double weight = 20.5);
};
Person::Person(int id, string name, double weight) {
this->id = id;
this->name = name;
this->weight = weight;
}
int main() {
Person grace, ashley(2, "Ashley"), helen(3, "Helen", 32.5);
grace.show();
ashley.show();
helen.show();
}
3.
(1)
#include <iostream>
#include <string>
using namespace std;
int big(int a, int b) {
int n;
n = a > b ? a : b;
return n < 100?n:100;
}
int big(int a, int b, int c) {
int n = a > b ? a : b;
return n > c ? c : n;
}
int main() {
int x = big(3, 5);
int y = big(300, 60);
int z = big(30, 60, 50);
cout << x << ' ' << y << ' ' << z << endl;
}
(2)
#include <iostream>
#include <string>
using namespace std;
int big(int a, int b, int c = 100) {
int n = a > b ? a : b;
return n > c ? c : n;
}
int main() {
int x = big(3, 5);
int y = big(300, 60);
int z = big(30, 60, 50);
cout << x << ' ' << y << ' ' << z << endl;
}
4.
#include <iostream>
#include <string>
using namespace std;
class MyVector {
int* mem;
int size;
public:
MyVector(int val, int n = 100);
~MyVector() { delete[] mem; }
};
MyVector::MyVector(int val, int n) {
mem = new int[n];
size = n;
for (int i = 0; i < size; i++)
mem[i] = 0;
}
int main() {
}
5. public: 유의
#include <iostream>
#include <string>
using namespace std;
class ArrayUtility {
public:
static void intToDouble(int source[], double dest[], int size);
static void doubleToInt(double source[], int dest[], int size);
};
void ArrayUtility::intToDouble(int source[], double dest[], int size) {
for (int i = 0; i < size; i++) {
dest[i] = (double)source[i];
}
}
void ArrayUtility::doubleToInt(double source[], int dest[], int size) {
for (int i = 0; i < size; i++) {
dest[i] = (int)source[i];
}
}
int main() {
int x[] = { 1,2,3,4,5 };
double y[5];
double z[] = { 9.9, 8.8, 7.7, 6.6,5.6 };
ArrayUtility::intToDouble(x, y, 5);
for (int i = 0; i < 5; i++) cout << y[i] << ' ';
cout << endl;
ArrayUtility::doubleToInt(z, x, 5);
for (int i = 0; i < 5; i++) cout << x[i] << ' ';
cout << endl;
}
7. (double)rand() / RAND_MAX
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Random {
public:
static void seed() { srand((unsigned)time(0)); }
static int nextInt(int min = 0, int max = 32767);
static char nextAlphabet();
static double nextDouble();
};
int Random::nextInt(int min, int max) {
return rand() % (max - min + 1) + min;
}
char Random::nextAlphabet() {
int r = rand() % 52;
if (r < 26)
return 'A' + r;
else
return 'a' + (r - 26);
}
double Random::nextDouble() {
return (double)rand() / RAND_MAX;
}
int main() {
Random::seed();
cout << "1에서 100까지 랜덤한 정수 10개를 출력합니다." << endl;
for (int i = 0; i < 10; i++) {
cout << Random::nextInt(1, 100) << " ";
}
cout << endl;
cout << "알파벳을 랜덤하게 10개를 출력합니다." << endl;
for (int i = 0; i < 10; i++) {
cout << Random::nextAlphabet() << " ";
}
cout << endl;
cout << "랜덤한 실수를 10개 출력합니다." << endl;
for (int i = 0; i < 10; i++) {
cout << Random::nextDouble() << " ";
}
cout << endl;
}
9. static 멤버 변수의 전역 변수 선언 / 클래스명::함수명() 호출
#include <iostream>
#include <string>
using namespace std;
class Board {
static string post[100];
static int index;
public:
static void add(string s);
static void print();
};
string Board::post[100];
int Board::index = 0;
void Board::add(string s) {
if (index < 100)
post[index++] = s;
}
void Board::print() {
cout << "********** 게시판입니다. ************" << endl;
for (int i = 0; i < index; i++) {
cout << index << ": " << post[i] << endl;
}
index = 0;
}
int main() {
Board::add("중간고사는 감독 없는 자율 시험입니다.");
Board::add("코딩 라운지 많이 이용해주세요.");
Board::print();
Board::add("진소린 학생이 경진대회 입상하였습니다. 축하해주세요");
Board::print();
}
'C++' 카테고리의 다른 글
[명품 C++ Programming] 8장 실습 문제 (0) | 2025.05.24 |
---|---|
[명품 C++ Programming] 7장 실습 문제 (0) | 2025.05.24 |
[명품 C++ Programming] 5장 실습 문제 (0) | 2025.04.27 |
[명품 C++ Programming] 3장 실습 문제 (1) | 2025.04.26 |
[명품 C++ Programming] 3장 Open Challenge (지수 표현 클래스 만들기) (0) | 2025.04.26 |