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

C#

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


<C#>


==람다식


-익명 메소드를 만든느 또 하나의 방법

-LISP언어






=cf) Ruby on Rails : 웹언어


=형식유추

-람다식의 매개 변수를 컴파일러가 유추해주는 기능









=

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

namespace LambdaExepress
{
    
class Program
    {

//람다식 - delegate 활용 (delegate 선언 필요 O)
        
delegate int Calculate(int a, int
 b);
        delegate 
void DoSomething();


         
static void Main(string[] args)
        {
            //delegate 활용
            Calculate calc1 = delegate(int a, int b)
            {
                
return a + b;
            };
            
int temp = calc1(35);
            Console.WriteLine(temp);


           
 //람다식(Lambda Expression) 활용
            Calculate calc2 = (a, b) => a - b;

            temp 
= calc2(34);
            Console.WriteLine(temp);

            
//람다구문(Statement Lambda) 활용
            DoSomething DoIt = () =>
            {
                Console.WriteLine(
"{0} + {1}= {2}"123
);
            };

            DoIt();

 

//람다식 - Func 델리게이트 : 반환형 O (delegate 선언 필요 X)          

         


             
//Func 델리게이트 활용 (인자 2 마지막 args는 반환,out)
            Func<intintint> calc3 = (x,y) => x * y;

            temp 
= calc3(23);
            Console.WriteLine(temp);

            
//Func 델리게이트 활용 (인자 1 마지막 args는 반환,out)
            Func<int, int> calc5 = (x) => x * 2;
            temp 
= calc5(2);
            Console.WriteLine(temp);


//람다식 - Action 델리게이트 : 반환형 x (delegate 선언 필요 X)
            
//Action 반환형 없음
            Action<doubledouble> calc4 = (x, y) =>
            {
                
double p = x / y;
                Console.WriteLine(
"Action<T1,T2>({0},{1}:{2:f3}) "
, x, y, p);
            };


            calc4(
7,3);
        }
 
    }
    
class Myclass
    {
        Program pr1
= new Program();
    }

}






=Action 델리게이트

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

namespace ActionDelegate
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//매개변수 없는 버전
            Action act1 = () => Console.WriteLine("Action()");

            act1();

            
int result = 0;
            
//매개변수 1개
            Action<int> act2 = (x) => result = x * x;


            act2(
3);            
            Console.WriteLine("result : {0}", result);


              
//매개변수 2개... ~ 16개
            Action
<doubledouble> act3 = (x, y) =>
            {
                
double pi = x / y;
                Console.WriteLine(
"Action<T1, T2>({0}, {1}) : {2}"
, x, y, pi);
            };

            act3(
22.07.0);
        }
    }
}






=문형식의 람다식









=Func 델리게이트


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

namespace FuncTest
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Func
<int> func1 = () => 10;
            Console.WriteLine(
"func1() : {0}"func1());

            Func
<intint> func2 = (x) => x * 2;
            Console.WriteLine(
"func2(4) : {0}"func2(4));

            Func
<doubledoubledouble> func3 =
                (x, y) =>
 x / y;
            Console.WriteLine(
"func3(22/7) : {0}"func3(227));
        }
    }
}







= 식트리  ( LINQ :C#코드가 아님 => 실행 중 컴파일 해서 사용 가능 하도록 함)

-EXPRESSION :

-팩토리 메소드

-람다 : (a, b) => 1*2 + (a-b) : (a, b)는 1*2 + (a-b)로 이동(=> : move) 시킨다.


=

using System;
using System.Linq.Expressions;

namespace UsingExpressionTree
{
    
class Program
    {
        
/*
        static void Main(string[] args)
        {
            //1*2+(x-y)
            Expression const1 
= Expression.Constant(1);
            Expression const2 
= Expression.Constant(2);

            Expression leftExp 
= Expression.Multiply(const1, const2); // 1 * 2

            Expression param1 
= Expression.Parameter(typeof(int)); // x
            Expression param2 
= Expression.Parameter(typeof(int)); // y

            Expression rightExp 
= Expression.Subtract(param1, param2); // x - y
            Expression exp 
= Expression.Add(leftExp, rightExp);

            Expression
<Func<int, int, int>> expression =
                Expression<Func<int, int, int>>.Lambda<Func<int, int, int>>(
                    exp, new ParameterExpression[]{
                        (ParameterExpression)param1,
                        (ParameterExpression)param2});

            Func
<int, int, int> func = expression.Compile();

            //x 
= 7, y = 8
            Console.WriteLine("1 * 2 + ({0}-{1}) 
= {2}", 7, 8, func(7, 8));
        }
        *///람다식으로 변경
        static void Main(string[] args)
        {
            
Expression<Func<intintint>> expression =
                (a, b) => 1 * 2 + (a - b);
            
            Func
<intintint> func =
 expression.Compile();

            
//x = 7, y = 8
            Console.WriteLine("1 * 2 + ({0}-{1}) = {2}"78func(78));
        }
    }
}






=구글 내 위치 API => 코드를 줄수는 없음 / 필요한 사람이 원하는 곳에 넣을 때 EXPRESS를 이용해서 코드를 가리키는 위임자를 만들어 사용)



