HTML:CSSで作成した多階層ドロップダウンメニュー

CSSだけで何階層でも可能なドロップダウンメニューを作成しました。また、transition を使って、アニメーション効果を出しています。 尚、作成に当たり下記のサイトを参考にさせて頂きました。ありがとうございました <(_ _)>

1.CSSだけで作る「多階層」なドロップダウンメニュー
2.シンプルなソース・CSSだけでアニメーション・ドロップダウンメニュー
3.アクセシビリティを考えたドロップダウンメニューを実装する
4.Bootstrap3のドロップダウンを多階層にする方法
5.すごく役立った!サイト制作でさりげなく使われているCSSテクニックまとめ

 

menu.htm

<html>
<head>
<link rel="stylesheet" type="text/css" href="menu.css"/>
</head>
<body>
<div class="menu">
  <ul>
    <li>
      <a href="#">main menu 1</a>
      <ul>
        <li><a href="#">submenu 1-1</a>
          <ul>
            <li><a href="#">submenu 1-1-1</a></li>
            <li><a href="#">submenu 1-1-2</a></li>
            <li><a href="#">submenu 1-1-3</a></li>
          </ul>
        </li>
        <li><a href="#">submenu 1-2</a>
          <ul>
            <li><a href="#">submenu 1-2-1</a></li>
            <li><a href="#">submenu 1-2-2</a></li>
            <li><a href="#">submenu 1-2-3</a></li>
          </ul>
        </li>
        <li><a href="#">submenu 1-3</a>
          <ul>
            <li><a href="#">submenu 1-3-1</a></li>
            <li><a href="#">submenu 1-3-2</a>
              <ul>
                <li><a href="#">submenu 1-3-2-1</a></li>
                <li><a href="#">submenu 1-3-2-2</a></li>
                <li><a href="#">submenu 1-3-2-3</a></li>
              </ul>
            </li>
            <li><a href="#">submenu 1-3-3</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li>
      <a href="#">main menu 2</a>
      <ul>
        <li><a href="#">submenu 2-1</a></li>
        <li><a href="#">submenu 2-2</a>
          <ul>
            <li><a href="#">submenu 2-2-1</a></li>
            <li><a href="#">submenu 2-2-2</a></li>
            <li><a href="#">submenu 2-2-3</a></li>
          </ul>
        </li>
        <li><a href="#">submenu 2-3</a></li>
      </ul>
    </li>
    <li>
      <a href="#">main menu 3</a>
      <ul>
        <li><a href="#">submenu 3-1</a></li>
        <li><a href="#">submenu 3-2</a></li>
        <li><a href="#">submenu 3-3</a></li>
      </ul>
    </li>
  </ul>
</div>
</body>
</html>
 

 

menu.css

.menu {
  margin: 0 auto;
  width: 600px;  //メインの項目数×liのwidth;
}

.menu ul {
  margin: 0;
  padding: 0;
  z-index: 1000;
  list-style: none;
}

.menu ul li {
  position: relative;
  float: left;
  width: 200px;
  height: 30px;
  line-height: 30px;
  background: black;
}

.menu ul li a {
  display: block;
  color: white;
  font-family: 'MS Gothic';
  font-size: 13px;
  font-weight: bold;
  text-align: center;
  text-decoration: none;
}

.menu ul li:hover > a {
  color: gold;
  background: #303030;
}

.menu ul li ul {
  position: absolute;
}

.menu ul li ul li {
  overflow: hidden;
  height: 0;
  background: #505050;
}

.menu ul li:hover > ul > li {
  overflow: visible;
  height: 30px;
  transition: 0.5s;
}

.menu ul li ul li a {
  background: #505050;
}

.menu ul li ul li ul {
  top: 0;
  left: 100%;
}

.menu ul li ul li ul:before {
  position: absolute;
  top: 10px;
  left: -15px;
  content: "";
  border: 5px solid transparent;
  border-left-color: silver;
}
 

HTML:インラインフレーム

