<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

 

 

<code>

 

 

#include <iostream>
#include <string>
using namespace std;
class Accumulator
{
int value;
public:
Accumulator(int value) { this->value = value; }
Accumulator&add(int n) { //복사 참조 함수?
value += n;
return *this; //값을 나 자신에게 참조 반환
}
~Accumulator() {};
int get() { return value; }
};
int main() {
Accumulator acc(10);
acc.add(5).add(6).add(7);
cout << acc.get();
}
view raw cp5_9.cpp hosted with ❤ by GitHub

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
class MyIntStack
{
int *p; //최대 10개의 정수 저장
int size;
int tos; // 스택의 꼭대기를 가리키는 인덱스
public:
MyIntStack() {
tos = 0;
size = 1;
p = new int[size];
};
MyIntStack(int size)
{
tos = 0;
this->size = size;
p = new int[size];
};
MyIntStack(const MyIntStack& s) { //깊은 복사 생성자 만들기
this->tos = s.tos;
this->size = s.size;
this->p = new int[s.size];
for (int i = 0; i < s.tos; i++) {
this->p[i] = s.p[i];
}
}
~MyIntStack() {
if(p)
delete[]p;
}
bool push(int n); //정수 n푸시. 꽉차있으면 false, 아니면 true 리턴
bool pop(int& n); //팝하여 n에 저장. 스택이 비어있으면 false, 아니면 true 리턴
};
bool MyIntStack::push(int n)
{
p[tos] = n; // 들어온 값을 p배열에 대입하기
if (tos >= 10) { //스택이 꽉 차지 않도록 값을 대입;//스택의 꼭대기 값을 tos에 저장
return false;
}
else
{
tos++;
return true;
}
}
bool MyIntStack::pop(int& n)
{
n = p[tos-1];
tos--;
if (tos < 0) return false;
else return true;
}
int main() {
MyIntStack a(10);
a.push(10);
a.push(20);
MyIntStack b = a;
b.push(30);
int n;
a.pop(n);
cout << "스택 a에서 팝한 값 : " << n << endl;
b.pop(n);
cout << "스택 b에서 팝한 값 : " << n << endl;
}
view raw cp5_8.cpp hosted with ❤ by GitHub

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
class MyIntStack
{
int p[10]; //최대 10개의 정수 저장
int tos; // 스택의 꼭대기를 가리키는 인덱스
public:
MyIntStack() {};
bool push(int n); //정수 n푸시. 꽉차있으면 false, 아니면 true 리턴
bool pop(int& n); //팝하여 n에 저장. 스택이 비어있으면 false, 아니면 true 리턴
};
bool MyIntStack::push(int n)
{
p[n] = n; // 들어온 값을 p배열에 대입하기
if (n >= 10) { //스택이 꽉 차지 않도록 값을 대입
tos = n;//스택의 꼭대기 값을 tos에 저장
return false;
}
else return true;
}
bool MyIntStack::pop(int& n)
{
n = tos;
tos--;
if (tos<0) return false;
else return true;
}
int main() {
MyIntStack a;
for (int i = 0; i < 11; i++)
{
if (a.push(i)) cout << i << ' ';
else cout << endl << i + 1 << " 번째 stack full" << endl;
}
int n;
for (int i = 0; i < 11; i++) {
if (a.pop(n)) cout << n << ' ';
else cout << endl << i + 1 << " 번째 stack empty";
}
cout << endl;
}
view raw cp5_7.cpp hosted with ❤ by GitHub

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
char& find(char a[], char c, bool& success)
{
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) // 배열의 길이만큼 for문 돌도록 설정
{
if (a[i] == c)
{
success = true; // 원하는 char를 찾았을 때 true로 변경
}
return a[i]; // 찾은 배열 값 반환
}
}
int main() {
char s[] = "Mike";
bool b = false;
char& loc = find(s, 'M', b); // 포인터 변수에 찾은 배열 값을 대입
if (b == false)
{
cout << "M을 발견할 수 없다" << endl;
return 0;
}
loc = 'm';
cout << s << endl;
}
view raw cp5_6.cpp hosted with ❤ by GitHub

+ Recent posts