์ƒˆ์†Œ์‹

Languages/C++

[C++] ํด๋ž˜์Šค์™€ ๊ฐ์ฒด | Class | C++

  • -
๋ฐ˜์‘ํ˜•

์„ธ์ƒ์˜ ๋ชจ๋“  ๊ฒƒ์€ ๊ฐ์ฒด์ด๋‹ค.

 

์บก์Šํ™”(encapsulation)
    • ๊ฐ์ฒด์˜ ๋ณธ์งˆ์ ์ธ ํŠน์„ฑ
    • ๊ฐ์ฒด๋ฅผ ์บก์Š๋กœ ์‹ธ์„œ ๊ทธ ๋‚ด๋ถ€๋ฅผ ๋ณดํ˜ธํ•˜๊ณ  ๋ณผ ์ˆ˜ ์—†๊ฒŒ ํ•จ
      • ์บก์Š์— ๋“  ์•ฝ์€ ์–ด๋–ค ์ƒ‰์ธ์ง€ ์–ด๋–ค ์„ฑ๋ถ„์ธ์ง€ ๋ณด์ด์ง€ ์•Š๊ณ , ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์•ˆ์ „
    • ์บก์Šํ™” ์‚ฌ๋ก€

  • ์บก์Šํ™”์˜ ๋ชฉ์ 
    • ๊ฐ์ฒด ๋‚ด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ณด์•ˆ, ๋ณดํ˜ธ, ์™ธ๋ถ€ ์ ‘๊ทผ ์ œํ•œ

 

๏‚จ ํด๋ž˜์Šค
  • ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๊ธฐ ์œ„ํ•ด ์ •์˜๋œ ์„ค๊ณ„๋„, ํ‹€
  • ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด๊ฐ€ ์•„๋‹˜. ์‹ค์ฒด๋„ ์•„๋‹˜
  • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์„ ์–ธ

 

๏‚จ ๊ฐ์ฒด
  • ๊ฐ์ฒด๋Š” ์ƒ์„ฑ๋  ๋•Œ ํด๋ž˜์Šค์˜ ๋ชจ์–‘์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ํƒ„์ƒ
  • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋กœ ๊ตฌ์„ฑ
  • ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ, ์‹ค์ฒด(instance)๋ผ๊ณ ๋„ ๋ถ€๋ฆ„
  • ํ•˜๋‚˜์˜ ํด๋ž˜์Šค ํ‹€์—์„œ ์ฐ์–ด๋‚ธ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ์ฒด ์ƒ์„ฑ ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด๋“ค์€ ์ƒํ˜ธ ๋ณ„๋„์˜ ๊ณต๊ฐ„์— ์ƒ์„ฑ

 

 

C++ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ
  • ํด๋ž˜์Šค ์ž‘์„ฑ
    • ๏‚ค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋กœ ๊ตฌ์„ฑ
    • ๏‚ค ํด๋ž˜์Šค ์„ ์–ธ๋ถ€์™€ ํด๋ž˜์Šค ๊ตฌํ˜„๋ถ€๋กœ ๊ตฌ์„ฑ
  • ๏‚จ ํด๋ž˜์Šค ์„ ์–ธ๋ถ€(class declaration)
    • ๏‚ค class ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํด๋ž˜์Šค ์„ ์–ธ
    • ๏‚ค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์„ ์–ธ
      • ๏ฎ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ์„ ์–ธ ๋‚ด์—์„œ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์—†์Œ
      • ๏ฎ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋Š” ์›ํ˜•(prototype) ํ˜•ํƒœ๋กœ ์„ ์–ธ
    • ๏‚ค ๋ฉค๋ฒ„์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ ์ง€์ •
      • ๏ฎ private, public, protected ์ค‘์˜ ํ•˜๋‚˜
      • ๏ฎ ๋””ํดํŠธ๋Š” private
      • ๏ฎ public : ๋‹ค๋ฅธ ๋ชจ๋“  ํด๋ž˜์Šค๋‚˜ ๊ฐ์ฒด์—์„œ ๋ฉค๋ฒ„์˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•จ์„ ํ‘œ์‹œ
  • ๏‚จ ํด๋ž˜์Šค ๊ตฌํ˜„๋ถ€(class implementation)
    • ๏‚ค ํด๋ž˜์Šค์— ์ •์˜๋œ ๋ชจ๋“  ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๊ตฌํ˜„

 

์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ํŠน์ง•

 

์†Œ๋ฉธ์ž ํŠน์ง• 

 

 

