What is Class?

์šฐ๋ฆฌ๋Š” ํด๋ž˜์Šค๋ผ๋Š” ๊ฐœ๋…์„ ์™œ ๋„์ž…ํ–ˆ์„๊นŒ? C++์—์„œ ํ•จ์ˆ˜๊ฐ€ ํƒœ์–ด๋‚˜๊ฒŒ ๋œ ์ด์œ ๋„ ๋ถ„๋ฆฌํ•ด์„œ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•จ์ด์—ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ํ•จ์ˆ˜๋กœ ๊ณ„์†ํ•ด์„œ ๋‚˜๋ˆ ์„œ ํ•˜๋Š”๋ฐ ํ•œ๊ณ„๊ฐ€ ์ฐพ์•„์˜จ ๊ฒƒ! ํ•จ์ˆ˜์— ๋“ค์–ด๊ฐ€๋Š” ์ž…์ถœ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํŠน์ • ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ์—ฐ๊ด€๋˜์–ด ํ•„์š”ํ•˜๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ์•„๋ƒˆ๋‹ค.

์‹ค์ œ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์–ด๋–ค ๋ฌผ๊ฑด์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด, ํ•ด๋‹น ๋ฌผ๊ฑด(๊ฐ์ฒด)๋Š” ๋ฌผ๊ฑด์˜ ์šฉ๋„์— ๋”ฐ๋ผ ํ•  ์ˆ˜ ์žˆ๋Š” ํ–‰๋™(method)๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” ์–ด๋–ค ๊ฐ์ฒด์ธ์ง€๋ฅผ ๋ฐํžˆ๊ณ , ๊ทธ ๊ฐ์ฒด๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋†“๋Š” ๊ฒƒ์ด ๋ณด๋‹ค ์‹ค์ œ์ ์ธ ์ ‘๊ทผ์ด๋‹ค.

์ด ๋•Œย ํด๋ž˜์Šคย ์˜ ๊ฐœ๋…์€ ์‹ ์ด ๋ฌด์–ธ๊ฐ€๋ฅผ ๋งŒ๋“ค๋•Œ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์ง€ ์ƒ๊ฐํ•ด๋†“์€ย ์„ค๊ณ„๋„ย ๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

Class ์ƒ์„ฑ ๋ฐฉ๋ฒ•

Member data

์‹ค์ œ ์„ธ์ƒ์— ๋น„์œ ํ•˜๋ฉด ๋ฌผ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ํ–‰์œ„์— ํ•ด๋‹นํ•œ๋‹ค!

ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ, ๊ธฐ๋ณธ์œผ๋กœ ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์ง€๊ณ ์„œ ๋™์ž‘์„ ํ•  ๊ฒƒ์ธ์ง€ ์ •์˜ํ•œ๋‹ค.

Member function

๋ฌผ์ฒด์— ๋Œ€ํ•œ ํ–‰๋™์„ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋“ฏ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

Private, public

ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ํ–‰์œ„๋Š” ํŠน์ • ๋ฌผ์ฒด์— ๋Œ€ํ•ด ์„ค๊ณ„๋„๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๋ฏ€๋กœ, ์‚ฌ์šฉ์ž ์ง€ํ–ฅ์ ์ด์–ด์•ผ ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋ฆฌ๋ชจ์ปจ์„ ์„ค๊ณ„ํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ ์‚ฌ์šฉ์ž๋Š” ์ž…๋ ฅ ์‹ ํ˜ธ์™€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ๋งŒ ์•Œ๋ฉด๋˜์ง€, ๊ทธ ์ค‘๊ฐ„์— ์ž‘์šฉํ•˜๋Š” ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ์ œ์–ดํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

๋”ฐ๋ผ์„œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ์„ค๊ณ„์ž ์ž…์žฅ์—์„œ ํด๋ž˜์Šค ๋‚ด์—์„œ๋งŒ ์ž‘๋™ํ•˜๋Š”ย privateย ๊ตฌ๊ฐ„๊ณผ ํด๋ž˜์Šค ๋ฐ–์—์„œ ์ ‘๊ทผ๊ฐ€๋Šฅํ•œย publicย ๊ตฌ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

Constructor

์šฐ๋ฆฌ๊ฐ€ ๋ฌผ๊ฑด์„ ๋– ์˜ฌ๋ฆฌ๋ฉด, ๋‹ค๋ฅธ ๋ฌผ๊ฑด๋“ค์ด ์„ธ์ƒ์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ ๊ณตํ†ต์ ์„ ๊ฐ€์ง„๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋ฆฌ๋ชจ์ปจ์ด๋ผ ํ•œ๋‹ค๋ฉด, ๋ณดํ†ต 10๊ฐœ ์ •๋„์˜ ์ˆซ์ž๋ฒ„ํŠผ๊ณผ, ์ „์›๋ฒ„ํŠผ ์ •๋„๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ฌ๋ ค์žˆ๋Š” ๋ชจ์Šต์„ ๋– ์˜ฌ๋ฆด ๊ฒƒ์ด๋‹ค.

์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ,ย ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ€์ ธ์•ผ ํ•˜๋Š” ๊ฐ’์ด ์กด์žฌํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์ด๊ฑธ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ, ํ˜น์€ ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์ด๊ฒƒ์„ย ์ƒ์„ฑ์žย ๋ผ ํ•œ๋‹ค.

Publicย ๋ฐ‘์— ์„ ์–ธ์„ ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด ๋•Œ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์˜ ๋ชจ์–‘๊ณผ ๋‹ค๋ฅด๊ฒŒย Outputย ์ด ์—†๋‹ค. ๋˜ํ•œ, ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•์ด ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค! ๊ธฐ์–ตํ•ด๋‘˜ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

Example

Example 1

#include <iostream>
 
using namespace std;
 
class Point {
    
// ํด๋ž˜์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ Member variable ์ด๋ผ ํ•œ๋‹ค.
    
private:    // x, y ๋ณ€์ˆ˜๋Š” ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๊ณ , ํ•จ์ˆ˜๋กœ ์ด ๋ณ€์ˆ˜๋ฅผ ์ œ์–ดํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ.      
    int x;  // ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ
    int y;	
 
// ํด๋ž˜์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ Member Function(Method) ์ด๋ผ ํ•œ๋‹ค.
public:     // ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅ
    		
    void setXY(int _x, int _y) {
        x = _x;
        y = _y;
    }							// pt1.x = 1 ๊ณผ ๊ฐ™์ด ์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝ์‹œํ‚จ๋‹ค.
    							// ์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์„œ ๊ฐ’์„ ํ• ๋‹นํ•ด์ค€๋‹ค.
    
    int getX() {return x;}		// ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋„ ์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ฐ€์ ธ์˜จ๋‹ค.
    int getY() {return y;}
    							
    							// ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋„ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
    void print(){
        cout << "(" << x << ", " << y << ")" << endl;
    }
 
};
 
int main(){
 
    Point pt1, pt2;				// Point๋ผ๋Š” ํด๋ž˜์Šค๋กœ ๋ถ€ํ„ฐ ๊ฐ์ฒด 2๊ฐœ๋ฅผ ๋งŒ๋“ ๋‹ค.
//    pt1.x = 1;				// x, y๋ฅผ public ์—์„œ ์„ ์–ธํ–ˆ๋‹ค๋ฉด ์‹คํ–‰๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ์ด๋‹ค.
//    pt1.y = 2;
 
    pt1.setXY(1, 2);			// ์ƒ์„ฑ๋œ ๊ฐ์ฒด pt1์˜ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.
    
    cout <<  pt1.getX() << endl;		// pt1์˜ x๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ์ถœ๋ ฅํ•œ๋‹ค.
    
//    cout << pt1.x << ", " << pt1.y <<endl;		// public ์œผ๋กœ ์„ ์–ธํ–ˆ๋‹ค๋ฉด ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ
    pt1.print();						// pt1 ๊ฐ’์„ ์ถœ๋ ฅํ•ด๋ผ
    pt2.print();
 
    int temp = pt1.getX();				// ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ๋ฒ•
    cout << temp << endl;
 
    return 0;
}

Example 2

#include <iostream>
#include <string>
 
using namespace std;
 