iframe(インラインフレーム)

Character Set を UTF-8 で指定した場合は、HTML ファイルを UTF-8 フォーマットで保存すること。

 

iframe.htm

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>
iframe
</title>
<link rel="stylesheet" type="text/css" href="iframe.css">
</head>
<body>
  <iframe src="header.htm" id="header"></iframe>
  <div id="frame">
    <iframe src="left.htm" id="left"></iframe>
    <div id="container">
      <iframe src="content.htm" id="content"></iframe>
    </div>
    <iframe src="right.htm" id="right"></iframe>
  </div>
  <iframe src="footer.htm" id="footer"></iframe>
</body>
</html>
 

 

iframe.css

body {
  margin: 0px;
  padding: 0px;
}

iframe {
  border: none
}

#header {
  position: absolute;
  top: 0px;
  width: 100%;
  height: 50px;
}

#frame {
  position: absolute;
  top: 50px;
  bottom: 50px;
  width: 100%;
}

#left {
  position: absolute;
  left: 0px;
  width: 150px;
  height: 100%;
}

#container {
  position: absolute;
  left: 150px;
  right: 150px;
  height: 100%;
}

#content {
  width: 100%;
  height: 100%;
}

#right {
  position: absolute;
  right: 0px;
  width: 150px;
  height: 100%;
}

#footer {
  position: absolute;
  bottom: 0px;
  width: 100%;
  height: 50px;
}
 

 

header.htm

<html lang="ja">
<head>
<meta charset="utf-8">
<title>
header
</title>
<link rel="stylesheet" type="text/css" href="header.css">
</head>
<body>
<h1>
header
</h1>
</body>
</html>
 

 

header.css

body {
  text-align: center;
  background: #888fff;
  overflow: hidden;
}
 

 

left.htm

<html lang="ja">
<head>
<meta charset="utf-8">
<title>
left
</title>
<link rel="stylesheet" type="text/css" href="left.css">
</head>
<body>
<h1>
left
</h1>
<div id="sidemenu">
<ul>
<li><a href="#">メニュー1</a></li>
<li><a href="#">メニュー2</a></li>
<li><a href="#">メニュー3</a></li>
<li><a href="#">メニュー4</a></li>
<li><a href="#">メニュー5</a></li>
</ul>
</div>
</body>
</html>
 

 

left.css

body {
  text-align: center;
  background: #555fff;
  overflow: hidden;
}

#sidemenu ul {
  margin: 0px;
  padding: 0px;
  list-style: none;
}

#sidemenu li a {
  display: block;
  margin: 0px;
  padding: 0px;
  width: 130px;
  height: 25px;
  color: #000000;
  font-family: 'MS Gothic';
  font-size: 20px;
  font-weight: bold;
  text-align: left;
  text-decoration: none;
  background: gold;
  border: 1px solid black;
}

#sidemenu li a:hover {
  background: silver;
}
 

 

content.htm

<html lang="ja">
<head>
<meta charset="utf-8">
<title>
content
</title>
<link rel="stylesheet" type="text/css" href="content.css">
</head>
<body>
<h1>
content
</h1>
<p>
本文<br>
本文<br>
本文<br>
本文<br>
本文<br>
</p>
</body>
</html>
 

 

content.css

body {
  //text-align: center;
  //background: #888fff;
}
 

 

right.htm

<html lang="ja">
<head>
<meta charset="utf-8">
<title>
right
</title>
<link rel="stylesheet" type="text/css" href="right.css">
</head>
<body>
<h1>
right
</h1>
</body>
</html>
 

 

right.css

body {
  text-align: center;
  background: #555fff;
  overflow: hidden;
}
 

 

footer.htm

<html lang="ja">
<head>
<meta charset="utf-8">
<title>
footer
</title>
<link rel="stylesheet" type="text/css" href="footer.css">
</head>
<body>
<h1>
footer
</h1>
</body>
</html>
 

 

footer.css