์ ‘๊ทผ ์ง€์ •์ž 

 

ํ•จ์ˆ˜ ํ˜ธ์ถœ์— ๋”ฐ๋ฅธ ์‹œ๊ฐ„ ์˜ค๋ฒ„ํ—ค๋“œ

 

์ž‘์€ ํฌ๊ธฐ์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, ํ•จ์ˆ˜ ์‹คํ–‰ ์‹œ๊ฐ„์— ๋น„ํ•ด, ํ˜ธ์ถœ์„ ์œ„ํ•ด ์†Œ์š”๋˜๋Š” ๋ถ€๊ฐ€์ ์ธ ์‹œ๊ฐ„ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ํฌ๋‹ค.

 

 

์˜ˆ์‹œ ์ฝ”๋“œ ์ž‘์„ฑ 1)

#include<iostream>
#include<string>
using namespace std;

class Person{
private:
	string name;
	int age;
public:
	//์…์„ฑ์ž 3๊ฐœ
	//๋ฉ”์†Œ๋“œ ํ•œ๊ฐœ 
	string getName();		
	int getAge();
	Person();
	Person(string a, int b);
	Person(string a);
	Person(int length);
	~Person();
};


Person::Person(){
	name = "ํ•˜์€";
	age = 20;
}
Person::Person(string a, int b) {
	name = a; age = b;
}
Person::Person(string a) {
	name = a; 
	age = 300;
}
string Person::getName(){
	return name;
}
int Person::getAge(){
	return age;
}



Person::~Person(){
	cout<<name <<"์ด ์นœ๊ตฌ ์†Œ๋ฉธ."<<endl;
}
int main(){	

	Person ps, pa("์„ฑ์ง„",23), pt("๋ฏผ์ˆœ");
	
	cout <<"์ด๋ฆ„ : "<<ps.getName()<<",๋‚˜์ด = "<<ps.getAge()<<endl;
	cout <<"์ด๋ฆ„ : "<<pa.getName()<<",๋‚˜์ด = "<<pa.getAge()<<endl;
	cout <<"์ด๋ฆ„ : "<<pt.getName()<<",๋‚˜์ด = "<<pt.getAge()<<endl;
	 
	
}



 

#include <iostream>
using namespace std;
class Circle {
public:
	int radius;
	Circle();
	Circle(int r);
	double getArea();
	~Circle();
};

Circle::Circle(){ }

Circle::Circle(int r){
	radius = r;
}


double Circle::getArea() {
	return 3.14*radius*radius;
}


class Rectangle{
public:
	int width;
	int height;
	double getArea();		
	double getArea3();
	Rectangle();
	Rectangle(int a, int b);
	Rectangle(int length);
	bool isSquare();
	
	~Rectangle();
//	bool isSquare();
//	Rectangle();
//	Rectangle(int a,int b);
};
//Rectangle::Rectangle(){ }
/*
Rectangle::Rectangle(int a,int b){
	x = a; y = b;
	if(x != y){
		return false
	}
	
	return true
}
*/

Rectangle::Rectangle() {
	width = 1;
	height = 1;
}
Rectangle::Rectangle(int a, int b) {
	width = a; height = b;
}
Rectangle::Rectangle(int length) {
	width = height = length;
}
bool Rectangle::isSquare() {
if(width == height) return true;
else return false;}


double Rectangle::getArea(){
	return width * height;
};

double Rectangle::getArea3(){
	return (width*2)+(height*2);
};



Rectangle::~Rectangle(){
	cout<<width * height <<"<์š” ์‚ฌ๊ฐํ˜• ์†Œ๋ฉธ"<<endl;
}

Circle::~Circle(){
	cout<<radius <<"<์š” ๋ฐ˜์ง€๋ฆ„ ์›  ์†Œ๋ฉธ"<<endl;
}


