0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C++入門

0
Last updated at Posted at 2025-11-06

基本トピック

C++の概要

C++はC言語の機能や特徴を受け継ぎつつ表現力の向上にあたって下記を取り扱った言語です。

・手続き型プログラミング
・データ抽象
・オブジェクト指向プログラミング
・ジェネリックプログラミング

C++のコンパイラのバージョンの確認

UbuntuにはデフォルトでCとC++のコンパイラが入っており、下記のコマンドによってバージョンの確認を行うことができます。

$ gcc --version
$ g++ --version

サンプルコードの実行

c

hello.c
# include <stdio.h>

int main(void)
{
    printf("Hello World!\n");
    return 0;
}
$ gcc -o hello hello.c
$ ./hello

・実行結果

Hello World!

c++

hello.cpp
# include <iostream>

int main()
{
    std::cout << "Hello World!" << std::endl;
    return 0;
}
$ g++ -o hello hello.cpp
$ ./hello

・実行結果

Hello World!

C++の基本的な構文

配列

test_array.cpp
#include <iostream>


int main(int argc, char *argv[]) {
    int x[5] = {0, 1, 2, 3, 4};
    int y = x[1];
    
    std::cout << y << std::endl;
    std::cout << x[0] << std::endl;
}
$ g++ -o test_array test_array.cpp
$ ./test_array

・実行結果

1
0

構造体

C++

structure.cpp
#include <iostream>

struct{
  int levels = 5;
  char title[10] = "xxx";
} test;


int main(void)
{
  std::cout << test.levels << std::endl;
  std::cout << test.title << std::endl;
}
$ g++ -o structure structure.cpp
$ ./structure

・実行結果

5
xxx

ループと条件分岐

C++

for_if.cpp
#include <iostream>
#include <string>

int main(){
  int input_num; // int型変数input_numを定義

  std::cout << "N=";
  std::cin >> input_num;
  
  for(int i = 0; i < input_num; i++){
    int idx = i+1;
    
    if(idx%2==0){
      std::cout << idx << ": even" << std::endl;
    }
    else{
      std::cout << idx << ": odd" << std::endl;
    }
  }
  return 0; // return 0;はプログラムの終了を表す
}
$ g++ -o for_if for_if.cpp
$ ./for_if

・実行結果

N=10
1: odd
2: even
3: odd
4: even
5: odd
6: even
7: odd
8: even
9: odd
10: even

ポインタ

C++

#include <iostream>

int main(void)
{
  int *p, q;
  q = 12;
  p = &q;
  std::cout << *p <<std::endl;
  return 0;
}
$ g++ -o pointer pointer.cpp
$ ./pointer

・実行結果

12

関数

C++

function1.cpp
# include <cmath>
# include <iostream>


int add(int a, int b)
{
    return a + b;
}

int multiply(int a, int b)
{
    return a * b;
}


int main() {
  int a, b;
  a = 10;

  int answer1 = add(a, a);
  std::cout << answer1 << std::endl;
  
  int answer2 = multiply(a, a);
  std::cout << answer2 << std::endl;
}
$ g++ -o function1 function1.cpp
$ ./function1

・実行結果

20
100

クラス

C++

class1.cpp
#include <iostream>


class Numbers {
  int a, b;

  public:
    Numbers(int a, int b) :a(a), b(b) {}
    void show() {
      std::cout << "{" << a << "," << b << "}" << std::endl;
    }

    int sum() {
      return a + b;
    }
};


int main()
{
  auto numbers = Numbers(2, 8);
  numbers.show();
  std::cout << numbers.sum() << std::endl;
  return 0;
}
$ g++ -o class1 class1.cpp
$ ./class1

・実行結果

{2,8}
10

名前空間

C++

namespace.cpp
#include <iostream>


namespace class1 {
  class Numbers {
    int a, b;

  public:
    Numbers(int a, int b) :a(a), b(b) {}
    void show() {
      std::cout << "{" << a << "," << b << "}" << std::endl;
    }

    int sum() {
      return a + b;
    }
  };
}

int main()
{
  auto numbers = class1::Numbers(2, 8);
  numbers.show();
  std::cout << numbers.sum() << std::endl;
  return 0;
}
$ g++ -o namespace namespace.cpp
$ ./namespace

・実行結果

{2,8}
10

コマンドライン引数

C++

test_arg.cpp
#include <iostream>


int main(int argc, char *argv[]) {
    for(int i = 0; i < argc; i++) {
        std::cout << "No." << i << ": " << argv[i] << std::endl;
    }
    return 0;
}
$ g++ -o test_arg test_arg.cpp
$ ./test_arg

・実行結果

No.0: ./test_arg
No.1: 1
No.2: 2
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?