본문 바로가기
코스웨어/15년 스마트컨트롤러

20150527 - 19번 - 안향진 - C#,C++

by 알 수 없는 사용자 2015. 5. 28.
728x90
반응형

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

C#

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

<C#>

=서비스 어플리케이션


1. (heap : 1kbyte 미만의 주소만 보관 || stack : data(value) 보관) - 보통

2. (heap : value 보관 || stack : 주소 보관) - dynamic static 복합인 경우


-한정자 : access modifier 접근 - static - (메모리에 상주)

-반환 - return

-메소드 이름 - main

-매개 변수 - string[] args



=매서드 묶음 => 클래스

=클래스 묶음 => namespace

=namespace : 다수의 클래스를 블럭


=C#언어로 작성한 모든 프로그램은 CLR( : Common Language Runtime)위에서 동작함


어플리케이션 : c# | VB.NET | C++ ....

Common Language Runtime

.NET 프레임워크

운영체제(Windows, LINUX, MacOS X,...)


-운영체제에 이 파일들(DLL : 코드 덩어리)이 존재할 경우 프로그램(DLL에서 뽑아사용) 실행 가능







= (언어명세, TYPE, 조건 분기, 반복 )(데이터 처리..)(UI : WIN / CONSOLE / MOBILE..)


= IL (Intermediate Language ) : C# 컴파일러가 IL(중간어)로 번역

(CLR이 IL해석 가능 => 기계어로 변경 => 운영체제에 실행 요청)




= CTS(:Common Type System)

int I = 32; => CLR 통과 => System.Int32(CTS)


-오류처리 기능

-언어간의 상속

-COM과의 상호 운영

-자동 메모리 관리(Gargabe Collection : 사용하지 않는 메모리 해제(삭제가 아님))



= 계산기

첫 번째 숫자 12

두 번째 숫자 24

연산자(+,-,*,/) +

36


=> 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(
"첫번째 숫자");    
            
int num1 = Convert.ToInt32(Console.ReadLine());
            
//int num1 = int.Parse(Console.ReadLine());

            Console.WriteLine("두번째 숫자");    
            
int num2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(
"연산자( +,-,*,/ )");
            string op 
= Console.ReadLine();
            
//char c_op = Convert.ToChar(calc);

            switch (op)
            {
                
case "+":
                    Console.Write(num1 + num2);
                    
break;
                
case "-":
                    Console.Write(num1 - num2);
                    
break;
                
case "*":
                    Console.Write(num1 * num2);
                    
break;
                
case "/":
                    Console.Write(num1 / num2);
                    
break;
                
default:
                    Console.Write(
"Error\n");
                    
break;
            }
            
/*
            if ("+" 
== op)
                Console.Write(num1 + num2);
            else if ("-" 
== op)
                Console.Write(num1 - num2);
            else if ("*" 
== op)
                Console.Write(num1 * num2);
            else if ("/" 
== op)
                Console.Write(num1 / num2);
            else
                Console.Write("Error\n");
            */
        }
    }
}




=객체지향 : 코드 조각 | 조립 ...


=> 객체지향으로 수정



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

C++

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


<C++>


- c언어

#include <iostream>
using namespace std;

typedef struct _car
{
   
int iSpeed;
   
int iDir;
   
char cAcolor[256];
}Car;

void Show_Car(Car * cP)
{
    cout 
<< "CAR SPEED     : " << cP->iSpeed << endl;
    cout 
<< "CAR DIRECTION : " << cP->iDir << endl;
    cout 
<< "CAR COLOR     : " << cP->cAcolor << endl;
    
return;
}

void Set_Car_Speed(Car * cP) // 엑셀 
{
    
int iNum;

    cout 
<< "속도를 입력하시오\n";
    scanf(
"%d"&iNum);
    
if(110 < iNum)
    {
        iNum 
= 110;
    }
    
    
if(-60 > iNum) // 후진
    {
        iNum 
= -60;
    }

    cP-
>iSpeed = iNum;
    
return;
}

int main()
{
    Car myCar 
= {00"RED"};

    
    Show_Car(
&myCar);

    
return 0;
}


= 클래스 :  객체

붕어빵틀  : 붕어빵

   인간  : 김민정

   존재 x : 존재

  --> 인스턴스 구현

클래스의 인스턴스 : 객체



= type에 따라 함수를 따로 호출해줌 ( 같은 이름의 함수 가능- 인자의 type , 개수로 함수 구분) - 다형성 ( 명칭부호화를 통해 제공 )

= (smart()를 오버로딩 함)


#include <iostream>
using namespace std;

void smart(int A)
{
    cout 
<< "int    :" << A << endl;
    
return;
}

int main()
{
    smart(
100);
    smart(
5.1);

    
return 0;
}





#include <iostream>
using namespace std;

void smart(int A)
{
    cout 
<< "int    :" << A << endl;
    
return;
}

void smart(double A)
{
    cout 
<< "double :" << A << endl;
    
return;
}

int main()
{
    smart(
100);
    smart(
5.1);
    smart(
100,200);
    
return 0;
}






#include <iostream>
using namespace std;

