==========================================================================================
C#
==========================================================================================
<C#>
=객체 지향 프로그래밍과 클래스
-객체 지향 프로그래밍(Object Oriented Programming)
=클래스
-붕어빵틀 : 클래스, 붕어빵 : 객체
-int a = 3 => int : 클래스, a: 객체
-객체 = 인스턴스(instance), 청사진의 실체
-객체의 속성과 기능은 클래스 안에 변수와 메소드로 표현
-클래스(복합데이터 형식), 기본 데이터 형식을 조합해서 만드는 사용자 정의 데이터 형식
-객체의 인스턴스화 => 메모리에 로드되었다(new사용)
=자동차 공장
Car
-Color
-Wheel
-Model
-drive()
-brake()
=C1.Color / C2.Color /C3....
=C1.Wheel / C2.Wheel / ....
=상속
-Toco
-Sonata
==코드로..

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace _20150602 { class Program { static void Main(string[] args) { Car C1 = new Car(); // C1: 붕어빵 Car(): 생성자 Car C2 = new Car(); C1.Color = "RED"; // COLOR, WHEEL, MODEL => 속성 C1.Wheel = "4"; C1.Model = "TICO"; C1.drive(); // C1이 drive - 메소드 //drive(); // 함수 메소드 X
C2.Color = "WHITE"; C2.Wheel = "2"; C2.Model = "SONATA"; C2.drive(); C2.brake();
tico t1 = new tico(); t1. } }
class Car // 붕어빵 틀 - TEMPLATE { public string Color; public string Wheel; public string Model; public void drive(string k) { //this.Wheel = k; // 만들어진 객체 : this }
public void brake() { }
}
class tico : Car // Car를 상속 받겠다 {
}
} |
-상속
:오버라이딩(재정의)
(cf: 오버로딩 (다중정의))
-인터페이스 : class Car와 같이 껍데기만(실행내용이 없음) 있는 것

=데이터 => 속성 ==> 데이터 IN/OUT 할수 있는 형태로 진화(생성자 오버로딩)
=생성자
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace _20150602 { class Program { static void Main(string[] args) { Car C1 = new Car(); // C1: 붕어빵 Car(): 생성자 Car C2 = new Car(); Car C3 = new Car("tico", "4", "red"); C1.Color = "RED"; // COLOR, WHEEL, MODEL => 속성 C1.Wheel = "4"; C1.Model = "TICO"; C1.drive(); // C1이 drive - 메소드 //drive(); // 함수 메소드 X
C2.Color = "WHITE"; C2.Wheel = "2"; C2.Model = "SONATA"; C2.drive(); C2.brake();
tico t1 = new tico(); t1. } }
class Car // 붕어빵 틀 - TEMPLATE { public string Color; public string Wheel; public string Model; public Car() // default 생성자(다른 생성자가 없는 경우 명시적으로 선언하지 않아도 암시적으로 생성자 제공) {
}
public Car(string model, string wheel, string color) // 생성자 ( 매개변수 O) { this.Color = color; this.Wheel = wheel; this.Model = medel; }
public void drive(string k) { //this.Wheel = k; // 만들어진 객체 : this }
public void brake() { } // 명시적으로 선언하지 않아도 암시적으로 소멸자 제공 - 굳이 선언 필요 없음 - 가비지 컬렉터가 객체 소멸 처리 ~Car() { Console.Write("Car 객체 소멸"); }
}
class tico : Car // Car를 상속 받겠다 {
}
} |
=정적 필드와 메소드
-필드 : 변수 + 속성
-메소드
=static 필드 : 제일 처음 한번만 메모리 할당
=메모리 절약(인스턴스 마다 할당 되지 않음)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace _20150602 { class Program { static void Main(string[] args) { Car C1 = new Car(); // C1: 붕어빵 Car(): 생성자 Car C2 = new Car(); Car C3 = new Car("tico", "4", "red"); C1.Color = "RED"; // COLOR, WHEEL, MODEL => 속성 C1.Wheel = "4"; C1.Model = "TICO"; C1.drive(C1.Wheel); // C1이 drive - 메소드 //drive(); // 함수 메소드 X
C2.Color = "WHITE"; C2.Wheel = "2"; C2.Model = "SONATA"; C2.drive(C2.Wheel); C2.brake();
tico t1 = new tico(); Car.cc = "4000"; } }
class Car // 붕어빵 틀 - TEMPLATE { public string Color; public string Wheel; public string Model; public static string cc; // 이 공장에서 만들어진 모든 객체에 공통적으로 적용되는 경우 static public Car() // default 생성자(다른 생성자가 없는 경우 명시적으로 선언하지 않아도 암시적으로 생성자 제공) {
}
public Car(string model, string wheel, string color) // 생성자 ( 매개변수 O) { this.Color = color; this.Wheel = wheel; this.Model = model; }
public void drive(string k) { //this.Wheel = k; // 만들어진 객체 : this }
public void brake() { } // 명시적으로 선언하지 않아도 암시적으로 소멸자 제공 - 굳이 선언 필요 없음 - 가비지 컬렉터가 객체 소멸 처리 ~Car() { Console.Write("Car 객체 소멸"); }
}
class tico : Car // Car를 상속 받겠다 {
}
} |

