==========================================================================================
C#
==========================================================================================
<C#>
=simple calculator
1. 객체 (method 활용)
2. 주요 핵심 로직을 클래스 라이브러리(.dll) - + / - / * / /
dll활용해서
console / winform /webform
<C>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace _20150527 { class Program { static void Main(string[] args) { Console.WriteLine("========================================================"); Console.WriteLine("===================simple calculator===================="); Console.Write("Num1 ==>"); string s1 = Console.ReadLine();
Console.Write("Num2 ==>"); string s2 = Console.ReadLine();
Console.Write("OP( +,-,*,/ )==>"); string op = Console.ReadLine();
int i1 = int.Parse(s1); int i2 = int.Parse(s2); int total;
switch (op) { case "+": total = i1 + i2; Console.Write(total); break; case "-": Console.Write(i1 - i2); break; case "*": Console.Write(i1 * i2); break; case "/": Console.Write(i1 / i2); break; default: Console.Write("Error\n"); break; }
} } }
|
<method 활용>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace _20150528_2 { class Program { static void Main(string[] args) { Console.WriteLine("========================================================"); Console.WriteLine("===================simple calculator===================="); Console.Write("Num1 ==>"); string s1 = Console.ReadLine();
Console.Write("Num2 ==>"); string s2 = Console.ReadLine();
Console.Write("OP( +,-,*,/ )==>"); string op = Console.ReadLine();
switch(op) { case("+"): Console.WriteLine(plus(s1, s2)); break; case ("-"): Console.WriteLine(minus(s1, s2)); break; case ("*"): Console.WriteLine(multiply(s1, s2)); break; case ("/"): Console.WriteLine(divide(s1, s2)); break; } }
static int plus(string n1, string n2) { return int.Parse(n1) + int.Parse(n2); }
static int minus(string n1, string n2) { return int.Parse(n1) - int.Parse(n2); }
static int multiply(string n1, string n2) { return int.Parse(n1) * int.Parse(n2); }
static int divide(string n1, string n2) { return int.Parse(n1) / int.Parse(n2); } } }
|
1. opLib.dll

=class name 변경



using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace opLib { public class oplib { public static int plus(string n1, string n2) { return int.Parse(n1) + int.Parse(n2); }
public static int minus(string n1, string n2) { return int.Parse(n1) - int.Parse(n2); }
public static int multiply(string n1, string n2) { return int.Parse(n1) * int.Parse(n2); }
public static int divide(string n1, string n2) { return int.Parse(n1) / int.Parse(n2); } } }
|


2. op_win
-단일 솔루션내 프로젝트 추가



=컴파일 에러

=op_win을 시작 프로젝트로

=form 만 실행

=레퍼런스 추가

=(단일 솔루션) - 단일 머신내의 프로젝트라 바로 찾음 ( 원격도 가능 : 찾아야 함)

= form의 각 버튼 더블클릭

-using opLib; 추가


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using opLib; // opLib.oplib.plus() => oplib.plus()..
namespace op_win { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { textBox3.Text = oplib.plus(textBox1.Text, textBox2.Text).ToString(); }
private void button2_Click(object sender, EventArgs e) { textBox3.Text = oplib.minus(textBox1.Text, textBox2.Text).ToString(); }
private void button3_Click(object sender, EventArgs e) { textBox3.Text = oplib.multiply(textBox1.Text, textBox2.Text).ToString(); }
private void button4_Click(object sender, EventArgs e) { textBox3.Text = oplib.divide(textBox1.Text, textBox2.Text).ToString(); } } } |
=실행결과




=op_web


using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using opLib;
namespace op_web { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
} protected void Button1_Click(object sender, EventArgs e) { TextBox3.Text = oplib.plus(TextBox1.Text, TextBox2.Text).ToString(); }
protected void Button2_Click(object sender, EventArgs e) { TextBox3.Text = oplib.minus(TextBox1.Text, TextBox2.Text).ToString(); }
protected void Button3_Click(object sender, EventArgs e) { TextBox3.Text = oplib.multiply(TextBox1.Text, TextBox2.Text).ToString(); }
protected void Button4_Click(object sender, EventArgs e) { TextBox3.Text = oplib.divide(TextBox1.Text, TextBox2.Text).ToString(); } } } |
=실행결과




=op_con
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using opLib;
namespace op_con { class Program { static void Main(string[] args) { Console.WriteLine("========================================================"); Console.WriteLine("===================simple calculator===================="); Console.Write("Num1 ==>"); string s1 = Console.ReadLine();
Console.Write("Num2 ==>"); string s2 = Console.ReadLine();
Console.Write("OP( +,-,*,/ )==>"); string op = Console.ReadLine();
int i1 = int.Parse(s1); int i2 = int.Parse(s2); int total;
switch (op) { case "+": Console.WriteLine(oplib.plus(s1, s2).ToString()); break; case "-": Console.WriteLine(oplib.minus(s1, s2).ToString()); break; case "*": Console.WriteLine(oplib.multiply(s1, s2).ToString()); break; case "/": Console.WriteLine(oplib.divide(s1, s2).ToString()); break; default:
break; } } } } |
=실행결과


