C++

安裝

  1. Dev-C++ link
  2. vscode link

特性

  1. 編譯
  2. 物件導向

變數

類型 空間 range
bool 1 bit (0 to 1 )or (true to false)
int 2 Bytes(2*8 bit) -2^15 to 2^15-1
long int 4 Bytes(4*8 bit) -2^23 to 2^23-1
long long int 8 Bytes(4*8 bit) -2^31 to 2^31-1
float 4 Bytes(4*8 bit)
doblue 8 Bytes(8*8 bit)

int

#include <stdio.h>

int main()
{   
   int var_name;
   int w2,ww;
   var_name=4;
   w2=1;
   ww=42342;
   
}

float

float test=10;
float var= 0.003;

double

double test=10;
double var= 0.003;

陣列(array)

//宣告

int mo={0,1,2,3};

int num[4],mo[4];
float num[4],mo[4];
double num[4],mo[4];
char num[4],mo[4];

//儲存資料
mo[0]=0;
mo[1]=1;
mo[2]=2;
mo[3]=3;

//讀取資料


printf("%.3f", mo[3]);


輸入 輸出

printf

//int
printf("%d",a);
//float or double
printf("%lf",a);
//string
printf("%s",a);

//輸出


//特殊字元
//換行
printf("\n");	
//tab
printf("\t");
//\
printf("\\");



scanf

int a;
scanf("%d",&a);
float b;
scanf("%lf",&b);
char c[] = "some text";
scanf("%s",&c);

if

//判別式
a<b
a>b
a==b

int a ,b;
a=2;
b=3;
if(a>b)
{
	printf("%d big", a);
}
else
{
	printf("%d big", b);
}

迴圈

for

#include <stdio.h>
int main()
{
    int i;
    for (i = 0; i < 100; i++)
    {
        printf("%d\n", i);
    }
}

while

#include <stdio.h>
int main()
{
    int i=0;
    while(i<10)
    {
        printf("%d\n", i);
        i=i+1;
    }
    return 0;
}

function

return

int test_function(int a,int b)
{   
   int var;
   var=a*b;
   return var;
   
}
int something = test_function(4,8)

void

void test(int a)
{
    printf("%d",a);

}
int main()
{

    int var;
    var = 12;
    test(var);

    return 0;
}

call by value

void test(int a)
{
    printf("a1 %d\n", a);
    a = 10;
    printf("a2 %d\n", a);
    //10
}

int main()
{
    int var = 8;
    printf("va1 %d\n", var);

    test(var);
    printf("va2 %d\n", var);
    return 0;
}
//8

call by reference

void test(int &a)
{
    printf("a1 %d\n", a);
    a = 10;
    printf("a2 %d\n", a);
    //10
}

int main()
{
    int var = 8;
    printf("va1 %d\n", var);

    test(var);
    printf("va2 %d\n", var);
    return 0;
}
//10

call by address

void test(int* a)
{
	printf("a1 %d\n", a);
	printf("a1_v %d\n", *a);
	*a = 10;
	printf("a2 %d\n", a);
	printf("a2_v %d\n", *a);
}

int main()
{
    int var = 8;
    printf("va1 %d\n", var);

    test(&var);
    printf("va2 %d\n", var);
    return 0;
}
//10

class (物件導向)

basic

public 可以讀取
private 不可讀取

class test {
    public:
        int a=10;
        int b;
        int DoSomething();
        void printf(int a)
        {
            printf("%d",test::a);
        }
    private:
        int size;
        string color;
};

int test::DoSomething() {
    return a + b;
}

int main(){
   test d;
   printf("%d",d.a);
}

constructor,destructor

class test {
    public:
        int m_a=10;
        int m_b;
        int DoSomething();
    
        //constructer
        test(){
            m_a=0;
            m_b=1;
        }
        //constructer 
        //initialize list: constructerwill initialize child var before constructer
        test(int a,int b):m_a(a),m_b(b){}
        //destructor
        ~test(){
            //destructor will call when the var is going to be remove
            delete m_a;
            delete m_b;
        }
}
int main(){
    test d1();
    test d2;
    printf("%d",d1.a);
}

copy constructer

class test {
    public:
        int m_a=10;
        int m_b;
        //call when the another need copy of the value  
        test(const test &set_test){
            this->m_a=set_test.m_a;
            this->m_b=set_test.m_b;
        }
}
int main(){
    test d1(1,2);
    test d2=d2;
    printf("%d",d2.a);
}

copy assignment

class test {
    public:
        int m_a=10;
        int m_b;
        //call when var need to be overwrite by another var
        //so need to free the child var

        //"conset" mean this funciton will not change the input argement var
        //"&test mean" return the reference of the var
        //"&set_test" mean input the reference of the var
        &test operator =(const test &set_test){
            delete this->m_a;
            delete this->m_b;
            this->m_a=set_test.m_a;            
            this->m_b=set_test.m_b;
            return *this
        }
}
int main(){
    test d1(1,2);
    test d2(3,4);
    d1=d2;
    printf("%d",d1.a);
}

惡夢開始

::的意義

全域變數

int tem=0;
void main(){
::tem=::tem+10;
}

和外定義class的資料

class test {
    public:
        int a=10;
        int b;
        int something();
};
int test::something(){
	printf(this->a);
}

.

System::Math::Sqrt()=System.Math.Sqrt()

this

class test {
    public:
        int a=10;
        int b;
        int something();
};
int test::something(){
	printf(this->a);
}

->

struct a{
    int v=10;
    int b;
};
int something(a *a_ptr){
	printf(a_ptr->v);
	printf(*(a_ptr).v);
}
Select a repo