MapleStory Finger Point

๐Ÿ”ต C,C++,C#/C#

C# ์ •๋ฆฌ

HYEJU01 2024. 4. 14. 21:18

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ ์ด๋ž€?
๊ฐ์ฒด์ง€ํ–ฅ ๊ฐœ๋…(์บก์Šํ™”, ์ •๋ณด์€๋‹‰ ...)
ํ”„๋กœ๊ทธ๋žจ ๋ถ„์„(๊ฐ๊ด€์‹)
์ž…์ถœ๋ ฅ(cin, cout)
ํด๋ž˜์Šค(์ธ์Šคํ„ด์Šค) ๊ด€๊ณ„, ๊ตฌ์„ฑ
๊ฐ์ฒด (์‚ฌ์šฉ)  ๋™์ ์ƒ์„ฑ/์ •์ ์ƒ์„ฑ
์ ‘๊ทผ์ œ์–ด (public, private, protected)
๋ฉค๋ฒ„ ๋ณ€์ˆ˜/ํ•จ์ˆ˜ ์™ธ๋ถ€์ •์˜
์˜ค๋ฒ„๋กœ๋”ฉ(์ƒ์„ฑ์ž)/ ์˜ค๋ฒ„๋ผ์ด๋”ฉ
UML
์ƒ์„ฑ์ž/์†Œ๋ฉธ์ž (์ˆœ์„œ)
๋ฉค๋ฒ„ ์ดˆ๊ธฐํ™”(์ƒ์ˆ˜ ์ดˆ๊ธฐํ™” , ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• 3๊ฐ€์ง€.)
this ํฌ์ธํ„ฐ
์ •์ ๋ฉค๋ฒ„(static)
์ƒ์†
๋ถ€๋ชจ์ƒ์„ฑ์ž ๋ช…์‹œ์  ํ˜ธ์ถœ

 

 

 


๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ ์ด๋ž€?

๋ฐ์ดํ„ฐ์™€ ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ๋ฉ์–ด๋ฆฌ๋กœ ๋ฌถ์–ด์„œ ์ƒ๊ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
๋ฐ์ดํ„ฐ์™€ ํ•จ์ˆ˜๋ฅผ ๊ฐ์ฒด๋กœ ๋ฌถ๋Š” ๊ฒƒ์„ ์บก์Šํ™”(encapsulation)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.


- ๋ฐ์ดํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‘˜ ๋‹ค ์ค‘์š”ํ•ด์ง
- ๋ฐ์ดํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ class๋กœ ๋ฌถ์–ด์„œ ๊ฐ์ฒด๋ผ๊ณ  ๋ถ€๋ฆ„
- ๊ฐ์ฒด = ์‹ค์ œ ์„ธ๊ณ„๋ฅผ ๋ชจ๋ธ๋งํ•จ

๊ฐ์ฒด์ง€ํ–ฅ์˜ ์ตœ์ข… ๋ชฉํ‘œ
- ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ
๊ฐ์ฒด์ง€ํ–ฅ

- ์‹ค์ œ ์„ธ๊ณ„๋ฅผ ๋ชจ๋ธ๋งํ•ด์„œ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•

๊ฐ์ฒด