-
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace _20150602_2 { class Global { public static int Count = 0; }
class ClassA { public ClassA() { Global.Count++; } }
class ClassB { public ClassB() { Global.Count++; } }
class Program { static void Main(string[] args) { Console.WriteLine("Global.Count : {0}", Global.Count);
new ClassA(); new ClassA(); new ClassB(); new ClassB();
Console.WriteLine("Global.Count : {0}", Global.Count); } } }
|


==============================================================================================
=Class STUDENT
{
//국어 영어 필드
//총점 메소드
//평균 메소드
//학점 메소드
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace _20150602_2 { class STUDENT { public string name; public int mton; public int eng; public int math;
public static int grd(STUDENT stu) // 총점 { return stu.mton + stu.eng + stu.math; }
public static double grd(int sum) // 평균 { return sum / 3.0; }
public static string grd(double avg) // 학점 { string grd;
if (avg > 90) { grd = "A"; } else if (avg > 80) { grd = "B"; } else if (avg > 70) { grd = "C"; } else grd = "F";
return grd; } }
class Program { static void Main(string[] args) { STUDENT stu1 = new STUDENT(); input(stu1); output(stu1, STUDENT.grd(stu1), STUDENT.grd(STUDENT.grd(stu1)), STUDENT.grd(STUDENT.grd(STUDENT.grd(stu1)))); exit(); }
static void input(STUDENT stu) // 입력 { Console.Write("학생 성명을 입력하세요 --> "); stu.name = Console.ReadLine(); Console.Write("국어 성적을 입력하세요 --> "); stu.mton = int.Parse(Console.ReadLine()); Console.Write("영어 성적을 입력하세요 --> "); stu.eng = int.Parse(Console.ReadLine()); Console.Write("수학 성적을 입력하세요 --> "); stu.math = int.Parse(Console.ReadLine()); Console.Write("\n"); }
static void output(STUDENT stu, int sum, double avg, string grd) // 출력 { Console.WriteLine("{0}의 종합점수는 --> {1}", stu.name, sum); Console.WriteLine("{0}의 평균점수는 --> {1:.00}", stu.name, avg); Console.WriteLine("{0}의 학점은 --> {1}", stu.name, grd); }
static void exit() // 종료 { string chk; Console.WriteLine("종료하시려면 'X or x'를 입력하세요. "); while (true) { chk = Console.ReadLine();
if (chk == "x") break;
if (chk == "X") break; } }
} } |
==============================================================================================
=조각찾기(윈도우(이벤트위주-user), 웹, 모바일...)
=DB
= dll hell
-dll upgrade시 그 dll 공유하는 다른 프로그램이 동작안하는 경우 발생
-window진화하면서 -> dll버전 지원 -> 어느정도 해결
==========================================================================================
C++
==========================================================================================
<C++>
=형변환
-묵시적
float A = 3;
-명시적
float A =(float)3;
#include <iostream> using namespace std;
class Time { private: int hour, min, sec; public: Time(){} Time(int abssec) { hour = abssec/3600; min = (abssec/60)%60; sec = abssec%60; }
void OutTime() { cout << "현재 시간은 " << hour << ":" << min << ":" << sec << " 입니다\n"; } };
int main() {
Time Now(3723); Now.OutTime(); return 0; } |


