==========================================================================================
C#
==========================================================================================
<C#>
== 메소드
- 일련의 코드를 하나의 이름으로 묶은 것 ( == 함수)
- 객체의 데이터를 처리하는 방법을 추상적인 개념으로 만든 것 ( 객체 없는 메소드는 없다 )
- 매개변수
:메소드 외부에서 메소드 내부로 데이터를 전달하는 매개체역할
- 참조에의한 매개변수 전달
: ef (c에서 & / *)
:객체에서는 네임화하기 때문에(child of child....) 더블 포인터 사용 할 일이 없음
- 복수개의 return
: ref
- 출력 전용 매개 변수
: out
-메소드의 오버로딩
:하나의 메소드 이름에 여러개의 구현(매개변수 type,개수에 따라 다른 함수로 인식/호출 함)
-가변길이 매개변수
:params type[] args ( 매개변수의 type은 같고 개수가 유연하게 변해야 할 때 사용)
-명명된 매개변수
-선택적 매개변수(cf - 오버로딩: 코드 작성시 개수만큼 리스트 보임)
: int a = 0, int b = 0 => 생성 됨
(cf - int a, int b => 값이 없으므로 생성되지 않음 + 함수내에서 사용 하지 않으면 컴파일 에러 발생)
: 호출 시
MyMethod(3) ;
MyMethod(3, 4);
= ref, out 사용 X => 값 변경 X (- main()의 값 변경 X)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication2 { class Program { public static void Swap(int a, int b) { int temp = a; a = b; b = temp; Console.WriteLine("swap() - a: {0}, b:{1}", a, b); } static void Main(string[] args) { int a = 3; int b = 4;
Console.WriteLine("main() - a: {0}, b:{1}", a, b); Swap(a, b); Console.WriteLine("main() - a: {0}, b:{1}", a, b);
} } } |


= ref : main() 값 변경 O
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace REF { class Program { public static void Swap(ref int a, ref int b) { int temp = a; a = b; b = temp; Console.WriteLine("swap() - a: {0}, b:{1}", a, b); } static void Main(string[] args) { int a = 3; int b = 4;
Console.WriteLine("main() - a: {0}, b:{1}", a, b); Swap(ref a, ref b); Console.WriteLine("main() - a: {0}, b:{1}", a, b); } } }
|


= out : 복수 값 return
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace OUT { class Program { public static void Swap(int a, int b, out int quotient, out int remainder) { quotient = a / b; remainder = a % b; } static void Main(string[] args) { int a = 20; int b = 3; int c = 0; int d = 0;
Console.WriteLine("main() - a: {0}, b:{1}", a, b); Swap(a, b, out c, out d); Console.WriteLine("main() - c: {0}, d:{1}", c, d);
} } } |


=오버로딩
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace OVERLOADING { class Program { static int plus(int a, int b) { Console.WriteLine("Calling int plus(int a, int b)..."); return a + b; }
static int plus(int a, int b, int c) { Console.WriteLine("Calling int plus(int a, int b, int c)..."); return a + b + c; }
static double plus(double a, double b) { Console.WriteLine("Calling double plus(double a, double b)..."); return a + b; }
static double plus(int a, double b) { Console.WriteLine("Calling double plus(int a, double b)..."); return a + b; }
static void Main(string[] args) { Console.WriteLine(plus(1, 2)); Console.WriteLine(plus(1, 2, 3)); Console.WriteLine(plus(1.0, 2.4)); Console.WriteLine(plus(1, 2.4));
} } } |




