본문 바로가기
대외활동/시스템프로그래밍

C++ 객체지향 프로그래밍

by hoshi03 2024. 7. 25.

• 클래스와 객체

#include <iostream>

using namespace std;

class Rectangle {
public :
	int width;
	int height;
	Rectangle(int width, int height);
	Rectangle();

	int getArea() {
		return width * height;
	}
};

Rectangle::Rectangle() {
	width = 5;
	height = 10;
	cout << "width : " << width << " height : " << height << '\n';
}

Rectangle::Rectangle(int width, int hegiht) {
	this->width =width;
	this->height = hegiht;
	cout << "width : " << width << " height : " << height << '\n';
}

int main() {
	Rectangle rect(5,10);
	rect.width = 5;
	rect.height = 10;
	cout << "사각형의 넓이는 " << rect.getArea() << '\n';
}

 

• 생성자 중복 테스트

 

#include <iostream>

using namespace std;

class Rectangle {
public :
	int width = 1;
	int height = 1;
	Rectangle(int width, int height);
	Rectangle();
	Rectangle(int widthAndHeight);

	int getArea() {
		return width * height;
	}

	int isSquare() {
		if (width > 0 && height > 0 && (width == height)) {
			return 1;
		}

		return 0;
	}
};

Rectangle::Rectangle() {
	cout << "width : " << width << " height : " << height << '\n';
}

Rectangle::Rectangle(int width, int hegiht) {
	this->width =width;
	this->height = hegiht;
	cout << "width : " << width << " height : " << height << '\n';
}

Rectangle::Rectangle(int widthAndHeight) {
	width = widthAndHeight;
	height = widthAndHeight;
	cout << "width : " << width << " height : " << height << '\n';
}

int main() {
	Rectangle rect1;
	Rectangle rect2(3, 5);
	Rectangle rect3(3);

	if (rect1.isSquare()) cout << "rect1은 정사각형이다" << '\n';
	if (rect2.isSquare()) cout << "rect2은 정사각형이다" << '\n';
	if (rect3.isSquare()) cout << "rect3은 정사각형이다" << '\n';
}

 

• 인라인 함수

 

• 구조체

 

c++의 구조체는 접근 지정자 말고는 전부 클래스와 동일하다

 

• 동적 할당

 

포인터 자료형에 new 키워드, 자료형을 명시해서 할당한다, 사용후엔 delete 명령어를 사용해서 동적 할당한 메모리 해제

int main() {
	int* p = new int;
	*p = 5;
	int n = *p;
	cout << *p << " " << n << '\n';
	delete p;
}

 

배열의 동적 할당

delete[] 로 메모리를 해제하고 , 해제한 포인터의 주소는 접근 불가능해진다

int main() {

	Circle* circles = new Circle[5];

	for (int i = 0; i < 5; i++)
	{
		circles[i] = Circle(i+1);
		cout << circles[i].getArea() << '\n';
	}

	delete[] circles;

}

 

• 동적할당 연습으로 stack 자료구조 구현

#include <iostream>
#include <bits/stdc++.h>
#include <random>
#include <string>
using namespace std;

class Stack {
	int size;
	int* arr;
	int top = -1;

public :
	Stack();
	Stack(int s);
	~Stack();

	bool pop(int *x);
	bool push(int n);
	bool isEmpty();
	int getSize();
	int setSize(int n); 
	int getTop();
	int getData();

	void printAll();
};

Stack::Stack() {
	cout << "기본 스택 생성" << '\n';
	size = 10;
	arr = new int[size];
}

Stack::Stack(int s) {
	cout << "크기가 " << s << "인 스택 생성" << '\n';
	size = s;
	arr = new int[size];
}

Stack::~Stack() {
	cout << "스택 소멸" << '\n';
	delete[] arr;
}

bool Stack::pop(int *x) {
	if (top == -1) return false;
	*x = arr[top];
	top--;
	return true;
}

bool Stack::push(int n) {
	if (getTop() >= getSize()) setSize(size * 2);
	arr[++top] = n;
	return true;
}

int Stack::getSize() {
	return size;
}

int Stack::setSize(int n) {

	int* tmp = new int[n];

	if (n < getSize()) {
		copy(arr, arr + n, tmp);
		if (getTop() >= n) top = n - 1;
	}
	else copy(arr, arr + getSize(), tmp);

	delete[] arr;
	arr = tmp;
	size = n;

	return size;
}

int Stack::getTop() {
	return top;
}

int Stack::getData() {
	return arr[top];
}

bool Stack::isEmpty() {
	if (top == -1) return true;
	return false;
}

void Stack::printAll() { //일괄출력
	while (!isEmpty())
	{
		int tmp = 0;
		pop(&tmp);
		cout << tmp << " ";
	}
	cout << '\n';
}