void smart(int A,int B)
{
    cout 
<< "SUM    :" << A+B << endl;
    
return;
}

void smart(int A)
{
    cout 
<< "int    :" << A << endl;
    
return;
}

void smart(double A)
{
    cout 
<< "double :" << A << endl;
    
return;
}

int main()
{
    smart(
100);
    smart(
5.1);
    smart(
100,200);
    
return 0;
}







=short ? int ?.. => 컴파일러가 판단하지 못할 상황 만들지 말 것 ( short 변수 선언 후 호출시 short인자 함수 호출 됨)

#include <iostream>
using namespace std;

void smart(short A)
{
    cout 
<< "short  :" << A << endl;
    
return;
}

void smart(int A,int B)
{
    cout 
<< "SUM    :" << A+B << endl;
    
return;
}

void smart(int A)
{
    cout 
<< "int    :" << A << endl;
    
return;
}

void smart(double A)
{
    cout 
<< "double :" << A << endl;
    
return;
}

int main()
{
    smart(
100);
    smart(
5.1);
    smart(
100,200);
    
return 0;
}




=어셈블러는 같은 함수 이름 허용 안함 => 컴파일시 함수 이름을 바꿔줌



=중간파일 생성

g++ --save-temps -c main-3.cc





= 명칭부호화

숫자smart인자 (s / ii /  i / d) => 4종류 이름


<main-3.s>




= extern "C" : 이 함수는 C로 컴파일 하시오.

extern "C" void smart(int A, int B)


<test.c>

#include <stdio.h>
extern void smart(int,int);

int main()
{
    smart(
100, 200);
    
return 0;
}


<main-3.cc>

#include <iostream>
using namespace std;

void smart(short A)
{
    cout 
<< "short  :" << A << endl;
    
return;
}

extern "C" void smart(int A,int B)
{
    cout 
<< "SUM    :" << A+B << endl;
    
return;
}

void smart(int A)
{
    cout 
<< "int    :" << A << endl;
    
return;
}

void smart(double A)
{
    cout 
<< "double :" << A << endl;
    
return;
}


<main-3.s>






= 컴파일



=> test.o




=> main-3.o



=> g++로 링크해야함


=> ( C ) + ( C++ ) => *.o ( 기계어 )  +  *.o ( 기계어) => 실행파일 one 생성



=





=

<test.c>

#include <stdio.h>
extern void smart(double);

int main()
{
    smart(
5.1);
    
return 0;
}


<main-3.cc>

#include <iostream>
using namespace std;

void smart(short A)
{
    cout 
<< "short  :" << A << endl;
    
return;
}

void smart(int A,int B)
{
    cout 
<< "SUM    :" << A+B << endl;
    
return;
}

void smart(int A)
{
    cout 
<< "int    :" << A << endl;
    
return;
}

extern "C" void smart(double A)
{
    cout 
<< "double :" << A << endl;
    
return;
}




=<C> 구조체 => <C++> 변경

-구조체 멤버에 함수 추가 / 호출


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

typedef struct _car
{
    
int iSpeed;
    
int iDir;
    
char cAcolor[256];

    
void Show_Car()
    {
        cout 
<< "CAR SPEED     : " << iSpeed << endl;
        cout 
<< "CAR DIRECTION : " << iDir << endl;
        cout 
<< "CAR COLOR     : " << cAcolor << endl;
        
return;
    }

    
void Set_Car_Speed() // 엑셀
    {
        
int iNum;

        cout 
<< "속도를 입력하시오\n";
        scanf(
"%d"&iNum);
        
if(110 < iNum)
        {
            iNum 
= 110;
        }

        
if(-60 > iNum) // 후진
        {
            iNum 
= -60;
        }

        iSpeed 
= iNum;
        
return;
    }

}Car;


int main()
{
    Car myCar 
= {00"RED"};

    myCar.Set_Car_Speed();
    myCar.Show_Car(); 
// 인자 삭제 + 구조체

    return 0;
}




=전역변수의 동적 초기화


<C>

#include <stdio.h>

int a = 100// 정적 초기화 : 컴파일 시 초기화

int smart()
{
    
int iNum;
    scanf(
"%d"&iNum);
    
return iNum;
}

int c = smart(); // 컴파일 시  => 실행 시 => "C"에서 ERROR

int main()
{
    
int b = 200// 동적 초기화 : 실행 시 초기화


    return 0;
}



<C++>

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

int a = 100// 정적 초기화 : 컴파일 시 초기화

int smart()
{
    
int iNum;
    scanf(
"%d"&iNum);
    
return iNum;
}

int c = smart(); // 컴파일 시  => 실행 시 => "C++" 전역번수 동적 초기화 허용

int main()
{
    
int b = 200;
    cout 
<< "main()\n";
    
return 0;
}



=> smart()함수 먼저 호출됨( 전역 변수 c 값 셋팅) -> main() 호출됨 ==> 전역 변수 셋팅 중 프로그램 죽을 수 있음 : 주의






=입력

cin


#include <iostream>
using namespace std;