class Account {
private:
    string name;
    string id;
    double balance;
public:
    // constructor
    // ์ƒ์„ฑ์ž์˜ ์ด๋ฆ„์€ class์˜ ์ด๋ฆ„๊ณผ ๊ฐ™๋‹ค.
    // output์ด ์—†๋‹ค.
    // ์ดˆ๊ธฐํ™”๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ํ•จ์ˆ˜ (์ƒ์„ฑ์ž)
    
    // ์•„๋ฌด ๊ฒƒ๋„ ์ž…๋ ฅ์ด ์•ˆ๋˜์—ˆ์„ ๋•Œ ์ดˆ๊ธฐ๊ฐ’
    Account():
    	name("์ดˆ๊ธฐ๊ฐ’์ด๋ฆ„"), id(0000), balance(0){
    }
    
    // ์‚ฌ์šฉ์ž๊ฐ€ ํ•˜๊ณ  ์‹ถ์€ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ,
    Account(string _name, string _id, double _bal):
        name(_name), id(_id), balance(_bal){
            
            // ์ดˆ๊ธฐํ™”๊ฐ€ ์ง„ํ–‰๋  ๋•Œ ๋™์ž‘๋˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ณต๊ฐ„
            // ๋งˆ์ด๋„ˆ์Šค ํ†ต์žฅํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ฏ€๋กœ ์ž”๊ณ ๊ฐ€ ์Œ์ˆ˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
            if (balance < 0){
                cout << "Error, negatice balance" << endl;
                balance = 0;
            }
        }
    
    // Method
    
    // ์˜ˆ๊ธˆํ•  ๋•Œ,
    void deposit(int _amt){
        balance += _amt;
    }
    
    // ์ถœ๊ธˆํ•  ๋•Œ, ์ž”๊ณ ๋ณด๋‹ค ์ถœ๊ธˆ๊ธˆ์•ก์ด ๋งŽ์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜
    bool withdraw(int _amt){
        if( balance - _amt < 0)
            return false;
        // ์ถœ๊ธˆ์ด ๊ฐ€๋Šฅํ•˜๋ฉด ์ž”๊ณ ์—์„œ ๊ฐ’์„ ๋นผ๊ณ  true ๋ฐ˜ํ™˜
        else{
            balance -= _amt;
            return true;
        }
    }
    
    void print(){
        cout << setw(10) << name << setw(10) << id <<  setw(10) << balance << endl;
    }
    
};
 
 
int main(){
    
    Account ac1("์ตœ์™„์‹", "0316", 5000);
    ac1.print();
    
    ac1.deposit(2000);
    ac1.print();
    
//    ac1.withdraw(3000);
//    ac1.print();
    
    
    if(!ac1.withdraw(10000))
        cout << "์ž”์•ก ๋ถ€์กฑ" << endl;
    ac1.print();
    
    
    ac1.withdraw(5000);
    ac1.print();
    
    return 0;
}

Example 3

์‚ฌ์šฉ์ž์˜ ๊ณ„์ขŒ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•˜๊ณ , ์ด๋ฆ„๋ณ„, ์•„์ด๋””๋ณ„, ์ž”๊ณ ๋ณ„ ์ •๋ ฌ์„ ํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด๋ณด์ž. ๋จผ์ € ์„ค๋ช…์ด ์ ํžŒ ์ฝ”๋“œ๋ถ€ํ„ฐ ๋ณด๊ณ  ๊ฐ€์ž.

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
class Account{
public:
    std::string name;
    int id;
    double balance;
};
 
void add_account(std::vector<Account>& accts){
    std::string name;
    int number;
    double amount;
    
    cout << "Enter name, account number, and account balance: ";
    cin >> name >> number >> amount;
    Account acct;
    
    acct.name = name;
    acct.id = number;
    acct.balance = amount;
    accts.push_back(acct);
}
 
 
void print_accounts(const std::vector<Account>& accts){
    int n = (unsigned int)accts.size();
    
    for (int i = 0; i < n; i++)
        cout << accts[i].name << ", " << accts[i].id << ", " << accts[i].balance << endl;
}
 
 
void swap(Account& er1, Account& er2){
    Account temp = er1;
    er1 = er2;
    er2 = temp;
}
 
