코딩 및 기타

c++ chapter06 복습 함수 중복 및 static

정지홍 2023. 2. 27. 12:20

#include <iostream>
using namespace std;
int big(int a, int b) {
if (a > b)return a;
else return b;
}
int big(int a[], int size) {
int max = a[0];
for (int i = 1; i < size; i++) {
if (a[i] > max) {
max = a[i];
}
}
return max;
}
int main() {
int ary[5] = { 1,9,-1,8,6 };
cout << big(2, 3) << endl;
cout << big(ary, 5) << endl;
return 0;
}


#include <iostream>
using namespace std;
int sum(int a, int b) {
int sum = 0;
for (int i = a; i <= b; i++)
sum += i;
return sum;
}
int sum(int a) {
int sum = 0;
for (int i = 1; i <= a; i++)
sum += i;
return sum;
}
int main() {
cout << sum(3, 5) << endl;
cout << sum(3) << endl;
cout << sum(100) << endl;
}


#include <iostream>
#include <string>
using namespace std;

void star(int a = 5);
void msg(int id, string texxt = "");

void star(int a) {
for (int i = 0; i < a; i++)cout << '*';
cout << endl;
}

void msg(int id, string texxt) {
cout << id << ' ' << texxt << endl;
}
int main() {
star();
star(10);

msg(10);
msg(10, "hello");
return 0;
}


#include <iostream>
using namespace std;

void f(char c = ' ', int line = 1);

void f(char c, int line) {
for (int i = 0; i < line; i++) {
for (int j = 0; j < 10; j++) {
cout << c;
}
cout << endl;
}
}
int main() {
f();
f('%');
f('@', 5);
}


#include <iostream>
using namespace std;
void fillLine(int n = 25, char c = '*') {
for (int i = 0; i < n; i++) {
cout << c;
}
cout << endl;
}
int main() {
fillLine();
fillLine(10, '%');
return 0;
}


#include <iostream>
using namespace std;

class MyVector {
int* p;
int size;
public:
MyVector(int n=100) {
p = new int[n];
size = n;
}
~MyVector() { delete[]p; };
};
int main() {
MyVector* v1, * v2;
v1 = new MyVector();
v2 = new MyVector(1024);
delete v1;
delete v2;
}


#include<iostream>
using namespace std;
class Person {
public:
int money;
void addMoney(int money) { this->money += money; };
static int shareMoney;
static void addShared(int n) { shareMoney += n; };
};

int Person::shareMoney = 10;

int main() {
Person han;
han.money = 100;
han.shareMoney = 200;

Person lee;
lee.money = 150;
lee.addMoney(200);
lee.addShared(200);

cout << han.money << ' ' << han.shareMoney << endl;
cout << lee.money << ' ' << lee.shareMoney << endl;

}


#include <iostream>
using namespace std;

class Math {
public:
static int abs(int a) { return a > 0 ? a : -a; };
static int max(int a, int b) { return (a > b) ? a : b; };
static int min(int a, int b) { return (a > b) ? b : a; };
};

int main() {
cout << Math::abs(-5) << endl;
cout << Math::max(10, 8) << endl;
cout << Math::min(-3, -8) << endl;

}


#include <iostream>
using namespace std;

class Circle {
private:
static int numOfCircles;
int radius;
public:
Circle(int r = 1);
~Circle() { numOfCircles--; };
double getArea() { return 3.14 * radius * radius; };
static int getNumOfCircles() { return numOfCircles; };
};

Circle::Circle(int r) {
radius = r;
numOfCircles++;
}

int Circle::numOfCircles = 0;

int main() {
Circle* p = new Circle[10];
cout << "생존하고 있는 원의 개수 = " << Circle::getNumOfCircles() << endl;
delete[]p;
cout<<"생존하고 있는 원의 개수 = "<< Circle::getNumOfCircles() << endl;

Circle a;
cout << "생존하고 있는 원의 개수 = " << Circle::getNumOfCircles() << endl;

Circle b;
cout << "생존하고 있는 원의 개수 = " << Circle::getNumOfCircles() << endl;
return 0;
}

'코딩 및 기타' 카테고리의 다른 글

신경망  (1) 2023.03.01
c++ 복습  (0) 2023.02.28
백준3003  (0) 2023.02.26
3주차 데이터베이스의 이해와 활용 | K-MOOC  (0) 2023.02.25
c++ 복습 4-1~4-5  (1) 2023.02.22