5-1
#include <iostream>
#include <string>
using namespace std;
class Circle
{
int radius;
public:
Circle() { this->radius = 1; }
Circle(int r) { this->radius = r; }
void show() { cout << "반지름: " << radius << endl; }
};
int main()
{
Circle a(1), b(2);
a.show();
b.show();
swap(a,b);
a.show();
b.show();
}
void swap(Circle& a, Circle& b)
{
Circle& temp = a; //Circle temp=a;라고 해도 됨.
a = b;
b = temp;
}
5-2
#include <iostream>
#include <string>
using namespace std;
void half(double& m)
{
m /= 2;
}
int main()
{
double n = 20;
half(n);
cout << n;
}
5-3
#include <iostream>
#include <string>
using namespace std;
void combine(string a, string b, string& c)
{
c = a + " " + b;
}
int main()
{
string text1("I love you"), text2("very much");
string text3;
combine(text1, text2, text3);
cout << text3;
}
5-4
#include <iostream>
#include <string>
using namespace std;
bool bigger(int a, int b, int& big)
{
if (a == b)
return true;
if (a > b)
big = a;
else
big = b;
return false;
}
int main()
{
bool b;
cout << "정수 2개를 입력하시오:";
int n, m, big;
cin >> n >> m;
b=bigger(n, m, big);
if (b)
cout << "same" << endl;
else
cout << "더 큰 수는 " << big << endl;
}
5-5
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
Circle(int r) { radius = r; }
int getRadius() { return radius; }
void setRadius(int r) { radius = r; }
void show() { cout << "반지름이 " << radius << "인 원" << endl; }
};
void increaseBy(Circle& a, Circle b)
{
int r = a.getRadius() + b.getRadius();
a.setRadius(r);
}
int main()
{
Circle x(10), y(5);
increaseBy(x, y);
x.show();
}
5-6
#include <iostream>
#include <string>
using namespace std;
char& find(char a[], char c, bool& success)
{
for (int i = 0; i <strlen(a); i++)
{
if (a[i] == c)
{
success = true;
return a[i];
}
else
{
success = false;
}
}
}
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 <
5-7
#include <iostream>
#include <string>
using namespace std;
class MyIntStack
{
int p[10];
int tos;
public:
MyIntStack();
bool push(int n);
bool pop(int& n);
};
MyIntStack::MyIntStack()
{
tos = 0;
}
bool MyIntStack::push(int n)
{
if (tos == 10)
return false;
p[tos] = n;
tos++;
return true;
}
bool MyIntStack::pop(int& n)
{
if (tos==0)
return false;
tos--;
n = p[tos];
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;
}
5-8
#include <iostream>
#include <string>
using namespace std;
class MyIntStack {
int* p;
int size;
int tos;
public:
MyIntStack() { size = 0; tos = 0; }
MyIntStack(int size){ this->size = size;this->p = new int[size];}
MyIntStack(const MyIntStack& s) {
int len = s.size;
this->p = new int[len];
this->size = len;
this->tos = s.tos;
for (int i = 0; i <= tos; i++)
{
this->p[i] = s.p[i];
}
}
~MyIntStack() { delete [] p; }
bool push(int n);
bool pop(int& n);
};
bool MyIntStack::push(int n)
{
if (tos == size)
return false;
p[tos] = n;
tos++;
return true;
}
bool MyIntStack::pop(int& n)
{
if (tos == 0)
return false;
tos--;
n = p[tos];
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;
}
5-9
#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; }
int get(){ return value; }
};
int main()
{
Accumulator acc(10);
acc.add(5).add(6).add(7);
cout << acc.get();
}
5-10
#include <iostream>
#include <string>
using namespace std;
class Buffer {
string text;
public:
Buffer(string text) { this->text = text; }
void add(string next) { text += next; }
void print() { cout << text << endl; }
};
Buffer& append(Buffer& b, string s)
{
b.add(s);
return b;
};
int main()
{
Buffer buf("Hello");
Buffer& temp = append(buf, "Guys");
temp.print();
buf.print();
}
5-11
#include <iostream>
#include <string>
using namespace std;
class Book {
char* title;
int price;
public:
Book(const char* title, int price);
Book(Book& b);
~Book();
void set(const char* title, int price);
void show() { cout << title << ' ' << price << "원" << endl; }
};
Book::Book(const char* title, int price) {
int len = strlen(title);
this->title = new char[len + 1];
strcpy(this->title, title);
this->price = price;
}
Book::~Book() {
if (title)
delete[] title;
}
void Book::set(const char* title, int price)
{
if (this->title)
delete[]this->title;
int len = strlen(title);
this->title = new char[len + 1];
strcpy(this->title, title);
this->price = price;
}
/*
Book::Book(Book&b)
{
title=b.title;
price=b.price;
*/
Book::Book(Book& b) {
int len = strlen(b.title);
title = new char[len + 1];
strcpy(title, b.title);
price = b.price;
}
int main() {
Book cpp("명품C++", 10000);
Book java = cpp;
java.set("명품자바", 12000);
cpp.show();
java.show();
}
Comments (0)