body {
  text-align: center;
  background: #888fff;
  overflow: hidden;
}
 

 


参考サイト

 

HTML:ヘッダーとサイドバーを固定するためのCSS

ヘッダーとサイドバーを固定するための設定を紹介します。

 

sample.htm

<html>
<head>
<style>
body {
  margin: 0px;
  padding: 100px 0px 0px 200px;
}
#header {
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100px;
  text-align: center;
  background-color: #888fff;
}
#sidebar {
  left: 0px;
  padding-top: 10px;
  width: 200px;
  height: 100%;
  text-align: center;
  background-color: #fff888;
}
#content {
  overflow: auto;
  padding-top: 10px;
  padding-left: 20px;
  background-color: silver;
}
@media screen {
  #header {
    position: fixed;
  }
  #sidebar {
    position: fixed;
  }
}
</style>
</head>
<body>
<div id="header">
  <h1>header</h1>
</div>
<div id="sidebar">
  <h1>sidebar</h1>
</div>
<div id="content">
  <h1>content</h1>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
  <p>Hello!</p>
</div>
</body>
</html>
 

参考サイト

HTML:inurlを使ったサイト内検索

Googleのinurl(URLに含まれる文字列)を使ったサイト内検索方法です。

 

search.htm

<html>
<body>
<form action="http://www.google.com/search" target="_blank">
<input type="hidden" name="hl" value="ja">
<input type="text" name="hq" value="inurl" size="50">
<input type="text" name="q" value="keyword" size="50">
<input type="submit" value="search">
</form>
</body>
</html>
 

 

〈使い方〉
一つ目のテキストフィールドにinurl(例えば、xxxxx.hatenablog.com/)を入力し、二つ目のテキストフィールドに検索したいキーワードを入力します。


参考サイト

 

Swiftによるオブジェクト指向プログラミング

Swift では、クラス(class)は参照型(reference type)、構造体(struct)は値型(value type)です。

robot.swift


class Robot {
  static let version = "Robot1.0"
  var (x, y):(Double, Double)
  var location:(Double, Double) {
    set(parameter) { (self.x, self.y) = (parameter.0, parameter.1) }
    get { return (x, y) }
  }
  init(x:Double = 0, y:Double = 0) {
    (self.x, self.y) = (x, y)
  }
  func move(dx:Double, dy:Double) {
    x += dx
    y += dy
  }
  func show() {
    print("x =",x)
    print("y =",y)
    print("(x, y) = (\(x), \(y))")
  }
}

print(Robot.version)

let robot0 = Robot()
robot0.location = (0, 0)
print(robot0.location)
robot0.move(dx:1, dy:1)
robot0.show()

let robot1 = Robot(x:0, y:0)
robot1.move(dx:1, dy:1)
robot1.show()
 

 

コンパイルと実行〉


$ swiftc -O robot.swift -o robot
$ ./robot

 

参考サイト

 

Javaによるオブジェクト指向プログラミング

Javaのクラスは参照型(reference type)です。

注)Javaのローカル変数は、明示的に初期化しなければなりません。
注)Javaのメンバー変数は、明示的に初期化しなくても暗黙的にデフォルト値(default value)で初期化されます。


 

robot.java

class Robot {
  private double x, y;

  Robot() {
    set(0, 0);
  }

  Robot(double x, double y) {
    set(x, y);
  }

  void set(double x, double y) {
    this.x = x;
    this.y = y;
  }

  double getX() {
    return x;
  }

  double getY() {
    return y;
  }

  void move(double dx, double dy) {
    x += dx;
    y += dy;
  }

  void print() {
    System.out.println("x = " + x);
    System.out.println("y = " + y);
  }
}
 

 

main.java

