2021-2학기/C++
C++ programming 8장 실습문제
애플쩀
2021. 11. 18. 19:44
1
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
Circle(int radius = 0) { this->radius = radius; }
int getRadius() { return radius; }
void setRadius(int radius) { this->radius = radius; }
double getArea() { return 3.14 * radius * radius; }
};
class NamedCircle : public Circle {
string name;
public:
NamedCircle(int radius=0, string name=" ") : Circle(radius) {
this->name = name;
}
void show() {
cout << "반지름이 " << getRadius() << "인 와플";
}
};
int main() {
NamedCircle waffle(3, "waffle");
waffle.show();
return 0;
return 0;
}
2
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
Circle(int radius = 0) { this->radius = radius; }
int getRadius() { return radius; }
void setRadius(int radius) { this->radius = radius; }
double getArea() { return 3.14 * radius * radius; }
};
class NamedCircle : public Circle {
string name;
public:
NamedCircle(int radius=0, string name=" ") : Circle(radius) {
this->name = name;
}
void setName(string name) {
this->name = name;
}
string getName() {
return name;
}
void show() {
cout << "반지름이 " << getRadius() << "인 와플";
}
};
int main() {
NamedCircle pizza[5];
cout << "5개의 정수반지름과 원의 이름을 입력하세요"<<endl;
int radius;
string name;
for (int i = 0; i < 5; i++) {
cout << i+1<<">>";
cin >> radius >> name;
pizza[i].setRadius(radius);
pizza[i].setName(name);
cout << endl;
}
int maxArea = 0, maxind=0;
for (int i = 0; i < 5; i++) {
if (pizza[i].getArea() > maxArea) {
maxArea = pizza[i].getArea();
maxind = i;
}
}
cout << "면적이 가장 큰 피자는 " << pizza[maxind].getName() << "입니다.";
return 0;
}
3
#include <iostream>
#include <string>
using namespace std;
class Point {
int x, y;
public:
Point(int x, int y) { this->x = x; this->y = y; }
int getX() { return x; }
int getY() { return y; }
protected:
void move(int x, int y) { this->x = x; this->y = y; }
};
class ColorPoint : public Point {
string c;
public:
ColorPoint(int x, int y, string c) : Point(x, y) {
this->c = c;
}
void setPoint(int x, int y) {
move(x, y);
}
void setColor(string c) {
this->c = c;
}
void show() {
cout << c << "색으로" << "(" << getX() << "," << getY() << ") 위치한 점입니다." << endl;
}
};
int main() {
zeroPoint.show();
ColorPoint cp(5, 5, "RED");
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
return 0;
}
4
#include <iostream>
#include <string>
using namespace std;
class Point {
int x, y;
public:
Point(int x, int y) { this->x = x; this->y = y; }
int getX() { return x; }
int getY() { return y; }
protected:
void move(int x, int y) { this->x = x; this->y = y; }
};
class ColorPoint : public Point {
string c;
public:
ColorPoint(int x, int y, string c) : Point(x, y) {
this->c = c;
}
void setPoint(int x, int y) {
move(x, y);
}
void setColor(string c) {
this->c = c;
}
void show() {
cout << c << "색으로" << "(" << getX() << "," << getY() << ") 위치한 점입니다.";
}
};
int main() {
ColorPoint cp(5, 5, "RED");
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
return 0;
}#include <iostream>
#include <string>
using namespace std;
class Point {
int x, y;
public:
Point(int x, int y) { this->x = x; this->y = y; }
int getX() { return x; }
int getY() { return y; }
protected:
void move(int x, int y) { this->x = x; this->y = y; }
};
class ColorPoint : public Point {
string c;
public:
ColorPoint() : Point(0, 0) { c = "Black"; }
ColorPoint(int x, int y, string c) : Point(x, y) {
this->c = c;
}
void setPoint(int x, int y) {
move(x, y);
}
void setColor(string c) {
this->c = c;
}
void show() {
cout << c << "색으로" << "(" << getX() << "," << getY() << ") 위치한 점입니다." << endl;
}
};
int main() {
ColorPoint zeroPoint;
zeroPoint.show();
ColorPoint cp(5, 5, "RED");
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
return 0;
}ss
5
#include <iostream>
#include <string>
using namespace std;
class BaseArray {
private:
int capacity;
int* mem;
protected:
BaseArray(int capacity = 100) {
this->capacity = capacity; mem = new int[capacity];
}
~BaseArray() { delete[] mem; }
void put(int index, int val) { mem[index] = val; }
int get(int index) { return mem[index]; }
int getCapacity() { return capacity; }
};
class MyQueue : public BaseArray{
int start;
int end;
public:
MyQueue(int capacity) : BaseArray(capacity) { start = 0; end = 0; }
int capacity() { return getCapacity(); }
int length() { return end - start; }
void enqueue(int n) { put(end, n); end++; }
int dequeue() { return get(start++); }
};
int main() {
MyQueue mQ(100);
int n;
cout << "큐에 삽입할 5개의 정수를 입력하라>> ";
for (int i = 0; i < 5; i++) {
cin >> n;
mQ.enqueue(n);
}
cout << "큐의 용량:" << mQ.capacity() << ", 큐의 크기:" << mQ.length() << endl;
cout << "큐의 원소를 순서대로 제거하여 출력한다>> ";
while (mQ.length() != 0) {
cout << mQ.dequeue() << ' ';
}
cout<<endl<<"큐의 현재 크기 : " << mQ.length() << endl;
return 0;
}
6
#include <iostream>
#include <string>
using namespace std;
class BaseArray {
private:
int capacity;
int* mem;
protected:
BaseArray(int capacity = 100) {
this->capacity = capacity; mem = new int[capacity];
}
~BaseArray() { delete[] mem; }
void put(int index, int val) { mem[index] = val; }
int get(int index) { return mem[index]; }
int getCapacity() { return capacity; }
};
class MyStack : public BaseArray {
int top;
public:
MyStack(int capacity) : BaseArray(capacity) { top = -1; }
void push(int n) { put(++top, n); }
int pop(){ return get(top--); }
int length() { return top + 1; }
int capacity(){ return getCapacity(); }
};
int main() {
MyStack mStack(100);
int n;
cout << "스택에 삽입할 5개의 정수를 입력하라>> ";
for (int i = 0; i < 5; i++) {
cin >> n;
mStack.push(n);
}
cout << "스택의 용량:" << mStack.capacity() << ", 스택의 크기:" << mStack.length() << endl;
cout << "스택의 원소를 순서대로 제거하여 출력한다>> ";
while (mStack.length() != 0) {
cout << mStack.pop() << ' ';
}
cout << endl << "스택의 현재 크기 : " << mStack.length() << endl;
return 0;
return 0;
}
7