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

C#

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


<C#>


 ==LINQ ( Language INtergrated Query )

:c# 언어에 통합된 데이터 질의 기능


-From  : 어떤 데이터 집합 ?

-Where : 어떤 값 데이터 ?

-Select :어떤 항목 추출 ?


=from 범위변수 in 데이터 원본(반드시 IEnumerable<T> 인터페이스)

=where : 필터 ( 조건 )

=orderby : 정렬 ( ascending / descending )

=select : 추출 - IEnumverable < T >  T : select 문에의해 결정


=


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

namespace LINQ_EX
{
    
class Profile
    {
        
public string Name { get; set; }
        
public int Height { get; set; }
    }
    
class Program
    {
        
static void Main(string[] args)
        {
          
 // 원본 데이터 : 반드시 IEnumerable 인터페이스 
            Profile[] arrProfile = {
                                       
new Profile(){Name="정우성", Height = 186},
                                       
new Profile(){Name="김태희", Height = 158},
                                       
new Profile(){Name="고현정", Height = 172},
                                       
new Profile(){Name="이문세", Height = 178},
                                       
new Profile(){Name="하동훈", Height = 171},
                                   };


             
// 일반 - 키가 175 미만인 사람 오름차순 정렬
            List
<Profile> profiles = new List<Profile>();
            foreach(Profile P in arrProfile)
            {
                
if (P.Height < 175)
                    profiles.Add(P);
            }

            profiles.Sort(
                (profile1, profile2) 
=>
                {
                    
return profile1.Height - profile2.Height;
                });

             // 출력
            foreach (var p in profiles)
                Console.WriteLine(
"{0}, {1}", p.Name, p.Height);

            
// LINQ로 구현
            Console.WriteLine("\nLINQ");
            
var Lprofiles = from p in arrProfile       // arrProfile 원본 데이터로 부터
                           where p.Height 
< 175    // Heifht 가 175미만인 객체만 골라
                           orderby p.Height         // Height 기본 오름차순 (ASCENDING) 정렬
                           select p;                 // p객체를 추출

             // 출력
            foreach (var p in Lprofiles)
                Console.WriteLine(
"{0}, {1}", p.Name, p.Height);

        }
    }
}







=여러개의 데이터 원본에 질의

:from 중첩 사용


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

namespace FromFrom
{
    
class Class
    {
        
public string Name { get; set; }
        
public int[] Score { get; set; }
    }

    
class Program
    {
        
static void Main(string[] args)
        {
            Class[] arrClass 
=
            {
                
new Class(){Name = "연두반", Score = new int[]{99807024}},
                
new Class(){Name = "분홍반", Score = new int[]{60458772}},
                
new Class(){Name = "파랑반", Score = new int[]{92308594}},
                
new Class(){Name = "노랑반", Score = new int[]{9088017}},
            };

            var classed 
=   from c in arrClass
                            from s in c.Score
                                where s
<60
                                orderby s
                            select 
new { c.Name, Lowest =
s};

            foreach(var c in classed)
                Console.WriteLine(
"낙제 : {0} ({1})", c.Name, c.Lowest);
        }
    }
}






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

C++

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

<C++>



=중첩 클래스


#include <iostream>
using namespace std;

class Outer
{
    
private:
        
class inner
        {

             private:
                
int memA;
            
public:
                inner(
int a) : memA(a)
                {
                }
                
int GetA()
                {
                    
return memA;
                }
        }
obj;
    
public:
        Outer(
int a) :obj(a)
        {
        }
        
void OutOuter()
        {
            cout 
<< "멤버 값 = " << obj.GetA() << endl;
        }
};

int main()
{
    
Outer O(345);
    
//Inner I(678);   // 에러

    O.OutOuter();
    
return 0;
}






-

#include <iostream>
using namespace std;

void func()
{
    
struct dummy
    {
        
static void localfunc()
        {
            cout 
<< "저는 func 안에서만 정의됩니다.\n";
        }
    };
    
dummy::localfunc();
}

int main()
{
    func();
   
//localfunc()   // 에러
    return 0;
}











-cf) SQL (Stuructured Query(질의) Language) - 데이터 베이스에 던지는 질의언어

-오라클

-MSSQL

-MYSQL



==다형성


=가상함수

:객체 기반으로 호출 가능

:C++은 main()가 class에 속해있지 않음


-함수 동적 링킹(컴파일시 X) => 실행시 완성(코드 분석 힘들다)



(자바 : DEFAULT로 OBJECT CLASS 상속) => OBJECT * => 모두 가리킬 수 있음


=

#include <iostream>
using namespace std;

class CAR
{
    
public:
        
void print() 
        {
            cout 
<< "나는 CAR CLASS다\n";
        }
};

class BMW : public CAR
{
    
public:
        
void print() // 오버라이딩
        {
            cout 
<< "나는 BMW CLASS다\n";
        }
};

int main()
{
    BMW obj;
    
BMW *bp = &obj;
    
bp->print(); // bp에의해 결정 => BMW
    CAR *cp = &obj;
    
cp->print(); // cp에의해 결정 => CAR

    return 0;
}








