<code>

 

#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
string name;
public:
void setCircle(string name, int radius)
{
this->name = name;
this->radius = radius;
}
double getArea() { return 3.14 * radius * radius; };
string getName() { return name; };
};
class CircleManager {
Circle* p;
int size;
public:
CircleManager(int size)
{
p = new Circle[size];
this->size = size;
};
~CircleManager() { delete[]p; }
void searchByName()
{
string n;
cin >> n;
for (int i = 0; i < size; i++)
{
if (p[i].getName() == n)
cout << n << "의 면적은 " << p[i].getArea() << endl;
}
};
void searchByArea()
{
int a;
cin >> a;
cout << a << "보다 큰 원을 검색합니다." << endl;
for (int i = 0; i < size; i++)
{
if (p[i].getArea() > a)
{
cout << p[i].getName() << "의 면적은 " << p[i].getArea()<<",";
}
}
}
void setcircle()
{
string name;
int radius;
for (int i = 0; i < size; i++)
{
cout << "원 " << i + 1 << "의 이름과 반지름 >>";
cin >> name >> radius;
p[i].setCircle(name, radius);
}
}
};
int main()
{
int size;
string name;
cout << "원의 개수 >>";
cin >> size;
CircleManager circle(size);
circle.setcircle();
cout << "검색하고자 하는 원의 이름 >>";
circle.searchByName();
cout << "최소 면적을 정수로 입력하세요 >>";
circle.searchByArea();
}
view raw cp4_12.cpp hosted with ❤ by GitHub

 

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
class Container
{
int size; // 재료 양
public:
Container() { size = 10; }; // 재료의 기본 틀은 10개로 함
~Container() {};
void fill() { size = 10; }; // 재료 다시 채워놓기
void consume() { size -= 1; }; // 한개씩 차감 설정
int getSize() { return size; };
};
class CoffeeVendingMachine
{
Container tong[3];
void fill() {
for (int i = 0; i < 3; i++)
tong[i].fill(); // 재료를 다시 채워놓는 함수
cout << "커피 " << tong[0].getSize() << ", ";
cout << "물 " << tong[1].getSize() << ", ";
cout << "설탕 " << tong[2].getSize() << endl; // 재료들이 얼마 있는 지 출력
};
void selectEspresso()
{
for (int i = 0; i < 2; i++) {
tong[i].consume();
if (tong[i].getSize() <= 0) {
cout << "원료가 부족합니다." << endl; // 재료 중 한개라도 0보다 작거나 같을때 다시 처음 부터 시작하도록 설정
run();
}
}
cout << "에스프레소 드세요" << endl; // 재료가 충분할 경우엔 정상 출력
};
void selectAmericano()
{
for (int i = 0; i < 3; i++) {
if (tong[i].getSize() <= 0) {
cout << "원료가 부족합니다." << endl;
run();
}
}
tong[0].consume();
tong[1].consume();
tong[1].consume();
cout << "아메리카노 드세요" << endl;
};
void selectSugarCoffee()
{
for (int i = 0; i < 3; i++) {
if (tong[i].getSize() <= 0) {
cout << "원료가 부족합니다." << endl;
run();
}
}
tong[0].consume();
tong[1].consume();
tong[1].consume();
tong[2].consume();
cout << "설탕커피 드세요" << endl;
}
void show() {
cout << "커피 " << tong[0].getSize() << ", ";
cout << "물 " << tong[1].getSize() << ", ";
cout << "설탕 " << tong[2].getSize() << endl;
}
public:
void run()
{
cout << "***** 커피자판기를 작동합니다. *****" << endl;
int n;
while (true)
{
cout << "메뉴를 눌러주세요(1:에스프레소, 2:아메리카노, 3:설탕커피, 4:잔량보기, 5:채우기)>>";
cin >> n;
switch (n)
{
case 1:
selectEspresso();
break;
case 2:
selectAmericano();
break;
case 3:
selectSugarCoffee();
break;
case 4:
show();
break;
case 5:
fill();
break;
default:
break; // switch case를 사용하여 커피머신 구현
}
}
}
};
int main()
{
CoffeeVendingMachine c;
c.run();
}
view raw cp4_11.cpp hosted with ❤ by GitHub

 

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
class Person
{
string name; // person 클래스의 이름을 선언
public:
Person() {};
Person(string name) { this->name = name; }; //선언자의 문자열을 private name에 대입
~Person() {};
string getName() { return name; }
void setName(string name) { this->name = name; } // 입력된 문자열을 name 변수에 대입
};
class Family
{
Person* p; // Person 클래스의 포인터 선언
int size; // 가족 구성원 숫자
string name; // 가족 구성원이름
public:
Family(string name, int size)
{
p = new Person[size]; // Person 클래스 배열을 size만큼 동적 생성
this->size = size; //가족 구성원 숫자 대입
this->name = name;
}
~Family() { delete[]p; }; // 모든 함수가 끝날때 동적할당 해제해줌
void show();
string setName(int size, string name); //입력받은 숫자와 이름 대입
};
void Family::show()
{
cout << name << "가족은 다음과 같이 " << size << "명 입니다." << endl;
for (int i = 0; i < size; i++)
cout << p[i].getName() << '\t'; // person 포인터로 이름 출력
}
string Family::setName(int size, string name)
{
p[size].setName(name); // person 포인터로 이름 받아서 입력하기
return string();
}
int main()
{
Family* simpson = new Family("Simpson", 3);
simpson->setName(0, "Mr. simpson");
simpson->setName(1, "Mrs. simpson");
simpson->setName(2, "Bart simpson");
simpson->show();
delete simpson;
}
view raw cp4_10.cpp hosted with ❤ by GitHub

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
class Person
{
string name;
string tel;
public:
Person() { name = " "; tel = "000-0000-0000"; }
~Person() {};
string getName() { return name; }
string getTel() { return tel; }
void set(string name, string tel);
};
void Person::set(string name, string tel)
{
this->name = name; this->tel = tel;
}
int main()
{
string n, t;
cout << "이름과 전화 번호를 입력해 주세요" << endl;
Person person[3];
for (int i = 0; i < 3; i++)
{
cout << "사람" << i + 1 << ">>";
cin >> n >> t;
person[i].set(n, t);
}
cout << "모든 사람의 이름은";
for (int i = 0; i < 3; i++)
cout << person[i].getName()<< " ";
cout << endl<<"전화번호를 검색합니다. 이름을 입력하세요 >>";
cin >> n;
cout << "전화번호는 ";
for (int i = 0; i < 3; i++) {
if (n == person[i].getName()) cout << person[i].getTel();
}
}
view raw cp4_9.cpp hosted with ❤ by GitHub

 