=
1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EX1
{
class Program
{
static int sum(int mton, int eng, int math)
{
return mton + eng + math;
}
static double avg(int sum)
{
return sum / 3.0;
}
static string grd(double avg)
{
if(avg > 90)
{
return "A";
}
else if (avg > 80)
{
return "B";
}
else if (avg > 70)
{
return "C";
}
else
return "F";
}
static void exit()
{
string chk;
while(true)
{
chk = Console.ReadLine();
if(chk == "x")
break; ;
if(chk == "X")
break;
}
return;
}
static void Main(string[] args)
{
Console.Write("학생 성명을 입력하세요 --> ");
string name = Console.ReadLine();
Console.Write("국어 성적을 입력하세요 --> ");
int mton = int.Parse(Console.ReadLine());
Console.Write("영어 성적을 입력하세요 --> ");
int eng = int.Parse(Console.ReadLine());
Console.Write("수학 성적을 입력하세요 --> ");
int math = int.Parse(Console.ReadLine());
Console.Write("\n");
Console.WriteLine("{0}의 종합점수는 --> {1}", name, sum(mton, eng, math));
Console.WriteLine("{0}의 평균점수는 --> {1:.00}", name, avg(sum(mton, eng, math)));
Console.WriteLine("{0}의 학점은 --> {1}", name, grd(avg(sum(mton, eng, math))));
Console.WriteLine("종료하시려면 'X or x'를 입력하세요. ");
exit();
}
}
}
2.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace EX1 { class Program { static void grd(int mton, int eng, int math, out int sum) { sum = mton + eng + math; }
static void grd(int sum, out double avg) { avg = sum / 3.0; }
static void grd(double avg, out string grd) { if(avg > 90) { grd = "A"; } else if (avg > 80) { grd = "B"; } else if (avg > 70) { grd = "C"; } else grd = "F"; }
static void exit() { string chk; Console.WriteLine("종료하시려면 'X or x'를 입력하세요. "); while(true) { chk = Console.ReadLine();
if(chk == "x") break; ;
if(chk == "X") break; } return; }
static void input(ref string name, ref int mton, ref int eng, ref int math ) { Console.Write("학생 성명을 입력하세요 --> "); name = Console.ReadLine(); Console.Write("국어 성적을 입력하세요 --> "); mton = int.Parse(Console.ReadLine()); Console.Write("영어 성적을 입력하세요 --> "); eng = int.Parse(Console.ReadLine()); Console.Write("수학 성적을 입력하세요 --> "); math = int.Parse(Console.ReadLine()); Console.Write("\n"); }
static void output(string name, int sum, double avg, string grd) { Console.WriteLine("{0}의 종합점수는 --> {1}", name, sum);//sum(mton, eng, math)); Console.WriteLine("{0}의 평균점수는 --> {1:.00}", name, avg);//avg(sum(mton, eng, math))); Console.WriteLine("{0}의 학점은 --> {1}", name, grd);//grd(avg(sum(mton, eng, math)))); } static void Main(string[] args) { string name=""; int mton=0; int eng=0; int math=0; input(ref name, ref mton, ref eng, ref math);
int i_sum; double d_avg; string s_grd;
grd(mton, eng, math, out i_sum); grd(i_sum, out d_avg); grd(d_avg, out s_grd);
output(name, i_sum, d_avg, s_grd);
exit(); } } } |


==========================================================================================
C++
==========================================================================================
<C++>
=상수 초기화 ( 상수 아닌 것도 초기화 리스트로 초기화 가능하나 잘 사용하지 않는다)
-c 고대문법 차용
void test()
int A, int B
{
}
#include <iostream> using namespace std;
class smart { public: const int a; // = 100; const int b; int c; // 초기화 리스트로 초기화 가능 //사용 잘 안함 smart() :a(100),b(200),c(300) // 상수 초기화 { //a = 100; // main() - obj1.a = 100 과 같은 효과 }
smart(int x) :a(x),b(200),c(300) // 상수 초기화 { }
smart(int x, int y) :a(x),b(y),c(300) // 상수 초기화 { }
smart(int x, int y, int z) :a(x),b(y),c(z) // 상수 초기화 { }
~smart() {
} };
int main() { //obj1.a = 100; smart obj1; cout << "obj1.a : " << obj1.a << endl; cout << "obj1.b : " << obj1.b << endl; cout << "obj1.c : " << obj1.c << endl; smart obj2(20); cout << "obj2.a : " << obj2.a << endl; cout << "obj2.b : " << obj2.b << endl; cout << "obj2.c : " << obj2.c << endl; smart obj3(20,30); cout << "obj3.a : " << obj3.a << endl; cout << "obj3.b : " << obj3.b << endl; cout << "obj3.c : " << obj3.c << endl; smart obj4(20,30,40); cout << "obj4.a : " << obj4.a << endl; cout << "obj4.b : " << obj4.b << endl; cout << "obj4.c : " << obj4.c << endl; return 0; } |


-defualt 생성자에서 상수 초기화 없을시 컴파일 에러
:a(100)//,b(200),c(300) // 상수 초기화 // 반드시 const 초기화 필요-컴파일 에러 |
=레퍼런스 멤버 초기화
-레퍼런스 변수는 만들 때 한번만 초기화 가능=> 예외적으로 함수의 형식인수, extern 선언시, class멤버인 경우 초기화리스트로 초기화 가능
#include <iostream> using namespace std;
class Some { public: int &ri; Some(int &i):ri(i){} // 레퍼런스 멤버 초기화 void OutValue() { cout << ri << endl; }
};
int main() { int i = 5; Some S(i); S.OutValue(); return 0; } |


=포함된 객체 초기화
#include <iostream> using namespace std;
class Position { public: int x,y; Position(int ax, int ay) { x = ax; y = ay; } };
class Some { public: Position Pos; Some(int x, int y):Pos(x,y){} void OutValue() { cout << Pos.x << " , " << Pos.y << endl; } }; int main() { Some S(3, 4); S.OutValue(); return 0; } |


=터보c
환경변수 - path 추가
chcp 437
ALT N