=파생클래스의 클래스포인터(bp)는 기반클래스의 객체(obj1)를 가리킬 수 없다


#include <iostream>
using namespace std;

class CAR
{
    
public:
        
void print() 
        {
            cout 
<< "나는 CAR CLASS다\n";
        }
};

class BMW : public CAR
{
    
public:
        
void print() // 오버라이딩
        {
            cout 
<< "나는 BMW CLASS다\n";
        }
};


int main()
{
    BMW obj;
    BMW *bp 
= &obj;
    bp-
>print(); // bp에의해 결정 => BMW
    CAR *cp = &obj;
    cp-
>print(); // cp에의해 결정 => CAR
//    CAR obj1;
//    bp 
= &obj1; // 에러

    return 0;
}





=


#include <iostream>
using namespace std;

class CAR
{
    
public:
        
void print() 
        {
            cout 
<< "나는 CAR CLASS다\n";
        }
};

class BMW : public CAR
{
    
public:
        
void print() // 오버라이딩
        {
            cout 
<< "나는 BMW CLASS다\n";
        }
};

class TICO : public CAR
{
    
public:
        
void print() // 오버라이딩
        {
            cout 
<< "나는 TICO CLASS다\n";
        }
};

int main()
{
    BMW obj;
    
BMW *bp = &obj;
    bp-
>print(); // bp에의해 결정 => BMW
    CAR *cp = &obj;
    cp-
>print(); // cp에의해 결정 => CAR
//    CAR obj1;
//    bp 
= &obj1; // 에러
    TICO obj2;
    
TICO * tp;
    tp-
>print(); // tp에의해 결정 => TICO

    
return 0;
}





=> 기반 클래스의 클래스 포인터는 파생 클래스의 OBJ를 가리킬 수 있다

#include <iostream>
using namespace std;

class CAR
{
    
public:
        
void print() 
        {
            cout 
<< "나는 CAR CLASS다\n";
        }
};

class BMW : public CAR
{
    
public:
        
void print() // 오버라이딩
        {
            cout 
<< "나는 BMW CLASS다\n";
        }
};

class TICO : public CAR
{
    
public:
        
void print() // 오버라이딩
        {
            cout 
<< "나는 TICO CLASS다\n";
        }
};

int main()
{
    BMW obj;
    BMW *bp 
= &obj;
//    bp->print(); 
    CAR *cp = &obj;
    
cp->print(); // cp에의해 결정 => CAR
//    CAR obj1;
//    bp 
= &obj1; // 에러
    TICO obj2;
    cp-
>obj2;
    
cp->print(); // cp에의해 결정 -> CAR
    TICO * tp;
    tp-
>print();

    
return 0;
}







=VIRTUAL 

=> 기반 클래스의 클래스 포인터는 파생 클래스의 OBJ를 가리킬 수 있다

=> 최종 가리킨 OBJ를 기반으로 호출


#include <iostream>
using namespace std;

class CAR
{
    
public:
        
virtual void print() // virtual => 최종 객체 기반으로 호출
        {
            cout 
<< "나는 CAR CLASS다\n";
        }
};

class BMW : public CAR
{
    
public:
        
void print() // 오버라이딩
        {
            cout 
<< "나는 BMW CLASS다\n";
        }
};

class TICO : public CAR
{
    
public:
        
void print() // 오버라이딩
        {
            cout 
<< "나는 TICO CLASS다\n";
        }
};

int main()
{
  
  BMW obj;
//    BMW *bp = &obj;
//    bp-
>print(); // bp에의해 결정 => BMW
    CAR *cp = &obj; // 파생 클래스의 객체 모두를 가리킬 수 있다
    cp-
>print(); // virtual => 최종객체 CP = BMW 에의해 결정 => BMW
//    CAR obj1;
//    bp 
= &obj1; // 에러
    TICO obj2;
//    TICO * tp;
//    tp-
>print();
    cp = &obj2;
    cp-
>print(); // virtual => 최종객체 CP = TICO 에의해 결정 => TICO
    
return 0;
}









=virtual


#include <iostream>
using namespace std;

class Base
{
    
public:
        virtual void OutMessage()
        {
            cout 
<< "Base Class\n";
        }
};

class Derived : public Base
{
    
public:
        virtual void OutMessage()
        {
            cout 
<< "Derived Class\n";
        }
};

void Message(Base *pB)
{
    pB-
>OutMessage();
}

int main()
{
    Base B;
    Derived D;


    Message(
&B);
    Message(
&D);

    
return 0;
}







=P219~연산자


=복합 대입 연산자

#include <iostream>
using namespace std;

class Time
{
    
private:
        
int hour, min, sec;

    
public:
        Time(){}
        Time(
int h, int m, int s)
        {
            hour 
= h;
            min 
= m;
            sec 
= s;
        }
        
void OutTime()
        {
            cout 
<< hour << ":" << min << ":" << sec << endl;
        }
        Time 
&operator +=(int s)
        {
            sec +
= s;
            min +
= sec/60;
            sec %
= 60;
            hour +
= min/60;
            min %
=60;
            
return *this
;
        }

};

