投稿日時: 2019/01/04
S Nakagawa
Code09
オペレータ(演算子)のオーバーロードを実装してみる。これまでに作成したクラスのインスタンスの和を求めることはできない。作成したクラス同士の足し算が可能となるように,プラス(+)演算子のオーバーロードを実現してみる。
c++
#include <iostream>
#include <string>
template<classType>
classextendedType
{
Typevalue_;
std::stringtypeName_;
public:
extendedType(conststd::string&type)
{
typeName_=type;
}
TypemakeDouble()
{
returnvalue_+value_;
};
booldisplayDouble()
{
std::cout<<typeName_<<": "<<value_<<" の2倍は "<<makeDouble()<<" です。"<<std::endl;
std::cout<<std::endl;
};
boolread()
{
std::cout<<typeName_<<" を入力してください。"<<std::endl;
std::cin>>value_;
};
booldisplay()
{
std::cout<<"typeName_ : "<<typeName_<<std::endl;
std::cout<<"value_ : "<<value_<<std::endl;
std::cout<<std::endl;
};
//overload of operator
constextendedTypeoperator+(constextendedTypeobj)const
{
// value and type is added!
extendedType<Type>tmp(typeName_);
tmp.value_=value_+obj.value_;
tmp.typeName_=typeName_+"+"+obj.typeName_;
returntmp;
}
};
intmain()
{
extendedType<int>extInt01("整数A");
extInt01.read();
extInt01.displayDouble();
extendedType<int>extInt02("整数B");
extInt02.read();
extInt02.displayDouble();
std::cout<<"拡張整数Aと拡張整数Bとの和「+」"<<std::endl;
extInt01=extInt01+extInt02;
extInt01.display();
extInt01.displayDouble();
return0;
}
Code10
クラスの継承について考える。
extendTypeクラスを継承し,インスタンス名も保持するクラスとしてnamedExtendTypeというクラスを作る。(親クラス:extendType,子クラス:namedExtendType)
主プログラムでは,拡張したクラスを使用している。
c++
#include <iostream>
#include <string>
template<classType>
classextendedType
{
protected:// this will be accesible from child-class.
Typevalue_;
std::stringtypeName_;
public:
extendedType(conststd::string&type)
{
typeName_=type;
}
TypemakeDouble()
{
returnvalue_+value_;
};
booldisplayDouble()
{
std::cout<<typeName_<<": "<<value_<<" の2倍は "<<makeDouble()<<" です。"<<std::endl;
std::cout<<std::endl;
};
boolread()
{
std::cout<<typeName_<<" を入力してください。"<<std::endl;
std::cin>>value_;
};
booldisplay()
{
std::cout<<"typeName_ : "<<typeName_<<std::endl;
std::cout<<"value_ : "<<value_<<std::endl;
std::cout<<std::endl;
};
//overload of operator
constextendedTypeoperator+(constextendedTypeobj)const
{
// value and type is added!
extendedType<Type>tmp(typeName_);
tmp.value_=value_+obj.value_;
tmp.typeName_=typeName_+"+"+obj.typeName_;
returntmp;
}
};
//--------------------------
//
template<classType>
classnamedExtendedType
:
publicextendedType<Type>
{
std::stringinstanceName_;
public:
// Step 1
namedExtendedType(conststd::string&type)
:
extendedType<Type>(type)
{
}
// step 2
voidsetInstanceName(conststd::string&name_)
{
instanceName_=name_;
}
booldisplay()
{
std::cout<<"instanceName_ : "<<instanceName_<<std::endl;
extendedType<Type>::display();
};
//overload of operator
constnamedExtendedTypeoperator+(constnamedExtendedTypeobj)const
{
// value and type is added!
namedExtendedType<Type>tmp(extendedType<Type>::typeName_);
tmp.value_=extendedType<Type>::value_+obj.value_;
tmp.typeName_=extendedType<Type>::typeName_+"+"+obj.typeName_;
tmp.instanceName_=instanceName_+"+"+obj.instanceName_;
returntmp;
}
};
intmain()
{
//extendedType<int> extInt01("整数A");
namedExtendedType<int>extInt01("整数A");
extInt01.read();
extInt01.displayDouble();
extInt01.setInstanceName("FirstInt");
extInt01.display();
namedExtendedType<int>extInt02("整数B");
extInt02.read();
extInt02.displayDouble();
std::cout<<"拡張整数Aと拡張整数Bとの和「+」"<<std::endl;
extInt01=extInt01+extInt02;
extInt01.display();
return0;
}