int main() {
	Stack* st = new Stack(7);

	for (int i = 0; i < st->getSize(); i++) st->push(i + 1); // 7까지
	st->printAll();
	st->setSize(20);
	for (int i = 0; i < st->getSize(); i++) st->push(i + 1); // 20까지 넣기
	st->printAll();
	st->setSize(3);
	for (int i = 0; i < st->getSize(); i++) st->push(i + 1);
	st->printAll();


	delete st;
	return 0;
}

 

 

 

#include <iostream>
#include <bits/stdc++.h>
#include <random>
#include <string>
using namespace std;

class Person {
	string name;
public:
	Person(string name);
	Person();
	string getName();
	string setName(string name);
	
};

class Family {
	Person* p;
	int size;
public:
	Family(string name, int size);
	~Family();

	void setName(int idx, string name);

	void show();
};

string Person ::setName(string name) { return this->name = name; }
string Person ::getName() { return name; }
Person :: Person(){}
Person :: Person(string name) { this->name = name; }
Family::Family(string name, int size) { this->size = size; p = new Person[size]; }
Family :: ~Family() { delete[] p; "일가족 소멸"; }
void Family :: show() {
	cout << "심슨 가족은 다음과 같이 " << size << "명 입니다." << '\n';
	for (int i = 0; i < size; i++)cout << p[i].getName() << '\t';
}


void Family :: setName(int idx, string name) {
	p[idx].setName(name);
}

int main() {
	Family* simpson = new Family("Simson", 3);
	simpson->setName(0, "Mr Simpson");
	simpson->setName(1, "Mrs. Simpson");
	simpson->setName(2, "Bart Simpson");
	simpson->show();
	delete simpson;
}

 

• 복사 생성자

얕은 복사 생성자를 생성하면 

Stack::Stack(const Stack& st) {
	this->size = st.size;
	this->top = st.top;
	this->arr = new int[this->size];
	memcpy(this->arr, st.arr, sizeof(int) * (this->top + 1));
	copy(st.arr, st.arr + st.top + 1, this->arr);
}


-- 사용
Stack st2(*st);
st2.printAll();

 

복사 생성자 에러나는코드

#include <iostream>
#include <bits/stdc++.h>
#include <random>
#include <string>
using namespace std;

class Stack {
	int size;
	int* arr;
	int top = -1;

public:
	Stack();
	Stack(int s);
	Stack(const Stack& st);
	~Stack();

	bool pop(int& x);
	bool push(int n);
	bool isEmpty();
	int getSize();
	int setSize(int n);
	int getTop();
	int getData();
	int getIndexData();

	void printAll();
};

Stack::Stack() {
	cout << "기본 스택 생성" << '\n';
	size = 10;
	arr = new int[size];
}

Stack::Stack(int s) {
	cout << "크기가 " << s << "인 스택 생성" << '\n';
	size = s;
	arr = new int[size];
}

Stack::~Stack() {
	delete[] arr;
	cout << "스택 소멸" << '\n';
}

Stack::Stack(const Stack& st) {
	cout << "복사 생성자 호출" << '\n';
	this->size = st.size;
	this->top = st.top;
	this->arr = new int[this->size];
	memcpy(this->arr, st.arr, sizeof(int) * (this->top + 1));
	/*copy(st.arr, st.arr + st.top + 1, this->arr);*/
}

bool Stack::pop(int& x) {
	if (top == -1) return false;
	x = arr[top];
	top--;
	return true;
}



bool Stack::push(int n) {
	if (getTop() >= getSize()) setSize(size * 2);
	arr[++top] = n;
	return true;
}

int Stack::getSize() {
	return size;
}

int Stack::setSize(int n) {

	int* tmp = new int[n];

	if (n < getSize()) {
		copy(arr, arr + n, tmp);
		if (getTop() >= n) top = n - 1;
	}
	else copy(arr, arr + getSize(), tmp);

	delete[] arr;
	arr = tmp;
	size = n;

	return size;
}

int Stack::getTop() {
	return top;
}

int Stack::getData() {
	return arr[top];
}


int Stack::getIndexData() {
	
	for (int i = getTop(); i >= 0; i--) cout << arr[i] << ' ';
	cout << '\n';
	return 0;
}

bool Stack::isEmpty() {
	if (top == -1) return true;
	return false;
}

void Stack::printAll() { //일괄출력
	while (!isEmpty())
	{
		int tmp;
		pop(tmp);
		cout << tmp << " ";
	}
	cout << '\n';
}

int main() {
	Stack st = Stack(0);

	for (int i = 0; i < 20; i++) st.push(i + 1);


	/*Stack st2(st);
	st2.push(124);
	st2.printAll();*/
	st.printAll();

	return 0;
}