- ๊ฐ์ฒด๋Š” ์ƒํƒœ์™€ ๋™์ž‘์„ ๊ฐ€์ง€๊ณ ์žˆ์Œ
- ๊ฐ์ฒด ์ƒํƒœstate = ์†์„ฑ (๋ณ€์ˆ˜ํƒ€์ž…
- ๋™์ž‘ behavior = ๊ฐ์ฒด๊ฐ€ ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ๋™์ž‘ (ํ•จ์ˆ˜

ํด๋ž˜์Šค

- ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ
- ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์„ค๊ณ„๋„

์ธ์Šคํ„ด์Šค

- ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋“ค์„ ๊ทธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค


๊ฐ์ฒด์ง€ํ–ฅ ๊ฐœ๋…(์บก์Šํ™”, ์ •๋ณด์€๋‹‰ ...)

- 1์บก์Šํ™” ::๋ฐ์ดํ„ฐ์™€ ํ•จ์ˆ˜(์—ฐ์‚ฐ)๋ฅผ ๊ฐ์ฒด ์•ˆ์— ๋„ฃ์–ด ๋ฌถ๋Š” ๊ฒƒ
- 2์ •๋ณด์€๋‹‰:: private ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๋ฐ์ดํ„ฐ์™€ ๊ตฌํ˜„์˜ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ์™ธ๋ถ€ ์„ธ๊ณ„์—์„œ ๊ฐ์ถ”๋Š” ๊ฒƒ
- 3์ƒ์†::๋ถ€๋ชจํด๋ž˜์Šค๋ฅผ ์ด์–ด๋ฐ›์Œ
๊ธฐ์กด์˜ ์ฝ”๋“œ๋ฅผ ์žฌํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ฒ•, 
์ด๋ฏธ ์ž‘์„ฑ๋œ ํด๋ž˜์Šค(๋ถ€๋ชจํด๋ž˜์Šค)๋ฅผ ์ด์–ด ๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด ํด๋ž˜์Šค (์ž์‹ ํด๋ž˜์Šค) ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋ฒ•
- 4๋‹คํ˜•์„ฑ:: ๊ฐ์ฒด์˜ ๋™์ž‘์ด ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง 
๊ฐ์ฒด๊ฐ€ ์ทจํ•˜๋Š” ๋™์ž‘ํžˆ ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค -> ํ•จ์ˆ˜ ์ด๋ฆ„์˜ ์žฌ์‚ฌ์šฉ

 

ํ”„๋กœ๊ทธ๋žจ ๋ถ„์„(๊ฐ๊ด€์‹)

 

 

 

์ž…์ถœ๋ ฅ(cin, cout)

 

C++์—์„œ๋Š” ์ฝ˜์†” ์ž…๋ ฅ์€ cin ๊ฐ์ฒด๊ฐ€, ์ฝ˜์†” ์ถœ๋ ฅ์€ cout ๊ฐ์ฒด๊ฐ€ ๋‹ด๋‹น
์ด๋“ค์€ ๋ชจ๋‘ iostream ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํฌํ•จ

# Cout ๊ฐ์ฒด

- ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
- << ์‚ฝ์ž…์—ฐ์‚ฐ์ž
- cout << ์ถœ๋ ฅ๋ฐ์ดํ„ฐ ;

# Cin ๊ฐ์ฒด

- ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ๋ฐ›์Œ
- >> ์ถ”์ถœ ์—ฐ์‚ฐ์ž
- cin >> ์ €์žฅํ• ๋ณ€์ˆ˜;


cin >> num; //์ž…๋ ฅ
 cout << "๋ฒˆํ˜ธ๋Š” : " << num << "์ž…๋‹ˆ๋‹ค" << endl; // ์ถœ๋ ฅ 

#include<iostream>
using namespace std;

int main() {
	printf("hello pc\n");
	std::cout << "hello\n pc" << std::endl;
	cout << "hello pc" << endl;

}
/* #include<stdio.h>

int main() {

	printf("hellp PC\n");

	return 0;
}
*/
//์ฃผ์„ ์ฒ˜๋ฆฌ ๋‹จ์ถ•ํ‚ค

//#include<math.h> 
//C์–ธ์–ด ๊ฐœ์ • ์™„๋ฃŒ C99 1999 ์ž˜ ์•ˆ๋ฐ”๋€œ
//C++ 3๋…„๋งˆ๋‹ค ๊ฐœ์ •์ด ๋จ (๋‹ค๋ฅผ์ˆ˜์žˆ์Œ)

#include<iostream> //input output ํ๋ฅด๋‹ค, ํ—ค๋”ํŒŒ์ผ
using namespace std; //๊ธฐ๋ณธ์ ์œผ๋กœ ์จ์ค€๋‹ค , ์ด๋ฆ„๊ณต๊ฐ„์„ค์ • 


void main() { // void . ๋ฆฌํ„ด์ž , ํ•จ์ˆ˜์„ ์–ธ

	printf("hellp PC\n"); 
	//์ž๋ฆฟ์ˆ˜ ๋งž์ถœ๋•Œ 
	cout << "hello PC" << endl; 
	//ํ”„๋ฆฐํŠธf ์™€ /n = endl = ์ž๋ฆฟ์ˆ˜ ๋งž์ถœ ํ•„์š” ์—†์„ ๋•Œ
 //์•„๋ฌด๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•ด๋„๋จ

	cout << "hello PC"; //์ฝ˜์†”์•„์›ƒ == ์ถœ๋ ฅ
	cout << "hello PC" << endl; 
	 cout << "hello PC" << endl;
	 cout << "hello" << "PC\n" << endl << "white" << endl;
	
	 //cin ์ž…๋ ฅ

	 int num;
	 printf("๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”:  ");
	 cin >> num; // ํ™”์‚ดํ‘œ >> num ์•ˆ์œผ๋กœ ๋“ค์–ด๊ฐ„
	//scanf ์„œ์‹์ง€์ •์ž(ํ˜•์‹)๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ ์—†์–ด์ง
	 //C++์€ ํ˜•์‹์„ ์—†์•ฐ ์ž๋™์œผ๋กœ ์ฝ์–ด์˜ด
	 cout << "๋ฒˆํ˜ธ๋Š” : " << num << "์ž…๋‹ˆ๋‹ค" << endl; // ํ™”์‚ดํ‘œ << ๋ฐ–์œผ๋กœ ์ถœ๋ ฅ

	// return; 
}


โ˜†โ˜†โ˜†โ˜†ํด๋ž˜์Šค(์ธ์Šคํ„ด์Šค) ๊ด€๊ณ„, ๊ตฌ์„ฑ ###์ฃผ๊ด€์‹ 1๋ฌธ์ œ

 

 

 

ํด๋ž˜์Šค(class): ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์„ค๊ณ„๋„

ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฐ๊ฐ์˜ ๊ฐ์ฒด๋ฅผ ํŠน๋ณ„ํžˆ ๊ทธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค (instance)๋ผ๊ณ ๋„ ํ•œ๋‹ค.

 

class Car {}; // Car ํด๋ž˜์Šค ์ƒ์„ฑ = ํƒ€์ž…

Car c1, c2; // Car ํƒ€์ž… ๋ณ€์ˆ˜ ์„ ์–ธ = ๊ฐ์ฒด ์ƒ์„ฑ

	c1.speed = 50; //๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ ‘๊ทผ = (.) ๋„ํŠธ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
	c1.gear = 3;
	c1.start();
	c1.view();

๊ฐ์ฒด (์‚ฌ์šฉ)  ๋™์ ์ƒ์„ฑ/์ •์ ์ƒ์„ฑ

ํด๋ž˜์Šค = ํƒ€์ž…

int main(void) {

//์ •์  ์ƒ์„ฑ 
	Car myCar;
    myCar.view();

//๋™์ ์ƒ์„ฑ new ์™€ delete ์‚ฌ์šฉ
	Car* myCar = new Car(); //ํฌ์ธํ„ฐ ๋ฉ”๋ชจ๋ฆฌ์œ„์น˜์˜ ์ฐจ์ด
	myCar->speed;
	myCar->view(); //๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ์ƒ๊ธฐ๋‹ˆ๊นŒ 

	if (!myCar) //ํ•ด์ œ ์—ฌ๋Ÿฌ๋ฒˆํ•ด๋ฒ„๋ฆฌ๋ฉด ๋ฌธ์ œ ์ƒ๊ธฐ๋‹ˆ๊นŒ ์กฐ๊ฑด์‹ ๋„ฃ์–ด์ค€๋‹ค.
		delete myCar; //๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ ์‹œ์ผœ์ค˜์•ผํ•จ
}

 


์ ‘๊ทผ์ œ์–ด (public, private, protected)

 

 

#include<iostream>
using namespace std;

#private (๋””ํดํŠธ) ์ „์šฉ๋ฉค๋ฒ„-ํด๋ž˜์Šค ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ

class Car {
private: //๋ฐ์ดํ„ฐ ํ˜„์žฌ ์ƒํƒœ ๊ฐ’์— ์ ‘๊ทผ ์•ˆ๋˜๊ฒŒ ์ฃผ์˜
//๊ธฐ๋ณธ ๊ฐ’์€ private
	string color = "white";
	int speed = 0;
	int gear = 0;


# public ๊ณต์šฉ๋ฉค๋ฒ„-์–ด๋””์„œ๋“ ์ง€ ๊ฐ์ฒด๋ฅผ ํ†ตํ•˜์—ฌ ์‚ฌ์šฉ๊ฐ€๋Šฅ

public:
	void speedUp() { //ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ์ œ์•ฝ์„ ๊ฑด๋‹ค.(์กฐ๊ฑดํ†ต์ œ)
//๋ณ€์ˆ˜๋กœ๋Š” ์กฐ๊ฑดํ†ต์ œ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•จ
		if (speed > 100)
			speed = 0;
		else
			speed += 10;
	}

	void speedDown() {
		if (speed < 0)
			speed = 0;
		else
			speed -= 10;
	}

	void view() {
		cout << "speed = " << speed << endl;
		cout << "gear = " << gear << endl;
		cout << "color = " << color << endl;
	}

};


# protected 

์ƒ์†๊ด€๊ณ„์—์„œ ๋ถ€๋ชจํด๋ž˜์Šค์—์„œ ํŒŒ์ƒ๋œ ์ž์‹ํด๋ž˜์Šค๊ฐ€
๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋ณดํ˜ธ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

class Emp {
	int rrn;
	
protected:
		int salary;
public:
	string name;
	void setSalart(int salary);
	int getSaltry();

};

class Manager : public Emp {
	int bonus;

public:
	Manager(int b = 0) : bonus(b) {}
	void modify(int s, int b);//ํ•จ์ˆ˜์›ํ˜• ์™ธ๋ถ€์ •์˜
	void display();
};

	void Manager::modify(int s, int b) { //ํ•จ์ˆ˜ ์™ธ๋ถ€์ •์˜
		salary = s;
		bonus = b;

	}
};

 


๋ฉค๋ฒ„ ๋ณ€์ˆ˜/ํ•จ์ˆ˜ ์™ธ๋ถ€์ •์˜

 

class Cat {
	void setEat(int e); // 5.ํ•จ์ˆ˜ ์™ธ๋ถ€ ์ •์˜ - ์›ํ˜•
};


void Cat::setEat(int e) { // 5.ํ•จ์ˆ˜ ์™ธ๋ถ€ ์ •์˜
	~~
}


๋ฉค๋ฒ„ ํ•จ์ˆ˜๊ฐ€ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ •์˜๋˜๋ฉด ์ž๋™์ ์œผ๋กœ ์ธ๋ผ์ธ(inline) ํ•จ์ˆ˜๊ฐ€ ๋œ
๋‹ค. 
๏ต ๋ฉค๋ฒ„ ํ•จ์ˆ˜๊ฐ€ ํด๋ž˜์Šค ์™ธ๋ถ€์— ์ •์˜๋˜๋ฉด ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์™€ ๋™์ผํ•˜๊ฒŒ ํ˜ธ์ถœํ•œ๋‹ค
#pragma once
#include<iostream>
using namespace std;

class Cat { //1.๊ณ ์–‘์ด ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

	string name; //2.๋ฉค๋ฒ„๋ณ€์ˆ˜๋งŒ๋“ค๊ธฐ
	bool sleep;
	int age;
	int hungry;
	string meow;
	string meow2;


public:

	Cat() { //7.์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ (์ƒ์„ฑ์ž 1๋ฒˆ , ๋‚ด๋ถ€์ •์˜)
		name = "NABI";
		sleep = 0;
		age = 0;
		hungry = 0;
		meow = "...";
		meow2 = "...";

	}

	//Cat(int a); //7.์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ (<์ค‘๋ณต์ƒ์„ฑ์ž> ๋งค๊ฐœ๋ณ€์ˆ˜ ์žˆ๋Š” ์ƒ์„ฑ์ž 2๋ฒˆ , ์™ธ๋ถ€์ •์˜)


	void setName(string n) { //2.๋ฉค๋ฒ„ํ•จ์ˆ˜๋งŒ๋“ค๊ธฐ , 3. set์„ค์ •์ž
		name = n;
	};

	string getName() {  // 3.get์ ‘๊ทผ์ž
		return name;
	};

	void setAge(int a) { 
		age = a;
	}

	/* void setAge(double a) { //11. double ๊ฐ’๋“ค์–ด์˜ค๋ฉด ๋ฐ˜์˜ฌ๋ฆผํ•˜๊ธฐ
		age = a;
		age = ceil(a);
	} */


	int getAge() {
		return age;
	}


	void setSleepOn() {
		sleep = true;
		meow = "Zzzz...";
	};

	void setSleepOff() {
		sleep = false;
		meow = "์ผ์–ด๋‚ฌ์–ด์š”.";
		setAttractOn();
	};
	
	void setEat(int e); // 5.ํ•จ์ˆ˜ ์™ธ๋ถ€ ์ •์˜

	int getEat() {
		return hungry;
	}

	void setAttractOn() {
		meow.append("Meow~♥");
	}
	void setAttractOff() {
		meow2.append(" Meow@#@^&#!#!!!!");
	}


	void view() {
		cout << " ======์ƒํƒœ===== "<< endl;
		cout << " ์ด๋ฆ„   : " << name << endl;
		cout << " ๋‚˜์ด   : " << age << endl;
		cout << " ๋ฐฐ๊ณ ํ”” : " << hungry << endl << endl;
		cout << " ======๋Œ€ํ™”===== " << endl;
		cout << "๋‚˜      :" << name << ", ๋ญํ•ด?" << endl;
		cout << name << "   : " << meow << endl << endl;
		cout << "๋‚˜      :" << name << ", ๋ฐฐ๊ณ ํ”„๋‹ˆ?" << endl;
		cout << name << "   : " << meow2 << endl;
		cout << " =============== " << endl;
		
	}

	~Cat() { //์†Œ๋ฉธ์ž
	}

};

void Cat::setEat(int e) { // 5.ํ•จ์ˆ˜ ์™ธ๋ถ€ ์ •์˜
	
	hungry = e;

	if (sleep == false) {
		if (e > 8) {
			meow2 = "๋ฐฐ๋ถˆ๋Ÿฌ์š”";
		}
		else if (e == 0) {
			meow2 = "๋„ˆ๋ฌด๋„ˆ๋ฌด ๋ฐฐ๊ณ ํŒŒ์š”";
			setAttractOff();
		}
		else if (0 < e <= 4) {
			meow2 = "๋ฐฐ๊ณ ํŒŒ์š”";
		}
	}
	else {
		meow2 = "Zzz...";
	}
		
}

	/* Cat::Cat(int a) {//7.์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ (<์ค‘๋ณต์ƒ์„ฑ์ž> ๋งค๊ฐœ๋ณ€์ˆ˜ ์žˆ๋Š” ์ƒ์„ฑ์ž 2๋ฒˆ , ์™ธ๋ถ€์ •์˜)
		name = "NABI";
		sleep = 0;
		age = a;
		hungry = 0;
		meow = "...";
		meow2 = "...";
	}
	*/

 

 


์˜ค๋ฒ„๋กœ๋”ฉ(์ƒ์„ฑ์ž)/ ์˜ค๋ฒ„๋ผ์ด๋”ฉ

 

์˜ค๋ฒ„๋กœ๋”ฉ = ์ค‘๋ณต์ •์˜

class Car {
private:
	string color = "RED";
	int speed = 0;
	int gear = 0;

public:
	int getSpeed() { //์ ‘๊ทผ์ž
		return speed;
	}
    
#๋ฉค๋ฒ„ํ•จ์ˆ˜ ์ค‘๋ณต ์ •์˜(์˜ค๋ฒ„๋กœ๋”ฉ)๊ฐ€ ๊ฐ€๋Šฅ

	void setSpeed(int s);
	void setSpeed(double s) { cout << "double" << endl; };
	void setSpeed(int s, double g) { cout << "int, double" << endl; };
	void setSpeed(double s, int g) { cout << "double, int" << endl; };
	void setSpeed(string c) { cout << "string" << endl; };

//

	c1.setSpeed(); //์˜ค๋ฅ˜
	c1.setSpeed(10); // ๊ณณ์ด ์ •ํ•ด์ ธ์ž‡์Œ ์˜ค๋ฅ˜ ์•ˆ๋‚จ
	c1.setSpeed(10,3.5); // ๊ณณ์ด ์ •ํ•ด์ ธ์ž‡์Œ ์˜ค๋ฅ˜ ์•ˆ๋‚จ
}

--------------------------------
	//๊ณต ์ƒ์„ฑ์ž
	// ๋””ํดํŠธ ์ƒ์„ฑ์ž
	//์ธ์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž


	int getGear();
	void setGear(int g) { gear = g; }
	int getSpeed() { return speed; }


	int Car::getGear() {
		return gear;
	}

	void Car::setSpeed(int s) {
		if (speed > 100)
			speed = 100;
		else
			speed = s;
	}

์˜ค๋ฒ„๋ผ์ด๋”ฉ = ์žฌ์ •์˜

์žฌ์ •์˜(overriding): ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ํ•„์š”์— ๋”ฐ๋ผ ์ƒ์†๋œ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ์ •์˜ํ•˜๋Š” ๊ฒƒ

 

/* ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์™€ ๋™์ผํ•œ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค. 
๏ต ์ฆ‰ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์˜ ์ด๋ฆ„, ๋ฐ˜ํ™˜ํ˜•, ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์ผ์น˜ํ•˜์—ฌ
์•ผ ํ•œ๋‹ค. */


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

class Car {

public:
	int getHp()
	{
		return 100; // 100๋งˆ๋ ฅ ๋ฐ˜ํ™˜
		}
};

class SportsCar :public Car {
public: 
	int getHp()
	{
		return 300; // ์˜ค๋ฒ„๋ผ์ด๋”ฉ = ์žฌ์ •์˜
	}
};

int main() {
	SportsCar sc;
	cout << "๋งˆ๋ ฅ :" << sc.getHp() << endl;
	return 0;
}

--------------

int main() {
	SportsCar sc;
	cout << ":" << sc.Car::getHp() << endl; //์žฌ์ •์˜ ์ „ ํ•จ์ˆ˜ ํ˜ธ์ถœ
	return 0;
}


UML

UML(Unified Modeling Language):

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์„ฑํ•˜๋Š” ํด๋ž˜์Šค๋“ค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•˜์—ฌ ์‚ฌ์šฉ

 


์ƒ์„ฑ์ž/์†Œ๋ฉธ์ž (์ˆœ์„œ)

 

๊ฐ์ฒด์˜ ์ƒ์„ฑ
์ƒ์„ฑ์ž()
๊ฐ์ฒด์˜ ์‚ฌ์šฉ
์†Œ๋ฉธ์ž()
๊ฐ์ฒด์˜ ํŒŒ๊ดด

m.blog.naver.com/psychoria/40093564549

์ƒ์„ฑ์ž(contructor):

๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ์— ํ•„๋“œ์—๊ฒŒ ์ดˆ๊ธฐ๊ฐ’์„ ์ œ๊ณตํ•˜๊ณ  ํ•„์š” ํ•œ ์ดˆ๊ธฐํ™” ์ ˆ์ฐจ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฉค๋ฒ„ ํ•จ์ˆ˜

๏ต ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ๋™์ผํ•˜๋‹ค
๏ต ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋‹ค. 
๏ต ๋ฐ˜๋“œ์‹œ public ์ด์–ด์•ผ ํ•œ๋‹ค.
๏ต ์ค‘๋ณต ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค

-์ƒ์„ฑ์ž ์™ธ๋ถ€ ์ •์˜ 
	1. ๋””ํดํŠธ ์ƒ์„ฑ์ž
    


    
	2. ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์ƒ์„ฑ์ž
    
    
	3. ์ƒ์„ฑ์ž ์ค‘๋ณต์ •์˜
	4. ์ƒ์„ฑ์ž ํ˜ธ์ถœ
	5. ์ƒ์„ฑ์ž ํ•˜๋‚˜๋„ ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด,
	6. ๋””ํดํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜
    7. ์ƒ์„ฑ์ž์—์„œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
    
    
        A(int a, int b){
        m_a = a;
        m_b = b;
};

    
    A(int a, int b) : m_a(a), m_b(b){  // ์ดˆ๊ธฐํ™” ๋ฆฌ์ŠคํŠธ(Member Initialization List)
        //๋” ํ•  ์ผ
};

1. ์ƒ์ˆ˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”
class ABC{
public:
        const int A;
        ABC(int i) : A(i){}
};
 2. ๋ ˆํผ๋Ÿฐ์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”
 3. ํฌํ•จ๋œ ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™”
 4. ์ƒ์† ๋ฐ›์€ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ๋•Œ
์†Œ๋ฉธ์ž๋Š” ํด๋ž˜์Šค ์ด๋ฆ„์— ~๊ฐ€ ๋ถ™๋Š”๋‹ค. 
๏ต ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. 
๏ต public ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋กœ ์„ ์–ธ๋œ๋‹ค. 
๏ต ์†Œ๋ฉธ์ž๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.
๏ต ์ค‘๋ณต ์ •์˜๋„ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
	1.์†Œ๋ฉธ์ž
    ~Car() {
		}
    
 	2.๋””ํดํŠธ ์†Œ๋ฉธ์ž //ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์†Œ๋ฉธ์ž ์ •์˜ ํ•˜์ง€ ์•Š์•˜์œผ๋ฉด ์ž๋™์‚ฝ์ž…๋˜์–ด์„œ ํ˜ธ์ถœ๋จ
    
public:
	Car() {
		speed = 100;
		gear = 3;
		color = "RED";
}

//์ƒ์„ฑํ• ๋•Œ ์ดˆ๊ธฐ๊ฐ’ ๋“ค์–ด๊ฐ€๊ณ  , ์ƒ์„ฑ์ž๊ฐ€ ๋™์ž‘ํ•˜๋ฉด์„œ ๊ฐ’์ด ๋“ค์–ด๊ฐ
//์ƒ์„ฑ์ž ์ดˆ๊ธฐํ™”ํ• ๊ฑฐ๋ฉด ์ƒ์„ฑํ• ๋•Œ ์ดˆ๊ธฐ๊ฐ’ ์•ˆ๋„ฃ์–ด์ค˜๋„๋จ


// ์•ž์— ๋ฐ˜ํ™˜๊ฐ’ xx

public:
	Car() {
		speed = 100;
		gear = 3;
		color = "RED";
}

	Car(int g) {
		speed = 100;
		gear = g;
		color = "RED";
}

//์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋ผ๋„ ๋™์ž‘ํ•˜๋Š”๊ฑด ์ƒ์„ฑ์ž ๋‹จ !! 1๊ฐœ !!!!!!!!
// ์ธ์ˆ˜

main {

Car c1; // ์ธ์ˆ˜ ์—†๋Š”
Car c2(20); //์ธ์ˆ˜์žˆ๋Š”

 ์ž๋™์œผ๋กœ ๋งž์ถฐ์„œ ๋“ค์–ด๊ฐ 
ํ•จ์ˆ˜์ธ๋ฐ ๊ฐ์ฒด ๋งŒ๋“ค๊ฑธ ์„ ํƒ๊ฐ€๋Šฅ


์™ธ๋ถ€๋กœ ๋นผ๊ธฐ

Car (int g);


	Car:: Car(int g) {
		speed = 100;
		gear = g;
		color = "RED";
}

//๊ณต์ƒ์„ฑ์ž
//๋””ํดํŠธ ์ƒ์„ฑ์ž
Car () {
}

//์ธ์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž
Car (int n) {
}


//์†Œ๋ฉธ์ž
~Car() {
}


๋ฉค๋ฒ„ ์ดˆ๊ธฐํ™”(์ƒ์ˆ˜ ์ดˆ๊ธฐํ™” , ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• 3๊ฐ€์ง€.)

 

1. ์ƒ์ˆ˜ ๋ฉค๋ฒ„ ์ดˆ๊ธฐํ™” (๋…ํŠน)

Car(): speed(300) {
}
-------------
Car(int s, int g, string c) : spped(s), gear(g), color(c) { 
}

 


this ํฌ์ธํ„ฐ

this๋Š” ํ˜„์žฌ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ

 

๋ฉค๋ฒ„๋ณ€์ˆ˜

#include <iostream>

using namespace std;

class ThisPo {
    int n1;
    int n2;
public:
    //๋ฉ”์†Œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ž‘ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ๊ตฌ๋ณ„๊ฐ€๋Šฅ
    ThisPo(int n1, int n2) { 
        this->n1 = n1; //this->n1 ๋ฉค๋ฒ„๋ณ€์ˆ˜
        this->n2 = n2;
    }

    void print()
    {
    cout << this->n1 << endl;
    cout << this->n1 << endl;
       }
};

void main() {

    ThisPo num(2, 4);
    num.print();

    return;
}

 

 

 


์ •์ ๋ฉค๋ฒ„(static)

 

์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(instance variable): ๊ฐ์ฒด๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ์žˆ๋Š” ๋ณ€์ˆ˜

์ •์  ๋ณ€์ˆ˜(static variable): ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ํ†ตํ‹€์–ด์„œ ํ•˜๋‚˜๋งŒ ์žˆ๋Š” ๋ณ€์ˆ˜

static์€ ์™ธ๋ถ€์—์„œ ์ดˆ๊ธฐํ™”

class Car {
	static int count;
};

int Car::count = 0;

 

 

 


์ƒ์†

 

: ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ์œ ์‚ฌํ•œ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์†์„ฑ๊ณผ ๋™์ž‘์„ ์ด์–ด๋ฐ›๊ณ  ์ž์‹ ์ด ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ

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

class Car {

	int speed = 10;

public:
	void setSpeed(int s) {
		speed = s;
	}

};

class sportCar :public Car { //์ƒ์†
	bool turbo = false;
public:
	void setTurbo(int t) {
		turbo = t;
	}

	void view() {
		cout << "speed = " << speed << endl; 
		// ๋ถ€๋ชจ ์ƒ์†์ด๋ผ๊ณ  ํ•ด๋„  private๋Š” ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ speed ์˜ค๋ฅ˜ ๋œธ
		cout << "turbo = " << turbo << endl; 
	}

};

void main() {
	sportCar s1;

	s1.speed = 100;
}


๋ถ€๋ชจ์ƒ์„ฑ์ž ๋ช…์‹œ์  ํ˜ธ์ถœ

 

Rectangle::rectangle(int x, int y, int w, int, y) : Shape(x, y) {
}