int main()
{
    Time A(
1,1,1);

    A+
=62;
    A.OutTime();
    
return 0;
}








=[] 배열연산자


#include <iostream>
using namespace std;

class Time
{
    
private:
        
int hour, min, sec;
    
public:
        Time(){}
        Time(
int h, int m, int s)
        {
            hour 
= h;
            min 
= m;
            sec 
= s;
        }
        
void OutTime()
        {
            cout 
<< hour << ":" << min << ":" << sec << endl;
        }
        
int &operator [](int what)
        {
            
switch (what)
            {
                
case 0:
                    
return hour;
                
case 1:
                    
return min;
                
default:
                
case 2:
                    
return
 sec;
            }
        }

        
const int &operator [](int what) const
        {
            
switch (what)
            {
                
case 0:
                    
return hour;
                
case 1:
                    
return min;
                
default:
                
case 2:
                    
return sec;
            }
        }
};


int main()
{
    Time A(
1,1,1);
    
const Time B(7,7,7);

    A[
0]=12;
    cout 
<< "현재 " << A[0] << "시 입니다\n";
    
//B[0]=8; // 에러 const => 수정 불가 read only
    cout << "현재 " << B[0] << "시 입니다\n";
    
return 0;
}
















=[]


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

class StuList
{
    
private:
        
struct Student
        {
            
char Name[10];
            
int StNum;
        }S[
30];

    
public:
        StuList()
        {
            strcpy(S[
0].Name, "이승만");S[0].StNum=1;
            strcpy(S[
1].Name, "박정희");S[1].StNum=3;
            strcpy(S[
2].Name, "전두환");S[2].StNum=6;
            strcpy(S[
3].Name, "노태우");S[3].StNum=9;
            strcpy(S[
4].Name, "김영삼");S[4].StNum=15;
            strcpy(S[
5].Name, "김대중");S[5].StNum=17;
            strcpy(S[
6].Name, "노무현");S[6].StNum=20;
            strcpy(S[
7].Name, "??????");S[7].StNum=100;
        }
        
int operator[](const char * Name)
        {
            
for(int i=0;;++i)
            {
                
if(strcmp(S[i].Name,Name)==0)
                    
return S[i].StNum;
                
if(S[i].Name[0]=='?')
                    
return -1
;
            }
        }

};


int main()
{
    StuList SL;

    cout 
<< "김영삼 학생의 학번은 " << SL["김영삼"] <<" 번 입니다\n";
    
return 0;
}








= -> 멤버참조연산자

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

struct Author
{
    
char Name[32];
    
char Tel[24];
    
int Age;
};

class Book
{
    
private:
        
char Title[32];
        Author Writer;

    
public:
        Book(
const char *aTitle, const char * aName, int aAge)
        {
            strcpy(Title, aTitle);
            strcpy(Writer.Name, aName);
            Writer.Age
=aAge;
        }
        Author *
operator->()
        {
            
return &
Writer;
        }

        
const char *GetTitle()
        {
            
return Title;
        }
};

int main()
{
    Book Hyc(
"혼자 연구하는 C/C++","김상형",25);
    cout 
<< "제목: " << Hyc.GetTitle() <<"저자: " << Hyc->Name << "저자나이: " <<  Hyc->Age << endl;
    
return 0;
}







=()연산자


#include <iostream>
using namespace std;

class Sum
{
    
public:
        
int operator()(int a, int b, int c, int d)
        {
            
return a + b + c + d;
        }
        
double operator()(double a, double b)
        {
            
return
 a + b;
        }

};

int main()
{
    Sum S;
    cout 
<< "1 2 + 3 + 4 = " <<  S(1234<< endl;
    cout 
<< "1.2 + 3.4 = : " <<  S(1.23.4<< endl;
    
return 0;
}











-()

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

class ScoreManager
{
    
private:
        
// 성적을 저장하는 여러가지 멤버 변수들
        int ar[3][5][10][4];
    
public:
        ScoreManager()
        {
            memset(ar, 
0sizeof(ar));
        }
        
int &operator()(int Grade, int Class, int StNum, const char * Subj)
        {
            
return ar[Grade][Class][StNum][0
];
        }

};

int main()
{
    ScoreManager SM;

    cout 
<< "1학년 2반 3번 학생의 국어 성적 = " << SM(123"국어") << endl;
    SM(
234"산수"= 99;
    
return 0;
}









=new / delete


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

void *operator new(size_t t)
{
    
return malloc(t);
}


void operator delete(void *p)
{
    free(p);
}

int main()
{
    
int *pi = new int;
    *pi 
= 1234;
    cout 
<< *pi << endl;
    
delete pi;
    
return 0;
}











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

20150619  (0) 2015.06.21
20150618  (0) 2015.06.18
20150616  (0) 2015.06.16
20150615  (0) 2015.06.16
20150612  (0) 2015.06.14
Posted by ahj333
,