class Main {
  public static void main(String... args) {
    Robot robot1 = new Robot();
    robot1.move(1, 1);
    robot1.print();
    System.out.println("x = " + robot1.getX());
    System.out.println("y = " + robot1.getY());

    Robot[] robot2 = new Robot[]{new Robot(), new Robot()};
    for (Robot robot : robot2) {
      robot.move(1, 1);
      robot.print();
    }

    int n = 10;
    Robot[] robot3 = new Robot[n];
    for (int i = 0; i < robot3.length; i++) {
      robot3[i] = new Robot(0, 0);
      robot3[i].move(i, i);
      robot3[i].print();
    }
  }
}
 

 

コンパイル

javac0.bat(ファイル名をjavac.batにすると暴走します)

set JAVA_HOME=D:\sdk\jdk
set path=%path%;%JAVA_HOME%\bin
javac *.java
 

 

実行

java0.bat(ファイル名をjava.batにすると暴走します)

set JAVA_HOME=D:\sdk\jdk
set path=%path%;%JAVA_HOME%\bin
java Main
pause
 

継承

 

inheritance.java

class Position {
  double x, y;

  Position() {
    set(0, 0);
  }

  Position(double x, double y) {
    set(x, y);
  }

  void set(double x, double y) {
    this.x = x;
    this.y = y;
  }
}

class Robot extends Position {
  Robot() {
    super();
  }

  Robot(double x, double y) {
    super(x, y);
  }
}

class Main {
  public static void main(String... args) {
    Robot robot0 = new Robot();
    System.out.println("x = " + robot0.x);
    System.out.println("y = " + robot0.y);

    Robot robot1 = new Robot(1, 1);
    System.out.println("x = " + robot1.x);
    System.out.println("y = " + robot1.y);
  }
}
 

packageの作成とimport

 

packageの作成

D:\pkg\pkg1\pkg2\pkg3\Hello.java

package pkg1.pkg2.pkg3;

public class Hello {
  public Hello() {
    System.out.println("Hello!");
  }
}
 

 

packageのコンパイル

D:\pkg\javac0.bat(ファイル名をjavac.batにすると暴走します)

set JAVA_HOME=D:\sdk\jdk
set path=%path%;%JAVA_HOME%\bin
javac D:/pkg/pkg1/pkg2/pkg3/*.java
 

 

JARライブラリの作成

D:\pkg\jar0.bat(ファイル名をjar.batにすると暴走します)

set JAVA_HOME=D:\sdk\jdk
set path=%path%;%JAVA_HOME%\bin
jar cfv lib.jar pkg1
 

注)jar cfv lib.jar D:/pkg/pkg1 とするとうまくいきません!


Mainクラスの作成(packageのインポート)

D:\src\main.java

import pkg1.pkg2.pkg3.Hello;

class Main {
  public static void main(String... args) {
    new Hello();
    //new pkg1.pkg2.pkg3.Hello();
  }
}
 

 

Mainクラスのコンパイル

D:\src\javac0.bat(ファイル名をjavac.batにすると暴走します)

set JAVA_HOME=D:\sdk\jdk
set path=%path%;%JAVA_HOME%\bin
javac -cp D:/pkg *.java
 

 

Mainクラスの実行

D:\src\java0.bat(ファイル名をjava.batにすると暴走します)

set JAVA_HOME=D:\sdk\jdk
set path=%path%;%JAVA_HOME%\bin
java -cp .;D:/pkg Main
pause
 

 

Mainクラスのコンパイル(JARライブラリを使った場合)

D:\src\javac0j.bat(ファイル名をjavac.batにすると暴走します)

set JAVA_HOME=D:\sdk\jdk
set path=%path%;%JAVA_HOME%\bin
javac -cp D:/pkg/lib.jar *.java
 

 

Mainクラスの実行(JARライブラリを使った場合)

D:\src\java0j.bat(ファイル名をjava.batにすると暴走します)

set JAVA_HOME=D:\sdk\jdk
set path=%path%;%JAVA_HOME%\bin
java -cp .;D:/pkg/lib.jar Main
pause
 

Accessibility
 access modifier   same class   same package   subclasses in other package   from anywhere 
public
protected ×
no modifier × ×
private × × ×

参考サイト

 

C++によるオブジェクト指向プログラミング

コンストラクタはオブジェクトの記憶領域が確保された直後に実行され、ディストラクタはオブジェクトの記憶領域が解放された直後に実行されます。どちらも自動的に実行されます。また、コンストラクタの中で変数を new した場合は、通常ディストラクタの中でその変数を delete します。

注)C++では構造体もクラスも値型(value type)です。
注)A a; クラスAのデフォルトコンストラクタが定義されていれば、aはデフォルトコンストラクタで初期化されます。
注)デフォルト引数(default parameter)のデフォルト値(default value)は、関数のプロトタイプ宣言で設定します。
注)デフォルトコンストラクタとは、引数がないか、全ての引数にデフォルト値が設定されているコンストラクタです。
注)引数無しのコンストラクタと全ての引数がデフォルト引数であるコンストラクタを同時に定義するとエラーになります。
注)static const の整数型メンバー変数を除いて、メンバー変数を宣言と同時に初期化しないこと! → 初期化してもよい!


オブジェクトの初期化と代入

注)C++では初期化の=と代入の=の違いに注意する必要があります。

 

class.cpp

#include <iostream>

class Robot {
public:
  double x = 1, y = 1;
};

int main() {
  Robot robot;
  std::cout << robot.x << std::endl;
  std::cout << robot.y << std::endl;
}
 

 

object.cpp

#include <iostream>

class Robot {
  double x, y;
public:
  Robot();
  Robot(double, double);
  void set(double, double);
  double getX() const;
  double getY() const;
};

//メンバー初期化リストを使った引数無しコンストラクタ&デフォルトコンストラクタ
//Robot::Robot() : x(0), y(0) {}

//引数無しコンストラクタ&デフォルトコンストラクタ
Robot::Robot() {
  set(0, 0);
}

//メンバー初期化リストを使った引数付きコンストラクタ
//Robot::Robot(double x, double y) : x(x), y(y) {}

//引数付きコンストラクタ
Robot::Robot(double x, double y) {
  set(x, y);
}

void Robot::set(double x, double y) {
  this->x = x;
  this->y = y;
}

double Robot::getX() const {
  return x;
}

double Robot::getY() const {
  return y;
}

int main() {
  //デフォルトコンストラクタによる初期化
  Robot robot;
  std::cout << robot.getX() << std::endl;
  std::cout << robot.getY() << std::endl;

  //一時オブジェクトの代入
  robot = Robot();
  robot = Robot(0, 0);

  //デフォルトコピーコンストラクタによる初期化
  Robot robot0(robot);

  //デフォルト代入演算子によるオブジェクトの代入
  robot0 = robot;

  //引数無しコンストラクタによる初期化
  Robot robot1(); // -> NG
  auto robot2 = Robot();

  //引数付きコンストラクタによる初期化
  Robot robot3(3, 3);
  auto robot4 = Robot(4, 4);
}
 

 

robot.hpp

#pragma once

#include <iostream>

class Robot {
  double x, y;

public:
  static int nRobots;

  //default constructor with default parameters
  Robot(double x = 0, double y = 0) : x(x), y(y) {}

  Robot(const Robot& robot) {
    x = robot.x;
    y = robot.y;
  }

  Robot& operator=(const Robot& robot) {
    x = robot.x;
    y = robot.y;
    return *this;
  }

  virtual ~Robot() {}

  void set(double x, double y) {
    this->x = x;
    this->y = y;
  }

  double getX() const {
    return x;
  }

  double getY() const {
    return y;
  }

  void move(double dx, double dy) {
    x += dx;
    y += dy;
  }

  void print() const {
    using namespace std;
    cout << "(x, y) = " << "(" << x << ", " << y << ")" << endl;
  }
};

int Robot::nRobots = 0;
 

 

main.cpp

#include "robot.hpp"

int main() {
  //Robot robot0;
  //Robot robot0(1);
  //Robot robot0(1, 1);
  auto robot0 = Robot();
  robot0.print();

  Robot robot1(robot0);
  robot1 = robot0;
  robot1.move(1, 1);
  robot1.print();

  auto robot2 = new Robot;
  robot2->move(2, 2);
  robot2->print();
  delete robot2;

  //g++ OK, clang++ NG
  Robot::nRobots = 10;
  Robot robot3[Robot::nRobots];
  for (int i = 0; i < Robot::nRobots; i++) {
    robot3[i].set(0, 0);
    robot3[i].move(3, 3);
    robot3[i].print();
  }

  Robot::nRobots = 10;
  auto robot4 = new Robot[Robot::nRobots];
  for (int i = 0; i < Robot::nRobots; i++) {
    robot4[i].set(0, 0);
    robot4[i].move(4, 4);
    robot4[i].print();
  }
  delete[] robot4;
}
 

 

build.bat

set path=d:\sdk\msys64\mingw64\bin;%path%;
g++ -std=c++17 -O3 -pedantic-errors -s -Wall -static *.cpp -o main.exe
strip main.exe

 

robot.hpp

#pragma once

class Robot {
  double x, y;
public:
  static int nRobots;
  Robot(double x = 0, double y = 0);
  Robot(const Robot&);
  Robot& operator=(const Robot&);
  virtual ~Robot();
  void set(double, double);
  double getX() const;
  double getY() const;
  void move(double, double);
  void print() const;
};
 

 

robot.cpp

#include "robot.hpp"
#include <iostream>

int Robot::nRobots = 0;

/* member initialization list
Robot::Robot() : x(0), y(0) {}
Robot::Robot(double x, double y) : x(x), y(y) {}
*/

Robot::Robot(double x, double y) {
  this->x = x;
  this->y = y;
}

//copy constructor
Robot::Robot(const Robot& robot) {
  x = robot.x;
  y = robot.y;
}

//assignment operator overloading
Robot& Robot::operator=(const Robot& robot) {
  x = robot.x;
  y = robot.y;
  return *this;
}

Robot::~Robot() {}

void Robot::set(double x, double y) {
  this->x = x;
  this->y = y;
}

double Robot::getX() const {
  return x;
}

double Robot::getY() const {
  return y;
}

void Robot::move(double dx, double dy) {
  x += dx;
  y += dy;
}

void Robot::print() const {
  using namespace std;
  cout << "(x, y) = " << "(" << x << ", " << y << ")" << endl;
}
 

 

main.cpp

#include "robot.hpp"

int main() {
  //Robot robot0;
  //Robot robot0(1);
  //Robot robot0(1, 1);
  auto robot0 = Robot();
  robot0.print();

  Robot robot1(robot0);
  robot1 = robot0;
  robot1.move(1, 1);
  robot1.print();

  auto robot2 = new Robot;
  robot2->move(2, 2);
  robot2->print();
  delete robot2;

  //g++ OK, clang++ OK
  const int n = 10;
  Robot robot3[n];
  for (int i = 0; i < n; i++) {
    robot3[i].set(0, 0);
    robot3[i].move(3, 3);
    robot3[i].print();
  }

  Robot::nRobots = 10;
  auto robot4 = new Robot[Robot::nRobots];
  for (int i = 0; i < Robot::nRobots; i++) {
    robot4[i].set(0, 0);
    robot4[i].move(4, 4);
    robot4[i].print();
  }
  delete[] robot4;
}
 

 

build.bat

set path=d:\sdk\msys64\mingw64\bin;%path%;
g++ -std=c++17 -O3 -pedantic-errors -s -Wall -c robot.cpp -o robot.o
g++ -std=c++17 -O3 -pedantic-errors -s -Wall -static robot.o main.cpp -o main.exe
strip main.exe

オブジェクトをメンバーに持つクラス

 

member_object.cpp

#include <iostream>

class X {
public:
  double x;
  X(double x = 0);
};

X::X(double x) : x(x) {}

class Y {
public:
  double y;
  Y(double y = 0);
};

Y::Y(double y) : y(y) {}

class Robot {
public:
  X x;
  Y y;
  //引数付きコンストラクタ&デフォルトコンストラクタ
  Robot(double x = 1, double y = 1);
};

//引数付きコンストラクタを使ったメンバー初期化リスト
Robot::Robot(double x, double y) : x(x), y(y) {}
/*
Robot::Robot(double x, double y) {
  this->x = X(x);
  this->y = Y(y);
}
*/

int main() {
  X x;
  Y y;
  std::cout << x.x << std::endl;
  std::cout << y.y << std::endl;
  //Robot robot; // -> OK
  //Robot robot(); // -> NG
  //Robot robot(1, 1); // -> OK
  //auto robot = Robot(); // -> NG
  auto robot = Robot(1, 1);
  std::cout << robot.x.x << std::endl;
  std::cout << robot.y.y << std::endl;
}
 

クラスの継承

 

inheritance.cpp

#include <iostream>

class X {
public:
  double x;
  X(double x = 0);
};

X::X(double x) : x(x) {}

class Y {
public:
  double y;
  Y(double y = 0);
};

Y::Y(double y) : y(y) {}

class Robot : public X, public Y {
public:
  //引数付きコンストラクタ&デフォルトコンストラクタ
  Robot(double x = 1, double y = 1);
};

//スーパークラスの引数付きコンストラクタを使ったメンバー初期化リスト
Robot::Robot(double x, double y) : X(x), Y(y) {}
/*
Robot::Robot(double x, double y) {
  X::x = x;
  Y::y = y;
}
*/

int main() {
  X x;
  Y y;
  std::cout << x.x << std::endl;
  std::cout << y.y << std::endl;
  //Robot robot; // -> OK
  //Robot robot(); // -> NG
  //Robot robot(1, 1); // -> OK
  //auto robot = Robot(); // -> NG
  auto robot = Robot(1, 1);
  std::cout << robot.x << std::endl;
  std::cout << robot.y << std::endl;
}
 

 

array.hpp

#pragma once

class Array {
public:
  static double common;
  static const long length = 10;
private:
  double element[length];
public:
  ArragetY();
  double& operator[](long);
};
 

 

array.cpp

#include "array.hpp"
#include <cassert>

double Array::common = 0;

Array::ArragetY() {
  if (length > 0) {
    for (long i = 0; i < length; i++) {
      element[i] = 0;
    }
  }
}

auto Array::operator[](long index) -> double& {
  assert(0 <= index && index < length);
  return element[index];
}
 

 

main.cpp(オブジェクトを static data area に割り当てるプログラム)

#include "array.hpp"
#include <iostream>

auto array = ArragetY();
//auto array = new ArragetY(); // -> NG

auto main() -> int {
  for (long i = 0; i < Array::length; i++) {
    array[i] = i;
    std::cout << array[i] << std::endl;
  }
  std::cout << Array::common << std::endl;
}
 

 

main.cpp(オブジェクトを stack area に割り当てるプログラム)

#include "array.hpp"
#include <iostream>

auto main() -> int {
  auto array = ArragetY();
  for (long i = 0; i < Array::length; i++) {
    array[i] = i;
    std::cout << array[i] << std::endl;
  }
  std::cout << Array::common << std::endl;
}
 

 

main.cpp(オブジェクトを heap area に割り当てるプログラム)

#include "array.hpp"
#include <iostream>

auto main() -> int {
  auto array = new ArragetY();
  for (long i = 0; i < Array::length; i++) {
    (*array)[i] = i;
    std::cout << (*array)[i] << std::endl;
  }
  delete array;
  std::cout << Array::common << std::endl;
}
 

 

build.bat

set path=d:\sdk\msys64\mingw64\bin;%path%;
clang++ -std=c++17 -O3 -pedantic-errors -s -Wall -static *.cpp -o main.exe
strip main.exe

 

注)Windowsのstack領域のサイズは、2MB。


参考サイト