/*This
code was written by A. El-Gadi of Tripoli University for educational
purposes. It offers variations on the template theme and their implications. The classes used here are not necessarily useful, as they were designed for the sole purpose of offering a synoptic peek at the template syntax. For any
questions and discussion
use the comment section below.*/
#include <iostream>
using namespace std;
class AA{public: int data1; double data2;};
class BB{public: int data1; double data2;};
template<typename T1,typename T2>
class BoxOf2{private:
T1 var1;
T2 var2;
public:
double sum1(){return var1+var2.data1;}//The presence of this function dectates that T2 must have a public member variable called data1
double sum2(){return var1+var2.data2;}//The presence of this function dectates that T2 must have a public member variable called data1
T1 &getvar1(){return var1;}//We let get return a reference here to allow for left-hand-side assignment. See below. This consideration is not related to templates per se.
T2 &getvar2(){return var2;}//We let get return a reference here to allow for left-hand-side assignment. See below. This consideration is not related to templates per se.
void setvar1(T1 v1){var1=v1;}
void setvar2(T2 v2){var2=v2;}
};
int main(){
BoxOf2<int,AA> bia1; BoxOf2<int,AA> bia2; BoxOf2<float,AA> bfa; BoxOf2<int,BB> bib1; BoxOf2<int,BB> *pbib;//Box<int,AA> Box<float,AA> Box<int,BB> are strictly different types. As such, they cannot be assign to each other nor cast to each other.
bia1.setvar1(10);
bia1.setvar2(AA());//Set var2 with an object of type AA. If they exist, parametrized constructors can be used here.
bia1.getvar2().data1=100;//This is possible because getvar2 returns a reference.
bia1.getvar2().data2=1000;//This is possible because getvar2 returns a reference.
cout<<bia1.getvar1()<<endl;
cout<<bia1.getvar2().data1<<endl;
cout<<bia1.getvar2().data2<<endl;
bia2=bia1;//bia1 and bia2 are of the same type this why they can be assigned to each other. They are both of type BoxOf2<int,AA>
pbib = new BoxOf2<int,BB>();//Example of using the new operator to create and object in the runtime.
bib1.setvar1(11);
bib1.setvar2(BB());//Set var2 with an object of type AA. If they exist, parametrized constructors can be used here.
bib1.getvar2().data1=111;
bib1.getvar2().data2=1111;
BoxOf2<int,BB> bib2(bib1);//Copy constructor.
*pbib=bib2;
cout<<pbib->getvar1()<<endl;
cout<<pbib->getvar2().data1<<endl;
cout<<pbib->getvar2().data2<<endl;
delete pbib; //delete as usual.
return 0;
}
using namespace std;
class AA{public: int data1; double data2;};
class BB{public: int data1; double data2;};
template<typename T1,typename T2>
class BoxOf2{private:
T1 var1;
T2 var2;
public:
double sum1(){return var1+var2.data1;}//The presence of this function dectates that T2 must have a public member variable called data1
double sum2(){return var1+var2.data2;}//The presence of this function dectates that T2 must have a public member variable called data1
T1 &getvar1(){return var1;}//We let get return a reference here to allow for left-hand-side assignment. See below. This consideration is not related to templates per se.
T2 &getvar2(){return var2;}//We let get return a reference here to allow for left-hand-side assignment. See below. This consideration is not related to templates per se.
void setvar1(T1 v1){var1=v1;}
void setvar2(T2 v2){var2=v2;}
};
int main(){
BoxOf2<int,AA> bia1; BoxOf2<int,AA> bia2; BoxOf2<float,AA> bfa; BoxOf2<int,BB> bib1; BoxOf2<int,BB> *pbib;//Box<int,AA> Box<float,AA> Box<int,BB> are strictly different types. As such, they cannot be assign to each other nor cast to each other.
bia1.setvar1(10);
bia1.setvar2(AA());//Set var2 with an object of type AA. If they exist, parametrized constructors can be used here.
bia1.getvar2().data1=100;//This is possible because getvar2 returns a reference.
bia1.getvar2().data2=1000;//This is possible because getvar2 returns a reference.
cout<<bia1.getvar1()<<endl;
cout<<bia1.getvar2().data1<<endl;
cout<<bia1.getvar2().data2<<endl;
bia2=bia1;//bia1 and bia2 are of the same type this why they can be assigned to each other. They are both of type BoxOf2<int,AA>
pbib = new BoxOf2<int,BB>();//Example of using the new operator to create and object in the runtime.
bib1.setvar1(11);
bib1.setvar2(BB());//Set var2 with an object of type AA. If they exist, parametrized constructors can be used here.
bib1.getvar2().data1=111;
bib1.getvar2().data2=1111;
BoxOf2<int,BB> bib2(bib1);//Copy constructor.
*pbib=bib2;
cout<<pbib->getvar1()<<endl;
cout<<pbib->getvar2().data1<<endl;
cout<<pbib->getvar2().data2<<endl;
delete pbib; //delete as usual.
return 0;
}
No comments:
Post a Comment