=연습문제


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

namespace LamdaTest
{
    
class Program
    {
        
/*
        static void Main(string[] args)
        {
            int[] array 
= { 11, 22, 33, 44, 55 };

            foreach (int a in array)
            {
                Action action 
= new Action
                (
                    delegate()
                    {
                        Console.WriteLine(a*a);
                    }
                );
                action.Invoke();
            }


        }*/

         //같은 결과 되도록 람다식으로 수정

        static void Main(string[] args)
        {
            
int[] array = { 1122334455 };

            
Func<int, string> test =
                (x) => (x * x).ToString();

            
            foreach (
int a in array)
            {                
                Console.WriteLine(
test(a));
            }

        }
    }
}





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

C++

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

<C++>



=다중상속의 문제점

class A

{

        int a;

}

class B public A

class C : public A

class D : public B, public C

{

        a = 3 // 어느 a인지 알 수 없음

};

:다이아몬드 계층도


=> 해결법

1. :: 연산자 사용

-B::a

-C::a


2. vitual

=가상기반 클래스

class B : virtual public A

class C : vittual public A

class D : public B, public C

{

};


=포함


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

class Date
{
    
protected:
        
int year, month, day;
    
public:
        Date(
int y, int m, int d)
        {
            year 
= y;
            month 
= m;
            day 
= d;
        }
        
void OutDate()
        {
            cout 
<< year << "/" << month << "/" << day << endl;
        }
};

class Product
{
    
private:
        
char Name[64];
        
char Company[32];
        
Date ValidTo;
        
int Price;
    
public:
        Product(
const char * aN, const char * aC, int y, int m, int d, int aP) : ValidTo(y, m, d)
        {
            strcpy(Name, aN);
            strcpy(Company, aC);
            Price 
= aP;
        }
        
void OutProduct()
        {
            cout 
<< "이름    :" << Name << endl;
            cout 
<< "제조사  :" << Company << endl;
            cout 
<< "유효기간:";
            ValidTo.OutDate();
            
//puts("");
            cout << "가격    :" << Price << endl;
               
        }
};



int main()
{
    Product S(
"새우깡""농심"2009815900);
    S.OutProduct();
        
return 0;
}



=private 상속


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

class Date
{
    
protected:
        
int year, month, day;
    
public:
        Date(
int y, int m, int d)
        {
            year 
= y;
            month 
= m;
            day 
= d;
        }
        
void OutDate()
        {
            cout 
<< year << "/" << month << "/" << day << endl;
        }
};

class Product : private Date
{
    
private:
        
char Name[64];
        
char Company[32];
       
 //Date ValidTo;
        int Price;
    
public:
        Product(
const char * aN, const char * aC, int y, int m, int d, int aP) : Date(y, m, d)
        {
            strcpy(Name, aN);
            strcpy(Company, aC);
            Price 
= aP;
        }
        
void OutProduct()
        {
            cout 
<< "이름    :" << Name << endl;
            cout 
<< "제조사  :" << Company << endl;
            cout 
<< "유효기간:";
            OutDate();
            
//puts("");
            cout << "가격    :" << Price << endl;
               
        }
};



int main()
{
    Product S(
"새우깡""농심"2009815900);
    S.OutProduct();
        
return 0;
}



-결과 동일






=두개 이상의 Date 정보(제조일자, 유효기간) 사용 해야 할 경우 => 포함 O / private 상속 X


-포함

Date MunuFact;

Date ValidTo;


-private 상속

class Product : private Date, private Date // 에러


=




=인터페이스 상속(선언) / 구현 상속(정의)


순수 가상 함수       : 가상함수 이나, 함수의 정의부분이 없고, 선언 부분만 있는 함수
                        :      모든 파생 클래스마다 동일한 역활을 하는 
고유의 동작을 필요로 하게 될 때

                        : 다형성
가상 함수             : 파생 클래스에서 가상함수를 받는 함수가 없다면,

                                기본 클래스 함수가 호출되고
                      있다면,

                                파생 클래스의 가상 함수를 호출시켜주는 매체가 되는 함수

                        : 모든 파생 클래스마다 동일한 역활을 하는 일반적인 동작을 필요로 하게 될 때

비 가상 함수 : 일반 멤버 함수, 
                :모든 파생 클래스마다 동일한 역활을 하는 
절대적인 동작을 필수로 하게 될 때




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

20150618  (0) 2015.06.18
20150617  (0) 2015.06.17
20150615  (0) 2015.06.16
20150612  (0) 2015.06.14
20150611  (0) 2015.06.11
Posted by ahj333
,