==========================================================================================

C#

==========================================================================================

<C#>


==일반화 메소드

= 함수 사용 시 type(int...string...)을  형식 매개 변수로 보냄







-배열 복사 메소드






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20150610
{
    
class Program
    {
        
static void CopyArray<T>(T[] source, T[] target)
        {
            
for(int i = 0; i < source.Length; i++)
            {
                target[i] 
= source[i];
            }
        }
        
static void Main(string[] args)
        {
            
int[] source = { 12345 };
            
int[] target = new int[source.Length];

            CopyArray
<int>(source, target);

            foreach(
int element in target)
                Console.WriteLine(element);

            string[] source2 
= {"하나""둘""셋""넷""다섯"};
            string[] target2 
= new string[source2.Length];

            CopyArray
<string>(source2, target2);

            foreach(string element in target2)
                Console.WriteLine(element);
        }
    }
}






=일반화 클래스





using System;
using System.Collections.Generic;

namespace GenericClass
{
    
class MyList<T>
    {
        
private T[] array;

        
public MyList()
        {
            array 
= new T[3];
        }

        
public T this[int index]
        {
            get
            {
                
return array[index];
            }

            set
            {
                
if(index >= array.Length)
                {
                    Array.Resize
<T>(ref array, index + 1);
                    Console.WriteLine(
"Array Resized : {0}", array.Length);
                }
                array[index] 
= value;
            }
        }
        
public int Length
        {
            get
            {
                
return array.Length;
            }
        }
    }


    
class Program
    {
        
static void Main(string[] args)
        {
            MyList
<string> str_list = new MyList<string>();
            str_list[
0= "abc";
            str_list[
1= "def";
            str_list[
2= "ghi";
            str_list[
3= "jkl";
            str_list[
4= "mno";

            
for (int i = 0; i < str_list.Length; ++i)
                Console.WriteLine(str_list[i]);

            Console.WriteLine();

            MyList
<int> int_list = new MyList<int>();
            int_list[
0= 0;
            int_list[
1= 1;
            int_list[
2= 2;
            int_list[
3= 3;
            int_list[
4= 4;

            
for (int i = 0; i < int_list.Length; ++i)
                Console.WriteLine(int_list[i]);
        }
    }
}







=형식 매개변수 제약





-U는 T의 베이스

: 부모와 동일한 자식(부모가 INT면 자식도 INT)

( 인터페이스 부분 추가 필요)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConstraintsOnTypeParameters
{
    
/*
    interface InterfaceTest
    {
        void interface_test1();
    }

    interface InterfaceTest2
    {
        void interface_test2();
    }
    */

    class StructArray<T> where T:struct
    {
        
public T[] Array { get; set; }
        
public StructArray(int size)
        {
            Array 
= new T[size];
        }
    }

    
class RefArray<T> where T:class
    {
        
public T[] Array { get; set; }
        
public RefArray(int size)
        {
            Array 
= new T[size];
        }
    }

    
class Base 
    {
        
public string name{set;get;}
        
public Base()
        {
            name 
= "BASE CLASS";
        }
    }

    
class Derived : Base 
    {
        
public Derived()
        {
            name 
= "DERIVED CLASS";
        }
    }

    
class BaseArray<U> where U : Base
    {
        
public U[] Array { get; set; }
        
public BaseArray(int size)
        {
            Array 
= new U[size];
        }
        
public void CopyArray<T>(T[] Source) where T : U
        {
            Source.CopyTo(Array, 
0);
        }
    }

    
class Program
    {
        
public static T CreateInstance<T>() where T : new()
        {
            
return new T();
        }

        
static void Main(string[] args)
        {
            
//InterfaceTestClass<interface> a = new InterfaceTestClass<InterfaceTest1>();

            StructArray<int> a = new StructArray<int>(3);
            a.Array[
0= 0;
            a.Array[
1= 1;
            a.Array[
2= 2;

            
for (int i = 0; i < a.Array.Length; ++i)
            {
                Console.WriteLine(a.Array[i]);
            }
            Console.WriteLine(
"===========================================\n");
            RefArray
<StructArray<double>> b = new RefArray<StructArray<double>>(3);
            b.Array[
0= new StructArray<double>(5);
            b.Array[
1= new StructArray<double>(10);
            b.Array[
2= new StructArray<double>(15);

            
for (int i = 0; i < b.Array.Length; ++i )
            {
                
for (int j = 0; j < b.Array[i].Array.Length; ++j )
                    b.Array[i].Array[j] 
= (i+1)*j;
            }

            
for (int i = 0; i < b.Array.Length; ++i)
            {
                
for (int j = 0; j < b.Array[i].Array.Length; ++j )
                    Console.WriteLine(b.Array[i].Array[j]);
                Console.WriteLine();
            }
            Console.WriteLine(
"===========================================\n");
            BaseArray 
<Base> c = new BaseArray<Base>(3);
            c.Array[
0= new Base();
            c.Array[
1= new Derived();
            c.Array[
2= CreateInstance<Base>();
            
//c.Array[2] = CreateInstance<StructArray<int>(3)>();

            for(int i = 0; i < c.Array.Length; ++i)
            {
                Console.WriteLine(c.Array[i].name);
            }
            
//foreach (string str in c)
            //    Console.WriteLine("{0} ", str);
            Console.WriteLine("===========================================\n");
            BaseArray
<Derived> d = new BaseArray<Derived>(3);            
            d.Array[
0= new Derived(); // Base 형식은 여기에 할당 할 수 없다
            d.Array[1= CreateInstance<Derived>();
            d.Array[
2= CreateInstance<Derived>();
            
//d.Array[3] = new Base();

            for (int i = 0; i < c.Array.Length; ++i)
            {
                Console.WriteLine(d.Array[i].name);
            }
            Console.WriteLine(
"===========================================\n");
            BaseArray
<Derived> e = new BaseArray<Derived>(3);
            e.CopyArray
<Derived>(d.Array);

            
for (int i = 0; i < c.Array.Length; ++i)
            {
                Console.WriteLine(e.Array[i].name);
            }
            Console.WriteLine(
"===========================================\n");
        }
    }
}






=List<T>

using System;
using System.Collections.Generic;

namespace UsingGenericList
{
    
class Program
    {
        
static void Main(string[] args)
        {
            List
<int> list = new List<int>();
            
for (int i = 0; i < 5; ++i)
                list.Add(i);

            foreach (
int element in list)
                Console.Write(
"{0} ", element);
            Console.WriteLine();

            list.RemoveAt(
2);

            foreach (
int element in list)
                Console.Write(
"{0} ", element);
            Console.WriteLine();

            list.Insert(
22);

            foreach (
int element in list)
                Console.Write(
"{0} ", element);
            Console.WriteLine();
        }
    }
}






=Queue<T>

using System;
using System.Collections.Generic;

namespace UsingGenericQueue
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Queue
<int> queue = new Queue<int>();

            queue.Enqueue(
1);
            queue.Enqueue(
2);
            queue.Enqueue(
3);
            queue.Enqueue(
4);
            queue.Enqueue(
5);

            
while (queue.Count > 0)
                Console.WriteLine(queue.Dequeue());
        }
    }
}




=Stack<T>

using System;
using System.Collections.Generic;

namespace UsingGenericStack
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Stack
<int> stack = new Stack<int>();

            stack.Push(
1);
            stack.Push(
2);
            stack.Push(
3);
            stack.Push(
4);
            stack.Push(
5);

            
while (stack.Count > 0)
                Console.WriteLine(stack.Pop());
        }
    }
}





=Dictionary<TKey, TValue>


using System;
using System.Collections.Generic;

namespace UsingDictionary
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Dictionary
<string, string> dic = new Dictionary<string, string>();

            dic[
"하나"] = "one";
            dic[
"둘"= "two";
            dic[
"셋"= "three";
            dic[
"넷"= "four";
            dic[
"다섯"= "five";

            Console.WriteLine(dic[
"하나"]);
            Console.WriteLine(dic[
"둘"]);
            Console.WriteLine(dic[
"셋"]);
            Console.WriteLine(dic[
"넷"]);
            Console.WriteLine(dic[
"다섯"]);

        }
    }
}





=foreach 사용 할 수 있는 일반화 클래스


using System;
using System.Collections;
using System.Collections.Generic;

namespace EnumerableGeneric
{
    
class MyList<T> : IEnumerable<T>, IEnumerator<T>
    {
        
private T[] array;
        
int position = -1;

        
public MyList()
        {
            array 
= new T[3];
        }

        
public T this[int index]
        {
            get
            {
                
return array[index];
            }

            set
            {
                
if(index >=array.Length)
                {
                    Array.Resize
<T>(ref array, index + 1);
                    Console.WriteLine(
"Array Resized : {0}", array.Length);
                }

                array[index] 
= value;
            }
        }
        
public int Length
        {
            get
            {
                
return array.Length;
            }
        }

        
public IEnumerator<T> GetEnumerator()
        {
            
for(int i = 0; i<array.Length;i++)
            {
                yield 
return (array[i]);
            }
        }

        
//System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        IEnumerator IEnumerable.GetEnumerator()
        {
            
for(int i = 0; i<array.Length;i++)
            {
                yield 
return
 (array[i]);
            }
        }


        
public T Current
        {
            get
            {
                
return array[position];
            }
        }

        object IEnumerator.Current
        {
            get
            {
                
return array[position];
            }
        }

        
public bool MoveNext()
        {
            
if(position == array.Length -1)
            {
                Reset();
                
return false;
            }
            position++;
            
return (position < array.Length);
        }

        
public void Reset()
        {
            position 
= -1;
        }
        
public void Dispose()
        {

        }
    }
    
class Program
    {
        
static void Main(string[] args)
        {

            MyList
<string> str_list = new MyList<string>();
            str_list[
0= "abc";
            str_list[
1= "def";
            str_list[
2= "ghi";
            str_list[
3= "jkl";
            str_list[
4= "mno";

            foreach(string str in str_list)
                Console.WriteLine(str);

            Console.WriteLine();

            MyList
<int> int_list = new MyList<int>();
            int_list[
0= 0;
            int_list[
1= 1;
            int_list[
2= 2;
            int_list[
3= 3;
            int_list[
4= 4;

            foreach(string no in str_list)
                Console.WriteLine(no);            
        }
    }
}







==예외처리


-try

-catch

-finally


using System;

public class MainApp
{
    
public static void Main()
    {
         
try
         {
        Console.WriteLine(
"여기는 try 블록 시작");   
        
int zero = 0;
        
int j = 3/zero; // 예외 발생!         
        Console.WriteLine("여기는 try 블록 끝");
         }         
         
catch(Exception e)
         {
        Console.WriteLine(
"예외 발생 : {0}", e.Message);
         }
       finally
       {
        Console.WriteLine(
"여기는 finally 블록"); 
       }
    }
}








=에러 정보


using System;

class Division
{
   
public void Divide()
   {
      
try
      {
       
int zero = 0;
       
int j = 3/zero; // 예외 발생!         
      }
      
catch(Exception e)
      {
        Console.WriteLine(
"예외 발생 : {0}"e.Message);
      Console.WriteLine(
"예외가 발생한 위치 : {0}"e.StackTrace);
      Console.WriteLine(
"예외의 종류 : {0}", e.GetType());
      Console.WriteLine(
"에러를 일으킨 객체 : {0}", e.Source);
      Console.WriteLine(
"InnerException : {0}", e.InnerException);
      Console.WriteLine(
"TargetSite : {0}", e.TargetSite);   
      }
   }
}



==========================================================================================

C++

==========================================================================================

<C++>


=

-파일분할 시

inline 멤버함수 : 헤더파일 ( C의 매크로 함수와 마찬가지 - 소스코드에 바로 들어감 ( 호출 X))

-보통 CLASS안에 정의






=CONST함수

----const


=왠만하면 const함수로 만들기 ( const 객체는 const 함수만 사용 가능)





=멤버 함수 포인터

class smart

{

        void test();

};


void smart::test()

{

        return;

}


int main()

{

         void (smart::*f)(); // test()멤버함수 포인터 f

}


#include <iostream>
using namespace std;

class smart
{
    
public:
        
void test()
        {
            
return;

        }
};

int main()
{
    
void (smart::*f)();
    
return 0;
}



=

C책 P387

http://itsmart333.tistory.com/186


int *A = new int[3];

delete []A;

int **B = new int*[3];

delete []B;

int *A = &B;

int (*C)[3] = new int[2][3]

delete []C;






=car class 코딩하기




'2015 스마트 콘트롤러 > 업무일지' 카테고리의 다른 글

20150612  (0) 2015.06.14
20150611  (0) 2015.06.11
20150609  (0) 2015.06.09
20150608  (0) 2015.06.09
20150605  (0) 2015.06.06
Posted by ahj333
,