int main() {
	Circle donut,pizza,pizza2(20);
	donut.radius = 1; // donut ๊ฐ์ฒด์˜ ๋ฐ˜์ง€๋ฆ„์„ 1๋กœ ์„ค์ •
	double area = donut.getArea(); // donut ๊ฐ์ฒด์˜ ๋ฉด์  ์•Œ์•„๋‚ด๊ธฐ
	cout << "donut ๋ฉด์ ์€ " << area << endl;
	pizza.radius = 30;
	double area2 = pizza.getArea();
	cout << "pizza ๋ฉด์ ์€ " <<area2 <<endl;
	area = pizza2.getArea();
	
    cout << "๋ฐ˜์ง€๋ฆ„์ด "<<pizza2.radius<<" ์ด๋ฉด, pizza2 ๋ฉด์ ์€ "<<area<<endl;
	
	Circle ball;
	ball.radius = 10; // pizza ๊ฐ์ฒด์˜ ๋ฐ˜์ง€๋ฆ„์„ 30์œผ๋กœ ์„ค์ •
	double area3 = ball.getArea(); // pizza ๊ฐ์ฒด์˜ ๋ฉด์  ์•Œ์•„๋‚ด๊ธฐ
	cout << "ball ๋ฉด์ ์€ " << area3 << endl;
	
	Rectangle rect;
	rect.width = 3;
	rect.height = 5;
	double area5 = rect.getArea();
	double area6 = rect.getArea3();
	cout << "์‚ฌ๊ฐํ˜•์˜ ๋ฉด์ ์€ "<<area5 <<endl;
	cout <<"์‚ฌ๊ฐํ˜•์˜ ๋‘˜๋ ˆ๋Š”" <<area6<<endl;
	
	Rectangle rect1, rect2(3,5), rect3(3);
	if(rect1.isSquare()) cout << "rect1์€ ์ •์‚ฌ๊ฐํ˜•์ด๋‹ค."<<endl;	 
	if(rect2.isSquare()) cout << "rect2์€ ์ •์‚ฌ๊ฐํ˜•์ด๋‹ค."<<endl;
	if(rect3.isSquare()) cout << "rect3์€ ์ •์‚ฌ๊ฐํ˜•์ด๋‹ค."<<endl;
	
	
	return 0;
};

์˜ˆ์‹œ ์ฝ”๋“œ 2)

#include <iostream>
using namespace std;

class Add{
private:
	char giho;
	int num1;
	int num2;
public:
	int add(); //์—ฐ์‚ฐ๊ฒฐ๊ณผ 
	Add(int a, int b,char c);
	//~Add();
};
Add::Add(int a, int b, char c){
	num1 = a, num2= b, giho = c;
} 
int Add::add(){
	return num1 + num2;
}
/*
Add::~Add(){
	cout << giho <<" ๊ฐ€ ์†Œ๋ฉธ๋จ. "<<endl; 
}
*/


class Sub{
private:
	char giho;
	int num1;
	int num2;
public:
	int sub(); //์—ฐ์‚ฐ๊ฒฐ๊ณผ 
	Sub(int a, int b,char c);
//	~Sub();
};
Sub::Sub(int a, int b, char c){
	num1 = a, num2= b, giho = c;
} 
int Sub::sub(){
	return num1 - num2;
}
/*
Sub::~Sub(){
	cout << giho <<" ๊ฐ€ ์†Œ๋ฉธ๋จ. "<<endl; 
}
*/
class Mul{
private:
	char giho;
	int num1;
	int num2;
public:
	int mul(); //์—ฐ์‚ฐ๊ฒฐ๊ณผ 
	Mul(int a, int b,char c);
//	~Mul();
};
Mul::Mul(int a, int b, char c){
	num1 = a, num2= b, giho = c;
} 
int Mul::mul(){
	return num1 * num2;
}
/*
Mul::~Mul(){
	cout << giho <<" ๊ฐ€ ์†Œ๋ฉธ๋จ. "<<endl; 
}
*/


class Div{
private:
	char giho;
	int num1;
	int num2;
public:
	int div(); //์—ฐ์‚ฐ๊ฒฐ๊ณผ 
	Div(int a, int b,char c);
//	~Div();
};
Div::Div(int a, int b, char c){
	num1 = a, num2= b, giho = c;
} 
int Div::div(){
	return num1 / num2;
}
/*
Div::~Div(){
	cout << giho <<" ๊ฐ€ ์†Œ๋ฉธ๋จ. "<<endl; 
}
*/

int main()
{	
	int num1,num2;
	char giho;

//	Add a; Sub b; Mul c; Div d;
	while(1){
		
		cout<<"๋‘ ์ •์ˆ˜์™€ ์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” >> "<<endl;
		cin >> num1; cin >> num2; cin >> giho ;
		if(giho == '+'){
			Add a(num1,num2,giho);
			cout << a.add()<<endl; 
			continue;
		}else if(giho == '-'){
			Sub b(num1,num2,giho);
			cout << b.sub()<<endl;
			continue;	
		}else if(giho == '*'){
			Mul c(num1,num2,giho);
			cout << c.mul()<<endl;
			continue;
		}
		else if(giho == '/'){
			Div d(num1,num2,giho);
			cout << d.div()<<endl;
			continue;
		}else
			cout<<"์ž˜๋ชป๋œ ๊ธฐํ˜ธ ์ž…๋‹ˆ๋‹ค."<<endl;
			break;	
	}
	return 0;
}


 

 