int main()
{
    
int iNum;
    cout 
<< "숫자를 입력하세요 ";
    cin 
>> iNum;
    cout 
<< "입력하신 숫자는 " << iNum << " 입니다\n";
    
return 0;
}




=구조체 변수 선언시 struct 생략 가능

#include <iostream>
using namespace std;

struct smart
{
    
int iNum;
};

int main()
{
    
struct smart a;
    smart b; 
// struct 생략 가능

    return 0;
}



=구조체 내부에 멤버함수 정의 : 인라인 속성(매크로)

소스코드가 그 자리에 삽입됨 => 속도 빠름 + 실행파일 덩치 커짐 + 메모리 많이 사용 ( 보통 3줄이하)

=> 무조건 HEADER(자료형, 함수 원형) 파일에 있어야 함


=구조체 멤버함수 본체 구조체 밖으로 이동 ( :: 이용)

=> OVERHEAD 발생 ( 속도 느림 ( 함수 호출...) + 실행파일 덩치 작음 + 메모리 적게 사용

=> 인라인으로 사용시 : inline 앞에 적어줌

=> CPP(::함수 본체)에 있어도 됨


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

typedef struct _car
{
    
int iSpeed;
    
int iDir;
    
char cAcolor[256];

    
void Show_Car();

    
void Set_Car_Speed(); // 엑셀

}Car;


void Car::Show_Car()
{
    cout 
<< "CAR SPEED     : " << iSpeed << endl;
    cout 
<< "CAR DIRECTION : " << iDir << endl;
    cout 
<< "CAR COLOR     : " << cAcolor << endl;
    
return;
}


void Car::Set_Car_Speed() // 엑셀
{
    
int iNum;

    cout 
<< "속도를 입력하시오\n";
    scanf(
"%d"&iNum);
    
if(110 < iNum)
    {
        iNum 
= 110;
    }

    
if(-60 > iNum) // 후진
    {
        iNum 
= -60;
    }

    iSpeed 
= iNum;
    
return;
}

int main()
{
    Car myCar 
= {00"RED"};

    myCar.Set_Car_Speed();
    myCar.Show_Car(); 
// 인자 삭제 + 구조체

    return 0;
}





=overhead ( 목적외 다른 모든 것 )


=데이터의 은닉 : 구조체 멤버함수에서만 구조체 내의 변수를 수정 가능


=접근 속성 :    private:          - 외부에서 접근 못함

                public:          - 아무나 접근 가능

                protected:      - 상속에서 배움(혈족만 보여줌)





= 컴파일 불가

-함수 호출 불가 => public

-초기값 설정x




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

typedef struct _car
{
    
private:
        
int iSpeed;
        
int iDir;
        
char cAcolor[256];
    
public:
        
void Show_Car();

        
void Set_Car_Speed(); // 엑셀

}Car;


void Car::Show_Car()
{
    cout 
<< "CAR SPEED     : " << iSpeed << endl;
    cout 
<< "CAR DIRECTION : " << iDir << endl;
    cout 
<< "CAR COLOR     : " << cAcolor << endl;
    
return;
}


void Car::Set_Car_Speed() // 엑셀
{
    
int iNum;

    cout 
<< "속도를 입력하시오\n";
    scanf(
"%d"&iNum);
    
if(110 < iNum)
    {
        iNum 
= 110;
    }

    
if(-60 > iNum) // 후진
    {
        iNum 
= -60;
    }

    iSpeed 
= iNum;
    
return;
}

int main()
{
//    Car myCar = {0, 0, "RED"};
     
Car myCar;

    myCar.Set_Car_Speed();
    myCar.Show_Car(); 
// 인자 삭제 + 구조체

    return 0;
}






=CLASS 형태


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

class Car
{
    
public:
        
int iSpeed;
        
int iDir;
        
char cAcolor[256];

        
void Show_Car();

        
void Set_Car_Speed(); // 엑셀

};


void Car::Show_Car()
{
    cout 
<< "CAR SPEED     : " << iSpeed << endl;
    cout 
<< "CAR DIRECTION : " << iDir << endl;
    cout 
<< "CAR COLOR     : " << cAcolor << endl;
    
return;
}


void Car::Set_Car_Speed() // 엑셀
{
    
int iNum;

    cout 
<< "속도를 입력하시오\n";
    scanf(
"%d"&iNum);
    
if(110 < iNum)
    {
        iNum 
= 110;
    }

    
if(-60 > iNum) // 후진
    {
        iNum 
= -60;
    }

    iSpeed 
= iNum;
    
return;
}

int main()
{
    
//Car = {0, 0, "RED"};
    Car my_Car;
    my_Car.iSpeed 
= 0;
    my_Car.iDir 
= 0;
    strcpy(my_Car.cAcolor,
"RED");
    my_Car.Set_Car_Speed();
    my_Car.Show_Car(); 
// 인자 삭제 + 구조체

    return 0;
}






= 변수 선언/ 초기화


int A = 100;

int A(100);


#include <iostream>
using namespace std;


int main()
{
    
int A(100);
    cout 
<< "A : " << A << endl;
    
return 0;
}


728x90