bool less_than_by_name(const Account& e1, const Account& e2){
    return e1.name < e2.name;
}
 
 
bool less_than_by_id(const Account& e1, const Account& e2){
    return e1.id < e2.id;
}
 
 
bool less_than_by_balance(const Account& e1, const Account& e2){
    return e1.balance < e2.balance;
}
 
 
void sort(std::vector<Account>& db, bool (*comp)(const Account&, const Account&)){
    // ์ฐธ์กฐ๋ณ€์ˆ˜ db๋กœ vector๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ ,
    // comp๋ผ๊ณ  ๋ถ€๋ฅผ ํŠน์ • ํ•จ์ˆ˜์˜ ์‹œ์ž‘์ฃผ์†Œ๋ฅผ bool ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์— ๋‹ด๋Š”๋‹ค.
    // ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์‹œ์ž‘์ฃผ์†Œ์— ์žˆ๋Š” ํ•จ์ˆ˜๋Š”
    // Account ์ž๋ฃŒํ˜•์„ ๊ฐ€์ง€๋Š” ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•ด์„œ ๊ฐ€์ ธ์˜จ ๋…€์„ ๋‘๊ฐœ๋ฅผ ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š”๋‹ค.
    
    // db์˜ ์ด ์‚ฌ์ด์ฆˆ๋ฅผ ๋‹ด์•„ ๋‘”๋‹ค. ๋ฒกํ„ฐ์ด๋ฏ€๋กœ unsigned int๋กœ ๋ฐ›์•„์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    int size = (unsigned int)db.size();
    
    // 10 ํฌ๊ธฐ์ธ ๋ฐฐ์—ด์ด๋ผ๋ฉด, 0~8๊นŒ์ง€ i๋Š” ์›€์ง์ธ๋‹ค.
    for(int i = 0; i < size - 1; i++){
        // ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์ด๋ผ ๊ฐ€์ •ํ•œ๋‹ค.
        int smallest = i;
        // i์˜ ๋‹ค์Œ ๋ฒˆ์งธ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•œ๋‹ค.
        for (int j = i + 1; j < size; j++)
            if(comp(db[j], db[smallest]))
                // ๋น„๊ต๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋–ค ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋น„๊ตํ•จ์— ๋”ฐ๋ผ์„œ ๋‹ฌ๋ผ์ ธ์•ผ ํ•œ๋‹ค.
                // ์—ฌ๊ธฐ์„œ ํ•จ์ˆ˜ํฌ์ธํ„ฐ๋ฅผ ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ ๊ฒƒ์ด ํšจ๊ณผ๋ฅผ ๋ฐœํœ˜ํ•œ๋‹ค.
                // ํŠน์ • ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ์ž‘์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋˜ smallest ์ธ๋ฑ์Šค์˜ ๊ฐ’๊ณผ
                // ํ˜„์žฌ j์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค.
                // comp์˜ ํ›„๋ณด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋“ค 
                // (less_than_by_name, less_than_by_id, less_than_by_balance)
                // ์˜ ๋ฆฌํ„ด ๊ฐ’์€ ๋ชจ๋‘ boolian ์ด๋‹ค.
                // ๊ทธ๋ฆฌ๊ณ  ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ํด ๋•Œ, True ์ด๋‹ค.
                // ๋”ฐ๋ผ์„œ ์ด if ๋ฌธ์ด ์‹คํ–‰๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‚ด๊ฐ€ ์ž‘๋‹ค๊ณ  ๊ฐ€์ •ํ•œ smallest ์ธ๋ฑ์Šค๊ฐ€
                // j ์ธ๋ฑ์Šค ๋ณด๋‹ค ํด ๋•Œ ์‹คํ–‰๋˜๋ฉฐ, ์šฐ๋ฆฌ๋Š” smallest ์ธ๋ฑ์Šค๊ฐ€ j๊ฐ€ ๋˜์–ด์•ผ ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
                smallest = j;
        	// ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ๋Š” smallest ์ธ๋ฑ์Šค๊ฐ€ ์—ฌ์ „ํžˆ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ ๋‚ด๋น„๋‘๋ฉด ๋œ๋‹ค.
        
        // ๋งŒ์•ฝ์— ์œ„์˜ ์กฐ๊ฑด๋ฌธ์ด ์‹คํ–‰๋˜์—ˆ๋‹ค๋ฉด
        // smallest๊ฐ€ ์ด์ „์˜ ์ธ๋ฑ์Šค์™€ ๋‹ฌ๋ผ์กŒ์„ ๊ฒƒ์ด๋‹ค.
        // ์ด๋•Œ ์šฐ๋ฆฌ๋Š” ์‹ค์ œ i ์ธํ…์Šค์—์„œ์™€ smallest ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.
        if (smallest != i)
            swap(db[i], db[smallest]);
    }
}
 
 
int main(){
    // ์ž๋ฃŒํ˜•์ด Account ํด๋ž˜์Šค์ธ ๋ฒกํ„ฐ๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ด๊ฒƒ์˜ ์ด๋ฆ„์„ customers๋ผ ํ•˜์ž.
    std::vector<Account> customers;
    
    char cmd;
    bool done = false;
    
    do{
        cout << "[A]dd [N]ame [I]D [B]alanc [Q]uit ==> ";
        cin >> cmd;
        
        switch(cmd){
            case 'A':
            case 'a':
                add_account(customers);
                break;
            case 'P':
            case 'p':
                print_accounts(customers);
                break;
                
            // ํ•จ์ˆ˜์ด๋ฆ„์ด ํฌ์ธํ„ฐ์ด๋ฏ€๋กœ ํ•จ์ˆ˜์ด๋ฆ„์ž์ฒด๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.
            case 'N':
            case 'n':
                sort(customers, less_than_by_name);
                print_accounts(customers);
                break;
            case 'I':
            case 'i':
                sort(customers, less_than_by_id);
                print_accounts(customers);
                break;
            case 'B':
            case 'b':
                sort(customers, less_than_by_balance);
                print_accounts(customers);
                break;
            case 'Q':
            case 'q':
                done = true;
                break;
        }
    }while(!done);
}
 