==데이터 보관하기
=데이터 type => 메모리 ,체계, ...
-(Primitive Data Type) 기본데이터 : int, double, decimal...
-(Complex Data Type) 복합데이터 : string, Socket, Image, 클래스, 메소드, 객체, 배열...
= 형식
-값 : value
-참조 : address (위치) - 데이터의 공유를 위하여
=변수(VARIABLE)
-데이터 선언, 할당
( int x; // 메모리 할당 x
x = 100; // 메모리 할당 o )
=STACK : 자동 휘발성(기억공간의 소유권 박탈) LIFO
=HEAP : 명시필요(쓰레기 데이터는 가비지 콜렉터가 수거해감)- 강제로 지시
=BOXING / UNBOXING
-BOXING : 값 형식 데이터를 상자에 담아 힙에 올려놓고, 그 힙의 위치를 OBJECT형식 변수가 가리키도록 하는 것
-UNBOXING : 힙안에 올라가 있는 상자를 풀어 값 형식 데이터를 꺼내는 것
-OBJECT : 참조 -> 주소 => 아무 형식의 데이터 들어와도 가능 (들어오는 데이터 타입이 불명확 할 경우 사용)
object a =20; //
(꺼낼 때 TYPE을 알아내거나 명시해서 꺼냄)
int b = (int)a;
=

=OVERFLOW
=OBJECT : 부모 - 모든 데이터 타입 가질 수 있음 ( BOXING )
==========================================================================================
C++
==========================================================================================
<C++>
= 전역/지역 변수
a => 지역 변수
::a => 전역 변수
#include <iostream> using namespace std;
int a = 100;
int main() { int a = 200; cout << a << endl; cout << ::a << endl; return 0; } |


=
-STRUCT - 기본 PUBLIC
-CLASS - 기본 PRIVATE
=캡슐화
=객체 생성 -> 자료형만 가지고 있음
-함수는 코드 영역에 따로 만들어짐
=Position A;
인스턴스 == 오브젝트
실체 객체
= P57
#include <iostream> using namespace std;
class Time { private: int hour, min, sec;
public: void SetTime(int h, int m, int s) { hour = h; min = m; sec = s; }
void OutTime() { cout << "현재 시간은 " << hour << ":" << min << ":" << sec << " 입니다\n";
} };
int main(void) { Time Now;
Now.SetTime(12, 30, 40); Now.OutTime(); return 0; } |


=
-걷는다 : 배경 바꿈..
=BOOL (논리회로 BOOL 대수)
true / false
-warning을 띄우는 것이 원래 맞음..
#include <iostream> using namespace std;
int main() { bool a;
cout <<"bool size : " << sizeof(a) << endl; a = true; cout << "a = true : " << a << endl; cout << "true : " << true << endl; cout << "false : " << false << endl; a = 2; cout << "a = 2 : " << a << endl;
return 0; } |


==생성자
:객체가 생성 될 때 자동으로 호출되는 함수
(반환형 X)
#include <iostream> using namespace std;
class smart { public: void test() { cout << "test\n"; }
smart() { test(); } };
int main() { smart obj1; smart obj2; smart obj3; // obj1.test();
return 0; } |


= 생성자 private: => 에러 => 특수한 경우 사용
#include <iostream> using namespace std;
class smart { public: void test() { cout << "test\n"; } private: // ERROR 발생 smart() { test(); } };
int main() { smart obj1; smart obj2; smart obj3; // obj1.test();
return 0; } |


=생성자의 다형성0
#include <iostream> using namespace std;
class smart { public:
int iNum; void test() { cout << "test\n"; } // private: smart() { iNum = 0; test(); } };
int main() { smart obj1; smart obj2; smart obj3; // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl; return 0; } |


=생성자의 다형성1
=인자 없는 생성자 :default 생성자
=인자 가진 생성자
#include <iostream> using namespace std;
class smart { public: int iNum; void test() { cout << "test\n"; } // private: smart() // 인자 없는 생성자 : default 생성자 { iNum = 0; test(); } smart(int a) { iNum = a; test(); } };
int main() { smart obj1; // default 생성자 호출 smart obj2(100); // 인자 int a 가진 생성자 호출 smart obj3; // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl; return 0; }
|


=생성자의 다형성2
#include <iostream> using namespace std;
class smart { public: int iNum; void test() { cout << "test\n"; } // private: smart() // 인자 없는 생성자 : default 생성자 { iNum = 0; test(); } smart(int a) { iNum = a; test(); }
smart(int a, int b) { iNum = a + b; test(); } };
int main() { smart obj1; // default 생성자 호출 smart obj2(100); // 인자 int a 가진 생성자 호출 smart obj3(100, 200); // 인자 int a, int b 가진 생성자 호출 // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl; return 0; }
|


=DEFAULT 생성자
-private: - 특수한 경우 사용
1. 특정한 생성자를 사용 못하도록 할 때
2. 반드시 초기값 넣도록 할 때
#include <iostream> using namespace std;
class smart { public: int iNum; void test() { cout << "test\n"; } // private: // ERROR 발생
smart(int a) { iNum = a; test(); }
smart(int a, int b) { iNum = a + b; test(); } private: smart() // 인자 없는 생성자 : default 생성자 { iNum = 0; test(); } };
int main() { smart obj1; // default 생성자 호출 smart obj2(100); // 인자 int a 가진 생성자 호출 smart obj3(100, 200); // 인자 int a, int b 가진 생성자 호출 // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl; return 0; } |


=
생성자(인자 있는)가 하나라도 있는 경우
default 생성자를 반드시 만들어야 함
#include <iostream> using namespace std;
class smart { public: int iNum; void test() { cout << "test\n"; } // private: // ERROR 발생
smart(int a) { iNum = a; test(); }
smart(int a, int b) { iNum = a + b; test(); } // private: smart() // 인자 없는 생성자 : default 생성자 { iNum = 0; test(); } };
class smart2 { public: int iNum;
smart2(int a) { cout << "smart2\n"; }
}; int main() { smart obj1; // default 생성자 호출 smart obj2(100); // 인자 int a 가진 생성자 호출 smart obj3(100, 200); // 인자 int a, int b 가진 생성자 호출 // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl;
smart2 obj4(100); //smart2 obj5;
return 0; } |


=

#include <iostream> using namespace std;
class smart { public: int iNum; void test() { cout << "test\n"; } // private: // ERROR 발생
smart(int a) { iNum = a; test(); }
smart(int a, int b) { iNum = a + b; test(); } // private: smart() // 인자 없는 생성자 : default 생성자 { iNum = 0; test(); }
};
class smart2 { public: int iNum;
smart2(int a) { cout << "smart2\n"; }
}; int main() {
smart(700); // 임시객체 // 이 자리에서 생성/소멸 smart obj1; // default 생성자 호출 smart obj2(100); // 인자 int a 가진 생성자 호출 smart obj3(100, 200); // 인자 int a, int b 가진 생성자 호출 // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl;
smart2 obj4(100); smart2 obj5;
cout << "========================================\n"; return 0; } |


#include <iostream> using namespace std;
class smart { public: int iNum; void test() { cout << "test\n"; } // private: // ERROR 발생
smart(int a) { iNum = a; test(); }
smart(int a, int b) { iNum = a + b; test(); } // private: smart() // 인자 없는 생성자 : default 생성자 { iNum = 0; test(); } };
class smart2 { public: int iNum; /* smart2(int a) { cout << "smart2\n"; } */
}; int main() { smart obj1; // default 생성자 호출 smart obj2(100); // 인자 int a 가진 생성자 호출 smart obj3(100, 200); // 인자 int a, int b 가진 생성자 호출 // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl;
smart2 obj4(100); smart2 obj5;
return 0; } |


=생성자는 하나도 없고 멤버에 변수만 있으면
컴파일러가 자동으로 default 생성자 만들어줌

#include <iostream> using namespace std;
class smart { public: int iNum; void test() { cout << "test\n"; } // private: // ERROR 발생
smart(int a) { iNum = a; test(); }
smart(int a, int b) { iNum = a + b; test(); } // private: smart() // 인자 없는 생성자 : default 생성자 { iNum = 0; test(); } };
class smart2 { public: int iNum; /* smart2(int a) { cout << "smart2\n"; } */
}; int main() { smart obj1; // default 생성자 호출 smart obj2(100); // 인자 int a 가진 생성자 호출 smart obj3(100, 200); // 인자 int a, int b 가진 생성자 호출 // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl;
//smart2 obj4(100); smart2 obj5;
return 0; }
|


= 소멸자 : return 전 소멸 / 소멸자는 단 하나임
스택 구조의 특성 : 생성 순서의 반대로 소멸


=임시객체
#include <iostream> using namespace std;
class smart { public: int iNum; void test() { cout << "test\n"; } // private: // ERROR 발생
smart(int a) { iNum = a; test(); }
smart(int a, int b) { iNum = a + b; test(); } // private: smart() // 인자 없는 생성자 : default 생성자 { iNum = 0; test(); }
~smart() // 소멸자 { cout << "소멸자 호출됨" << iNum << endl; } };
class smart2 { public: int iNum;
smart2(int a) { cout << "smart2\n"; }
}; int main() {
smart(700); // 임시객체 // 이 자리에서 생성/소멸 smart obj1; // default 생성자 호출 smart obj2(100); // 인자 int a 가진 생성자 호출 smart obj3(100, 200); // 인자 int a, int b 가진 생성자 호출 // obj1.test(); cout << obj1.iNum << endl; cout << obj2.iNum << endl; cout << obj3.iNum << endl;
smart2 obj4(100); //smart2 obj5;
cout << "========================================\n"; return 0; } |

