<code>

 

 

#include <iostream>
using namespace std;
class ArrayUtility
{
public:
ArrayUtility() {};
~ArrayUtility() {};
static void intToDouble(int source[], double dest[], int size) {
for (int i = 0; i < 5; i++) {
dest[i] = (double)source[i];
}
}
static void doubleToDouble(double source[], int dest[], int size) {
for (int i = 0; i < 5; 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::doubleToDouble(z, x, 5);
for (int i = 0; i < 5; i++) cout << x[i] << ' ';
cout << endl;
}
view raw cp6_5.cpp hosted with ❤ by GitHub

 

 

 

 

#include <iostream>
using namespace std;
class MyVector
{
int* mem;
int size;
public:
MyVector();
MyVector(int n, int val);
~MyVector() { delete[] mem; }
void show() {
for (int i = 0; i < size; i++) cout <<i<<"번: "<< mem[i] << endl;
}
};
MyVector::MyVector()
{
mem = new int[100];
size = 100;
for (int i = 0; i < size; i++) mem[i] = 0;
}
MyVector::MyVector(int n, int val)
{
mem = new int[n];
size = n;
for (int i = 0; i < size; i++) mem[i] = val;
}
int main() {
MyVector v;
MyVector v2(3, 5);
v.show();
v2.show();
}
view raw cp6_4.cpp hosted with ❤ by GitHub
#include <iostream>
using namespace std;
int big(int a, int b) {
int max = 100;
if (a > b) { // 맨 처음 a와 b중 누가 더 큰 수인지 판단
if (a > max) {
return max; // a가 max보다 크면 max값을 리턴
}
else
return a; //아니라면 a의 값을 리턴
}
else {
if (b > max) {
return max;
}
else
return b;
}
}
int big(int a, int b, int m) {
int max = m; // max의 고정하지 않고 매개변수의 값으로 대체
if (a > b) {
if (a > max) {
return max;
}
else
return a;
}
else {
if (b > max) {
return max;
}
else
return b;
}
}
int main() {
int x = big(3, 5);
int y = big(300, 60);
int z = big(30, 60, 50);
cout << x << ' ' << y << ' ' << z << endl;
}
view raw cp6_3.cpp hosted with ❤ by GitHub
#include <iostream>
using namespace std;
class Person
{
int id;
double weight;
string name;
public:
Person();
Person(int i, string name) {
this->id = i;
this->name = name;
this->weight = 20.5; // 매개변수 id와 이름을 설정
}
Person(int i, string name, double w) {
this->id = i;
this->name = name;
this->weight = w;
}
void show() { cout << id << ' ' << weight << ' ' << name << endl; }
};
Person::Person()
{
this->id = 1;
this->weight = 20.5;
this->name = "Grace";
}
int main() {
Person grace, ashley(2, "Ashley"), helen(3, "Helen", 32.5);
grace.show();
ashley.show();
helen.show();
}
view raw cp6_2.cpp hosted with ❤ by GitHub

 

<code>

 

#include <iostream>
using namespace std;
int add(int a[], int b);
int add(int a[], int b, int c[]);
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;
}
int add(int a[], int b) {
int sum=0;
for (int i = 0; i < b; i++) {
sum += a[i];
}
return sum;
}
int add(int a[], int b, int c[]){
int sum=0;
for (int i = 0; i < b; i++) {
sum += a[i] + c[i];
}
return sum;
}
view raw cp6_1.cpp hosted with ❤ by GitHub

 

1) 구현 성공 code

 

#include <iostream>
#include <string>
using namespace std;
class Dept
{
int size;
int* scores;
public:
Dept(int size) {
this->size = size;
scores = new int[size];
}
Dept(const Dept& dept) {
this->size = dept.size;
scores = new int[dept.size];
for (int i = 0; i < dept.size; i++)
this->scores[i] = dept.scores[i]; // 깊은 복사 생성자를 생성
}
~Dept() { delete[]scores; };
int getSize() { return size; }
void read() {
cout << "10개 점수 입력 >>";
for (int i = 0; i < size; i++)
{
cin >> scores[i];
}
}
bool isOver60(int index) {
if (scores[index] > 60)
return true;
else
return false;
}
};
int countPass(Dept &dept) {
int count = 0;
for (int i = 0; i < dept.getSize(); i++) {
if (dept.isOver60(i)) count++;
}
return count;
}
int main() {
Dept com(10);
com.read();
int n = countPass(com);
cout << "60점 이상은 " << n << "명";
}
view raw cp5_12.cpp hosted with ❤ by GitHub

 

 

2) 복사 생성자가 생성되지 않으면, 같은 공간을 향하게 되어서 메모리 오류 생기게 된다. 

메모리 오류가 생기지 않게 하려면 깊은 복사 생성자를 만들거나 해야함  

위의 코드에서 "int n = countPass(com);" 부분이 복사 생성자가 호출되는 코드 부분이다. 

 

 

3) 복사 생성자를 제거하고도 실행오류가 없게 하려면 참조 연산자를 사용해주면 된다.

#include <iostream>
#include <string>
using namespace std;
class Dept
{
int size;
int* scores;
public:
Dept(int size) {
this->size = size;
scores = new int[size];
}
//Dept(const Dept& dept) {
// this->size = dept.size;
// scores = new int[dept.size];
// for (int i = 0; i < dept.size; i++)
// this->scores[i] = dept.scores[i];
//}
~Dept() { delete[]scores; };
int getSize() { return size; }
void read() {
cout << "10개 점수 입력 >>";
for (int i = 0; i < size; i++)
{
cin >> scores[i];
}
}
bool isOver60(int index) {
if (scores[index] > 60)
return true;
else
return false;
}
};
int countPass(Dept &dept) {
int count = 0;
for (int i = 0; i < dept.getSize(); i++) {
if (dept.isOver60(i)) count++;
}
return count;
}
int main() {
Dept com(10);
com.read();
int n = countPass(com);
cout << "60점 이상은 " << n << "명";
}

 

<code>

#include <iostream>
#include <string>
using namespace std;
class Book
{
string title;
int price=0;
public:
Book(string title, int price) {
this->title = title;
this->price = price;
}
~Book() {};
void set(string title, int price) {
this->title = title;
this->price = price;
}
void show() { cout << title << ' ' << price << "원" << endl; }
//Book& book(const Book&b) {
// this->title = b.title;
// this->price = b.price;
//}
};
int main() {
Book cpp("명품 C++", 10000);
Book java = cpp;
java.set("명품자바", 12000);
cpp.show();
java.show();
}
view raw cp5_11.cpp hosted with ❤ by GitHub

const char*를 쓸때보다 확실히 string 클래스를 사용하니 좀더 수월하게 코드를 짤 수 있었음

 

 

 

 

<code>

 

 

#include <iostream>
#include <string>
using namespace std;
class Buffer
{
string text;
public:
Buffer(string text) { this->text = text; };
~Buffer() {};
void add(string next) { text += next; }
void print() { cout << text << endl; }
};
Buffer& append( Buffer& buf, string s) {
buf.add(s);
return buf;
}
int main() {
Buffer buf("Hello");
Buffer& temp = append(buf, "Guys");
temp.print();
buf.print();
}
view raw cp5_10.cpp hosted with ❤ by GitHub

+ Recent posts