๋”๋ณด๊ธฐ

์„ธ๊ฐœ์˜ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์ธํด๋ฃจ๋“œ ํ•˜๋Š” ๋ฐฉ๋ฒ•

aa.h ํŒŒ์ผ

#include<iostream>
#include<string>
using namespace std;

class Account{
private:
	string name;
	int money;
	int id;
public:
	//์…์„ฑ์ž 3๊ฐœ
	//๋ฉ”์†Œ๋“œ ํ•œ๊ฐœ 
	string getOwner(); //๊ธฐํƒœ 
	int inquiry(); //์ž”์•ก 
	int  deposit(int a); //+  
	int withdraw(int a); // -
	Account();
	Account(string a, int b,int c);
	~Account();
};

 

bb.cpp ํŒŒ์ผ

#include "aa.h"
Account::Account(){
	name = "";
	money = 0;
	id = 0;
}
Account::Account(string a, int b, int c) {
	name = a; id = b; money = c;
}

string Account::getOwner(){
	return name;
}
int Account::deposit(int a){
	money += a;
	return money;
}
int Account::inquiry(){
	return money;
}
int Account::withdraw(int a){
	money -= a;
	return money;
}

Account::~Account(){
	cout<<name <<"์ด ์นœ๊ตฌ ์†Œ๋ฉธ."<<endl;
}

test.cpp (main)

#include "bb.cpp"

int main(){	

	Account a("๊ธฐํƒœ",1,5000), b("ํ•˜์€",2,3000);
	b.deposit(30000) ;
	a.deposit(50000);
	cout << a.getOwner()<<"์˜ ์ž”์•ก์€ "<<a.inquiry()<<endl;//์ž”์•ก์€ 55000 
	cout << b.getOwner()<<"์˜ ์ž”์•ก์€ "<<b.inquiry()<<endl;//์ž”์•ก์€ 33000
	
	a.withdraw(20000); //20000์ถœ๊ธˆ
	b.withdraw(1000); //20000์ถœ๊ธˆ
	cout <<a.getOwner()<<"์˜ ์ž”์•ก์€"<<a.inquiry()<<endl; //์ž”์•ก์€ 35000 

	cout <<b.getOwner()<<"์˜ ์ž”์•ก์€"<<b.inquiry()<<endl; //์ž”์•ก์€ 32000


	return 0;	
}



 

- ์ธ๋ผ์ธ(inline) ํ•จ์ˆ˜


์ž‘์€ ํฌ๊ธฐ์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด, ํ•จ์ˆ˜ ์‹คํ–‰ ์‹œ๊ฐ„์— ๋น„ํ•ด์„œ
ํ˜ธ์ถœ์„ ์œ„ํ•ด ์†Œ์š”๋˜๋Š” ์ถ”๊ฐ€์ ์ธ ์‹œ๊ฐ„ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ์ปค์ง‘๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด ์ธ๋ผ์ธ ํ•จ์ˆ˜๋ผ๋Š” ๊ฐœ๋…์ด ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.

inline ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ๋œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
์ธ๋ผ์ธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ณณ์— ์ธ๋ผ์ธ ํ•จ์ˆ˜ ์ฝ”๋“œ๋ฅผ ํ™•์žฅํ•ด ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.
๋งคํฌ๋กœ์™€ ์œ ์‚ฌํ•œ๋ฐ, ์ฝ”๋“œ ํ™•์žฅ ํ›„ ์ธ๋ผ์ธ ํ•จ์ˆ˜๋Š” ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.

์ธ๋ผ์ธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด,
ํ•จ์ˆ˜ ํ˜ธ์ถœ์— ๋”ฐ๋ฅธ ์˜ค๋ฒ„ํ—ค๋“œ๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์†๋„๊ฐ€ ๊ฐœ์„ ๋ฉ๋‹ˆ๋‹ค.

์ž์ฃผ ํ˜ธ์ถœ๋˜๋Š” ์งง์€ ์ฝ”๋“œ์˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ์— ๋Œ€ํ•œ ์‹œ๊ฐ„ ์†Œ๋ชจ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.