=
#include <iostream> using namespace std;
class Time { private: int hour, min, sec; public: Time(){} Time(int abssec) { hour = abssec/3600; min = (abssec/60)%60; sec = abssec%60; }
void OutTime() { cout << "현재 시간은 " << hour << ":" << min << ":" << sec << " 입니다\n"; }
};
void func(Time When) { When.OutTime(); }
int main() {
Time Now(3723); //Now.OutTime(); func(Now); func(1234); return 0; } |


=엄격한 타입 체크
explicit - 묵시적 타입 캐스팅 금지




-명시적 타입 캐스팅은 허용




=객체는 c언어 구조체형 초기화 불가능
smart obj = { 100, "김일성“} // 불가능
Time Now = 3723;
-생성자에서 초기화
Time Now(3723);
Time Now = (Time)3723;
==캡슐화
-은닉
(abs...)
-기능 단위 X => 개체단위 O (조립 가능 => 유지보수 쉬움)
=
c++ : 메모리 자체 보호 기능은 없음
Student Kim();
char *p = (char *)&Kim;
=
private 변수 접근 금지 => 함수를 통해서만 가능하도록
printf("%d\n", Kim.stNum); // 금지
printf("%d\n", Kim.GetStNum()); // 허용
#include <iostream> #include <math.h> #include <stdio.h> //#include <unistd.h> //#include <termios.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <conio.h> //#include <sys\select.h>
using namespace std;
#define min(A,B) ((A) > (B) ? (B):(A)) #define max(A,B) ((A) > (B) ? (A):(B)) #define delay(A) for(uiCnt = 0 ; uiCnt < (A); ++uiCnt);
class Car { private: int Gear; int Angle; int Rpm; public: Car() { Gear = 0; Angle = 0; Rpm = 0; }
void ChangeGear(int aGear) { if (aGear >= 0 && aGear <= 6) { Gear = aGear; } }
void RotateWheel(int Delta) { int tAngle = Angle + Delta; if (tAngle >= -45 && tAngle <45) { Angle = tAngle; } }
void Accel() { Rpm = min(Rpm + 100, 30000); }
void Break() { Rpm = max(Rpm - 500, 0); }
void Run() { int Speed; //char Mes[128]; // gotoxy(10,12); if (Gear == 0) { cout << "먼저 1~6키를 눌러 기어를 넣으시오 "; return; }
if (Gear == 6) { Speed = Rpm / 100; } else { Speed = Gear*Rpm / 100; }
cout << abs(Speed) << "의 속도로" << (Angle >= 0 ? "오른" : "왼") << "쪽 " << abs(Angle) << "도 방향으로 " << (Gear == 6 ? "후" : "전") << "진중 \n"; } };
int main() { Car C; int ch=100;
volatile unsigned int uiCnt = 0;
for (;;) { // gotoxy(10,10); cout << "1~5: 기어변속, 6:후진 기어, 0:기어중립\n"; // gotoxy(10,11); cout << "위:엑셀, 아래:브레이크, 좌우:핸들, Q:종료 \n"; //cin >> ch; //cout << "ch : " << ch << endl; if (_kbhit()) ch = _getch();
if (ch == 0xE0 || ch == 0) { ch = _getch(); switch (ch) { case 75: C.RotateWheel(-5); break; case 77: C.RotateWheel(5); break; case 72: C.Accel(); break; case 80: C.Break(); break; } } else { if (ch >= '0' && ch <= '6') { C.ChangeGear(ch - '0'); } else if (ch == 'Q' || ch == 'q') { exit(0); } } C.Run(); delay(1000); }
return 0; }
|

