ポリモーフィズム - (2016/03/23 (水) 12:54:42) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
*ポリモーフィズム
オブジェクト指向の要となる概念のひとつにポリモーフィズム(多態性)があります。
前章の人間と学生クラスを例にとります。
学生クラスは人間クラスの派生クラスです。
まずは学生の太郎を作ります。
Student *taro = new Student();
この太郎オブジェクトのポインタは、人間オブジェクトのポインタに代入できるのです。
Person *p = taro;
こうして太郎を人間として扱うことができます。
p->selfIntroduction();
太郎は学生である前に人間なのです。
人間pとして扱う太郎ですが、元は学生でも、人間として扱っているので学籍の動作や性質は使えません。
以下はコンパイルエラーになります。
p->getID();
**Person.h
#html2(){{
<pre class="brush: cpp;">
#pragma once
#include < string >
using namespace std;
class Person
{
protected:
int age;
string name;
public:
Person(int age, string name);
int getAge();
string getName();
void selfIntroduction(); //virtual キーワードの有無で動作が変わる
};
</pre>
}}
**Person.cpp
#html2(){{
<pre class="brush: cpp;">
#include "Person.h"
#include < iostream >
Person::Person(int age, string name){
this->age = age;
this->name = name;
}
int Person::getAge(){
return this->age;
}
string Person::getName(){
return this->name;
}
void Person::selfIntroduction(){
cout << name << "、" << age << "歳です。" << endl;
}
</pre>
}}
**Student.h
#html2(){{
<pre class="brush: cpp;">
#pragma once
#include "person.h"
class Student :
public Person
{
protected:
string ID;
public:
Student(int age, string name, string ID);
~Student(void);
string getID();
void selfIntroduction();
};
</pre>
}}
**Student.cpp
#html2(){{
<pre class="brush: cpp;">
#include "Student.h"
#include < iostream >
Student::Student(int age, string name, string ID) : Person(age, name){
this->ID = ID;
}
string Student::getID(){
return this->ID;
}
void Student::selfIntroduction(){
cout << name << "、" << age << "歳 学生です。" << endl;
}
</pre>
}}
**main
#html2(){{
<pre class="brush: cpp;">
#include < iostream >
int main()
{
//Studentとしてtaroを生成
Student *taro = new Student(20, "情報太郎", "S123456");
//Person型に代入
Person *p = taro;
//taroが持っているPerson型の性質と動作が使える
cout << "年齢 : " << p->getAge() << endl;
//元がStudentでもPerson型に入れたので、Studentの性質と動作は使えない
// cout << p->getID() << endl; //←コンパイルエラー
//オーバーライドされている関数
//Studentとして扱うとき
taro->selfIntroduction();
//Personとして扱うとき
//virtual無し PersonのselfIntroductionが呼ばれる
//virtual有り StudentのselfIntroductionが呼ばれる
p->selfIntroduction();
return 0;
}
</pre>
}}
#include(highlight)
*ポリモーフィズム
オブジェクト指向の要となる概念のひとつにポリモーフィズム(多態性)があります。
前章の人間と学生クラスを例にとります。
学生クラスは人間クラスの派生クラスです。
まずは学生の太郎を作ります。
Student *taro = new Student();
この太郎オブジェクトのポインタは、人間オブジェクトのポインタに代入できるのです。
Person *p = taro;
こうして太郎を人間として扱うことができます。
p->selfIntroduction();
太郎は学生である前に人間なのです。
人間pとして扱う太郎ですが、元は学生でも、人間として扱っているので学籍の動作や性質は使えません。
以下はコンパイルエラーになります。
p->getID();
**Person.h
#html2(){{
<pre class="brush: cpp;">
#pragma once
#include < string >
using namespace std;
class Person
{
protected:
int age;
string name;
public:
Person(int age, string name);
int getAge();
string getName();
void selfIntroduction(); //virtual キーワードの有無で動作が変わる
};
</pre>
}}
**Person.cpp
#html2(){{
<pre class="brush: cpp;">
#include "Person.h"
#include < iostream >
Person::Person(int age, string name){
this->age = age;
this->name = name;
}
int Person::getAge(){
return this->age;
}
string Person::getName(){
return this->name;
}
void Person::selfIntroduction(){
cout << name << "、" << age << "歳です。" << endl;
}
</pre>
}}
**Student.h
#html2(){{
<pre class="brush: cpp;">
#pragma once
#include "person.h"
class Student :
public Person
{
protected:
string ID;
public:
Student(int age, string name, string ID);
~Student(void);
string getID();
void selfIntroduction();
};
</pre>
}}
**Student.cpp
#html2(){{
<pre class="brush: cpp;">
#include "Student.h"
#include < iostream >
Student::Student(int age, string name, string ID) : Person(age, name){
this->ID = ID;
}
string Student::getID(){
return this->ID;
}
void Student::selfIntroduction(){
cout << name << "、" << age << "歳 学生です。" << endl;
}
</pre>
}}
**main
#html2(){{
<pre class="brush: cpp;">
#include < iostream >
int main()
{
//Studentとしてtaroを生成
Student *taro = new Student(20, "情報太郎", "S123456");
//Person型に代入
Person *p = taro;
//taroが持っているPerson型の性質と動作が使える
cout << "年齢 : " << p->getAge() << endl;
//元がStudentでもPerson型に入れたので、Studentの性質と動作は使えない
// cout << p->getID() << endl; //←コンパイルエラー
//オーバーライドされている関数
//Studentとして扱うとき
taro->selfIntroduction();
//Personとして扱うとき
//virtual無し PersonのselfIntroductionが呼ばれる
//virtual有り StudentのselfIntroductionが呼ばれる
p->selfIntroduction();
return 0;
}
</pre>
}}
//#include(highlight)
表示オプション
横に並べて表示:
変化行の前後のみ表示: