First steps towards C++:
Structures + Functions = Classes
(C++ の初歩: 構造体 + 関数 =
クラス)
Computing Practice I
8th lecture, May 31, 2018
http://www.sw.it.aoyama.ac.jp/2018/CP1/lecture8.html
Martin J. Dürst
© 2005-18 Martin
J. Dürst 青山学院大学
Today's Schedule
- Minitest
- About last week's exercises
- Object-orientation and C++
- Today's exercises
ミニテスト
- 授業開始までにログイン済み
- ナビゲーションは左に畳み、ブラウザは全画面に拡大
- 授業開始まで教科書、資料、筆箱、財布などを鞄に入れ、鞄を椅子の下に
- テスト終了後その場で待つ
Results of Previous Exercises
|
07A1 |
07B1 |
07C1 |
07C2 |
07C3 |
100 points |
76 |
42 |
17 |
7 |
- |
60 points |
23 |
54 |
70 |
61 |
9 |
partial |
- |
- |
- |
- |
15 |
errors |
- |
2 |
6 |
16 |
23 |
not submitted |
1 |
2 |
7 |
16 |
53 |
07C2/C3: Reuse functions in other functions!
Environment for C++ Exercises
- File extension:
.c
→ .cpp
(also: .C, .cc,
.CPP, .c++, .cp, .cxx)
- Compiler:
gcc
→ g++
(if necessary, use cygwin's setup.exe to add gcc-g++ on your home/notebook
PC)
- Use C input/output functions (
#include <stdio.h>
,
printf
, scanf
,...)
- Rest of environment is the same as for C exercises
C++ Overview
- Fix 'mistakes' in C
Example 1: // comment until end of line
(later added to C, too)
Example 2: typedef struct {...} Person;
→ struct
Person {...};
- Add object-orientation to C
classes, member functions,...
Applications of C++: Browsers
- Famous open-source browsers are written in C++
- Firefox (Mozilla)
- Webkit (Chrome, Safari, Opera,...)
- Object-oriented languages are well suited for GUI programming
- For low-level programming and high speed, C++ is best
- Mozilla browser compressed source ca. 180MB
Purpose of Object-Orientation
(noun: object-orientation, adjective: object-oriented)
- Combine data and operations
- Encapsulate data, block inappropriate access and change
- Most real-world programming languages use object-orientation in some way
(the C/C++ is an exception)
- Object-orientation becomes more and more useful as project size
increases
- Very important for large software projects
(not part of today's exercises:)
- Inheritance
- Polymorphism
- ...
Class Definition
class class_name {
declarations of member variables
public:
declarations of constructors
declarations of member functions
};
Example of Class Definition
class Point {
double x, y;
public:
Point ();
Point (double new_x, double new_y);
void set_x (double new_x);
void set_y (double new_y);
double get_x ();
double get_y ();
};
(set_...
and get_...
functions are called setter and getter and used to implement
encapsulation)
Member Functions: Important
- Declaration (inside class): Without class name
E.g.: void set_x (double new_x);
- Definition (outside class): Class name is needed
E.g.: void Point::set_x (double new_x) {
... }
- Use:
- Same as (structure) member variables
- Function, therefore
()
is necessary
- E.g.:
Point p(2, 3); p.get_x();
Conversion from Function to Member Function
- Function: Arguments and return value
- Member function: Receiver, arguments, and return value
- In most cases: 1st argument of a function → receiver
- As a result, there is one less argument
- Example of call:
distance(start, end);
→start.distance(end);
- Example of declaration:
double distance(Point start, Point end);
→ double distance(Point other);
- Use of member (variables/functions) inside a member function:
Only the member name is used for members of the receiver
Example: x - other.x
Important Points for Constructors
- Purpose: Create and initialize an instance
- Same name as class name
- No return value (
void
also not needed)
- Possible to have multiple constructors with different (number or type of)
arguments
- Arguments are given after the variable name when a variable is defined
E.g.: Point p1(3.5, 7.4);
- Constructor without arguments (default constructor):
- Automatically created if there are no other constructors
(may still be needed to assure correct initialization)
- Declaration and definition necessary if there are other
constructors
C++ Header Files
- Extension is
.h
, same as for C
- Definition of class (including declaration of constructors and member
functions)
- Definition (implementation) of constructors and member functions in
.cpp
file
- Usually, one class per
.h
/.cpp
pair, with class
name identical to file name (Point.h
and
Point.cpp
)
今日の演習
- 08A1: 以前作ったプログラムで
struct
を
class
に変更、public:
/private:
の体験
- 08A2: クラスを使って点の距離の計算
- 08B1: 日付のクラスの作成
- 08C1: 複素数のクラスの作成
- 08C2: 温度クラスによる単位変換 (内部表現の隠蔽;
重要: 必須
(全員提出)、月曜締切)
08B1/08C1/08C2
ではメインプログラムを提出プログラムの最後に
#include
インデントについて: public:
/private:
は普通のインデントの半分
次回までの準備
- 今日の復習
- 残りの演習問題を宿題として完成、提出
- 参考書の第 11 章 (pp. 282-311) をよく読む。
分かるところと分からないところをよく考える。
Glossary
- object-orientation
- オブジェクト指向
- class
- クラス
- member function
- メンバ関数
- object-oriented (programming) language
- オブジェクト思考 (プログラミング) 言語
- encapsulation
- カプセル化
- inheritance
- (クラスの) 継承
- polymorphism
- 多態性
- member variables
- メンバ変数
- receiver
- レシーバ
- constructor
- コンストラクタ
- setter
- セッタ
- getter
- ゲッタ