์ด์ œ ์„ค๋ช…์ด ์—†๋Š” ์ฝ”๋“œ์ด๋‹ค.

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
class Account{
public:
    std::string name;
    int id;
    double balance;
    
};
 
void add_account(std::vector<Account>& accts){
    std::string name;
    int number;
    double amount;
    
    cout << "Enter name, account number, and account balance: ";
    cin >> name >> number >> amount;
    Account acct;
    acct.name = name;
    acct.id = number;
    acct.balance = amount;
    accts.push_back(acct);
}
 
 
void print_accounts(const std::vector<Account>& accts){
    int n = (uint32_t)accts.size();
    
    for (int i = 0; i < n; i++)
        cout << accts[i].name << ", " << accts[i].id << ", " << accts[i].balance << endl;
}
 
 
void swap(Account& er1, Account& er2){
    Account temp = er1;
    er1 = er2;
    er2 = temp;
}
 
bool less_than_by_name(const Account& e1, const Account& e2){
    return e1.name < e2.name;
}
 
 
bool less_than_by_id(const Account& e1, const Account& e2){
    return e1.id < e2.id;
}
 
 
bool less_than_by_balance(const Account& e1, const Account& e2){
    return e1.balance < e2.balance;
}
 
 
void sort(std::vector<Account>& db, bool (*comp)(const Account&, const Account&)){
    int size = (uint32_t)db.size();
    
    for(int i = 0; i < size - 1; i++){
        int smallest = i;
        for (int j = i + 1; j < size; j++)
            if(comp(db[j], db[smallest]))
                smallest = j;
        if (smallest != i)
            swap(db[i], db[smallest]);
    }
}
 
 
int main(){
    std::vector<Account> customers;
    
    char cmd;
    bool done = false;
    
    do{
        cout << "[A]dd [N]ame [I]D [B]alanc [Q]uit ==> ";
        cin >> cmd;
        
        switch(cmd){
            case 'A':
            case 'a':
                add_account(customers);
                break;
            case 'P':
            case 'p':
                print_accounts(customers);
                break;
            case 'N':
            case 'n':
                sort(customers, less_than_by_name);
                print_accounts(customers);
                break;
            case 'I':
            case 'i':
                sort(customers, less_than_by_id);
                print_accounts(customers);
                break;
            case 'B':
            case 'b':
                sort(customers, less_than_by_balance);
                print_accounts(customers);
                break;
            case 'Q':
            case 'q':
                done = true;
                break;
        }
    }while(!done);
}