클래스 또한 동적할당해서 원하는 만큼 객체 클래스를 생성할 수 있다.

7번 문제에서 조금 심화되었지만, 동적할당으로 객체를 어떻게 생성해야하는 지를 좀 익힐 수 있었던 문제였다.

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
class Circle
{
int radius = 0;
public:
Circle() {};
~Circle() {};
void setRadius(int x);
double getArea();
};
void Circle::setRadius(int x)
{
radius = x;
}
double Circle::getArea()
{
return 3.14 * radius * radius;
}
int main()
{
int cn;
cout << "원의 개수 >>";
cin >> cn;
Circle* p = new Circle[cn];
int n, cnt=0;
for (int i = 0; i < cn; i++)
{
cout << "원"<<i+1<<"의 반지름 >>";
cin >> n;
p[i].setRadius(n);
if (p[i].getArea() > 100) cnt++;
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다";
}
view raw cp4_8.cpp hosted with ❤ by GitHub

 

<출력화면>

 

 

 

책에 이전에 계속 나오던 예제를 사용하면 쉽게 풀 수 있다!!

 

<code></p

#include <iostream>
#include <string>
using namespace std;
class Circle
{
int radius = 0;
public:
Circle() {};
~Circle() {};
void setRadius(int x);
double getArea();
};
void Circle::setRadius(int x)
{
radius = x;
}
double Circle::getArea()
{
return 3.14 * radius * radius;
}
int main()
{
Circle c1;
Circle c2;
Circle c3;
int n, cnt=0;
cout << "원 1의 반지름 >>";
cin >> n;
c1.setRadius(n);
cout << "원 2의 반지름 >>";
cin >> n;
c2.setRadius(n);
cout << "원 3의 반지름 >>";
cin >> n;
c3.setRadius(n);
if (c1.getArea() > 100) cnt++;
if (c2.getArea() > 100) cnt++;
if (c3.getArea() > 100) cnt++;
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다";
}
view raw cp4_7 hosted with ❤ by GitHub

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
for (;;)
{
cout << ">>";
getline(cin, str);
for (int i = str.size() - 1; i >= 0; --i)
{
cout << str[i];
}
cout << endl;
}
}
view raw cp4_6 hosted with ❤ by GitHub

 

string 함수는 문자열 내도 배열처럼 접근이 가능함.

for 문을 통해서 배열의 마지막부터 출력하면 문자열을 거꾸로 출력이 가능하다.

 

<code>

 

#include <iostream>
#include <string>
using namespace std;
class Sample
{
int* p;
int size;
public:
Sample(int n) { size = n; p = new int[n]; };
void read();
void write();
int big();
~Sample() {};
};
void Sample::read()
{
for (int i = 0; i < size; i++)
cin >> p[i];
}
void Sample::write()
{
for (int i = 0; i < size; i++)
cout << p[i]<<" ";
cout << endl;
}
int Sample::big()
{
int big = 0;
for (int i = 0; i < size; i++)
{
if (p[i] > big) big = p[i];
}
return big;
}
int main() {
Sample s(10);
s.read();
s.write();
cout << "가장 큰 수는 " << s.big() << endl;
}
view raw cp4_4 hosted with ❤ by GitHub

+ Recent posts