9 like 0 dislike

Just like we can create function templates, we can also create class templates, allowing classes to have members that use template parameters as types.

Please create Class template pair which have:

- Two items value

- Constructor

- getMax function return max value between 2 values.

And we use in the main function:

int main () {
  pipls <int> myobject1 (10, 7);
  pipls <float> myobject2 (5.5, 2.5);
  pipls <double> myobject3 (8, 9);
  cout << myobject1.getmax()<<endl;
  cout << myobject2.getmax()<<endl;
  cout << myobject3.getmax()<<endl;
  return 0;
}

Expected output:

10
5.5
9

Notice: You output might get 100/100, but your code matters!

asked in Templates by (9.7k points)
reshown by | 53 views
0 1
Called for Help
0 0
Called for Help
0 0
Called for Help

24 Answers

0 like 0 dislike
Hidden content!
* * * * * * ***
* ** *** * **

using namespace std;



template <class T>

class pipls{
* * * * ** *** * a,b;

public:
** ** * ** * * ** *** x, T y){
*** * * * * * * ** * * ** * ** = x;
* **** *** * ** * ** *** *** ** *** * * = y;
* ** ** * * * *** ****
* ** * **** * ******** * * getmax(){
* ***** ** *** ** * * *** *** * ***** ** ** ** *
* * * ***** ** ** *

};

int main () {
**** ** ** * ** *** * ** ** myobject1 (10, 7);
** * ***** *** * * *** * ** **** * * myobject2 (5.5, 2.5);
* ** * * *** * * * * ** *** myobject3 (8, 9);
** * ** * ***** ** ** ** **** * * * * * * * ** * *
** ** * * ** * << ** * **** ** *
* ** * * * * ** ** * * ** << * ** ****** *** * *
* ** *** * * * * * ** * 0;

}
answered by (-233 points)
0 like 0 dislike
Hidden content!
#include ** ** *** *

using ** std;


* * *** *** * * * *** *

class pipls {
** *** * *** * * * a, b;
* * * * ****
** ** ******** * * ** (T first, T second)
**** ** * * ***** * * * ** ** * * * * ** **
****** *** ** * * ** * * getmax ();

};


**** * ** * **

T *** * ** ***** ()

{
** * * retval;
** *** *** = * *** a : b;
* *** ***** retval;

}



int main () {
*** * * * * * ** * ** * ** (10, 7);
** * ***** ** * * ** * (5.5, 2.5);
* ** * * * *** ** * * * ** ****** myobject3 (8, 9);
* ** * * *** * * *** ** * **** * * ** * *** ***
** * ** * * * **** * ** ***** *** * * *
* * ** ** ** **** **** ** * * ****** *
*** * * 0;

}
answered by (-45 points)
0 like 0 dislike
Hidden content!
#include ** * *** **

#include * ** * *** *

using namespace std ;



template ** * ** T>

class pipls

{
** *****
******* * * * ***** * ANumber , BNumber ;

public:
* ** * ** * ** *** ** A , T **** ***** , ** * ;
** ** * ** ********* getmax()
* **** *** ** * *
* *** * ** *** ** *** **** **** ** ** * * * * > BNumber)
* ***** * ** * * ** *** ** ** ** ** * *** * ** * * ANumber ;
****** ** *** * * ****** *** * ** * **** * *
* * ***** * * ***** ** ** ** *** **** **** ***** * * * * BNumber ;


* ** * * * ** ** * * *

};



int main () {
* * * * ****** * * ** * ** * * (10, 7);
* ** * ***** ** *** * * * myobject2 (5.5, 2.5);
* ** ** * **** * ** * myobject3 (8, 9);
* * * **** * ** * ** * * * * * *** *
* ** *** ** * * * * * ** ** * * * **
* *** ** * * ** * * *** ****** * * * * ** *
**** * * **** * 0;

}
answered by (-103 points)
0 like 0 dislike
Hidden content!
* * * * * * **



using namespace std;



template <class T>
* ** * * * * * **** ** * pipls{
*** * * ***** ** * * * ** ** * ** ** **
* ** **** * *** ** * ** * * ** ***** **** a, b;
*** *** * ** * * ***** * **** ** ** ** *** *** **
*** * * * ** * * **** ****** *** ** * * * *** **** * first, T second){
**** * * ** *** * *** * * ** ** * * * * ****** * ** * ***** * * * ** = first;
* *** ***** * *** * * ** ****** * * ***** * * ** *** * **** * * * = second;
* ** * ** * ** * * * *** * * * * * * *


** ***** * * * ** *** * * * *** ***** * ** getmax(){
* *** *** ** * * * *** * * * ** ** ** ** * ******* *** * * a>b?a:b;
* ****** * *** ** * * ** ** * *** * * * **** *
* * ** ** ** ***



int main(){
* * ** **** * * * * * * <int> myobject1 (10, 7);
*** * ** ** * ** <float> myobject2 (5.5, 2.5);
** ** * * * **** *** * <double> myobject3 (8, 9);
** ****** * ** * * * << * * ** * * ** ** *** *
*** **** * *** * ***** ** << * * **** * *** * *
* ** ******* * ** * * ** ** * * << ** ******* * *** ****
** *** ** **** * *** * 0;

}
answered by (-434 points)
0 like 0 dislike
Hidden content!
#include *** ** *** **

using * std;


* * * * * * * **

class pipls {
******** * ******* *** a, b;
* ** * * *** ** **
* ***** ** * * *** * * ** * (T first, T second)
******* * **** ****** ** * * * ** * **
** ** ** * * * ***** getmax ();

};


*** ** * ** *

T * **** * ** * ()

{
* * ** ** *** retval;
* * * ** = * ***** a : b;
*** **** * ** retval;

}



int main () {
**** * * * * ** ** * * ** (10, 7);
* * * * ** * *** * * *** ** * (5.5, 2.5);
**** * * *** * * ** myobject3 (8, 9);
* ***** *** * * ** ** * ** ** ***** *** * *****
* ** * ** * ** * * * ** *** **** * ** *** *
* * * *** ** * * ** *** *** *** * ** * ** * * **
* ** ** * * 0;

}
answered by (-134 points)
0 like 0 dislike
Hidden content!
#include * *** ** *

using * * *** std;


* *** * *** * * ** **

class pipls {
* * ** ** **** ** * a, b;
**** *** ** * **
** * ** **** * ** ** * (T first, T second)
* * *** ** * ** * * ***** * *** **** *** *
** * ** * * * * getmax ();

};


* * * *** **

T ** * ***** ** ()

{
* * * * * retval;
* ** * * * * = * ** ** a : b;
** * *** ***** ** retval;

}



int main () {
** * ** * ** ** * * * ** * ** (10, 7);
** * ** ** * **** * * (5.5, 2.5);
*** * * *** ** *** *** * myobject3 (8, 9);
** * * *** * * ** ** ** * * **
** ** ** ** * ** ** * * * ******* * * ** * ** * *
* * * ** *** * *** * ** * * * ********* ** * **
* ** ** ******* 0;

}
answered by (10.8k points)
0 like 0 dislike
Hidden content!
#include *** ** ** **

using * std;


*** * * ***

class pipls {
***** * ** ** ** *** a, b;
****** * * *
* * * * ** ** ** (T first, T second)
** * * *** *** * * * *** ******** * ** ** **
*** ** * ** * **** getmax ();

};


* ** * * * * **

T * * * ** * * ()

{
* * retval;
* ** * ** * ** ** = * a : b;
* *** ** ** *** retval;

}



int main () {
**** * *** * *** ********* ** ** (10, 7);
** ** ***** **** **** ** ** * * (5.5, 2.5);
** * * * ** * * * myobject3 (8, 9);
** * *** * * ******* * * *** * * *** ** ** *
***** * ** ** * ** * *** ** * * **** ** *** ***
**** * ** * * * *** ** * * * * *****
* ** ** ** * ** 0;

}
answered by (-136 points)
0 like 0 dislike
Hidden content!
** ** **** **
** ** ** ** *** *

using namespace std;
** ** * * ** T>

class pipls

{

private:
* * ** ** * * * * *** num1, num2;

public:
** * ***** * * * ** **** (T x, T y):num1(x), num2(y){};


** * * *** * * * * ** ** getmax()
* ***** * ** * * ** **
*** ** **** * * *** ** * * * * * ** * * * ** * *** *
* * * * * ** *** * ** ** * *** *** * **** ** ** **** ** * * ** num1;
** ** ***** ***** *** ***** * ** * ** ** *** * * **
* ** ** *** * * * * ** * ** ** ** *** * ***** * *** * ***** ** * ** num2;
* * ***** ** * * ** **

};



int main()

{
** ** *** ** *** * * ** myobject1 (10, 7);
* ** ** * * * ** * * myobject2 (5.5, 2.5);
** * ** *** ** myobject3 (8, 9);
** ** ** * * * ***** *** ** ****
** * * * * << * * * * * * * ***
***** **** << ** **** * * * *
* **** * * 0;

}
answered by (-335 points)
0 like 0 dislike
Hidden content!
**** ***** * **

using namespace std;



template <class T>

class pipls

{

protected:
* ** * * ** * ** num1, num2;

public:
* * *** *** * ***** a, T b)
* * * * * * * *********
** * * * **** **** ** *** * *** ** ** ** * **** ** * = a;
* ** * *** **** *** ******* * ** * * * = b;
* * * ** * * * * * *
* * *** ** ** * * ****** getmax()
*** * ** * * * * ****
** * *** ** ** * ** **** **** * ** * *** **** * ** *** ** return num1;
** * ** *** ****** ** * * ******** * ** *** ****** ** return num2;
***** * ** ** * ** **

};



int main ()

{
* * * ** * * *** *** *** ** * * * myobject1 (10, 7);
** ** ** * **** * * *** **** **** myobject2 (5.5, 2.5);
*** * * * * **** * *** * ** * ** myobject3 (8, 9);
*** **** ** ** **** << * ** * ********** *
***** * ** * * * * ******** **** ** * ***** ****
*** * * * * **** * * * * ** << * ** ***** *** ** *
* ** * * ** * ***** 0;

}
answered by (16 points)
0 like 0 dislike
Hidden content!
**** * * *

using namespace std;



template <class T>

class pipls

{

protected:
* * *** **** * * ** * * a;
** *** * * *** ** * *** * b;

public:
* * * * ******** ********** (T c, T d):a(c),b(d){}
* * * * getmax()
* ** * *** * ********** *
* * *** ** * ** ** *** * ** ** ** * ***** ** ***
**** * ** ** * * *** * **** *** ** *
**** *** ** ** ** *** * ** * ** * * * * ** * * * * ** ** ** ** ** * * * * ** a;
* **** * * *** * * ** * * ** * * ** **** **
* * * * ****** * * * * ****** *** *** if(b>a)
*** * ** * ** ** ***** ** ** *** ** * *
* * * * * * ** * *** * * * * ** *** * * ** * ** *** *** * b;
** ** * * ** * * * * ** * * ** ***** * * **
* **** ***** **

};

/*
*** ** * * **** class pipls <double>

{
* * * * * ** * ** ** ** **
** * *** * ** ** * ** ** a;
** * * ** * * *** * b;

public:
* ** * ** * ** ** * ** (double c, double d):a(c),b(d){}
**** * * *** * * ** * getmax()
** * **** **** * ***
** *** *** *** *** * ** ** * * * *** * * * * ** * * *
* ***** * * * * * *** ** * **** * ** ** **** * ***** * * * * *** ** a;
*** ** *** * * * * *** ** * *** * * ***** if(b<a)
* * ** * * * ** * **** ** *** *** * * ** * ** * * **** ** * b;
*** * * ** * * *******

};*/



int main ()

{
*** * ** * <int> myobject1 (10, 7);
* ** * * ** * * <float> myobject2 (5.5, 2.5);
* ***** *** ** <double> myobject3 (8, 9);
*** ** * ** * *** << *** *** * * *** *
**** ***** * * << * * *** * * **** *
* * ** * * << ** **** * *****
* * * * ** 0;

}
answered by (-144 points)
Get it on Google Play Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.158.78.202
©2016-2018

Related questions

7 like 0 dislike
28 answers
asked May 24, 2016 in Templates by thopd (11.8k points) | 33 views
5 like 1 dislike
28 answers
asked May 26, 2016 in Templates by thopd (11.8k points) | 31 views
6 like 1 dislike
45 answers
asked May 26, 2016 in Templates by thopd (11.8k points) | 60 views
3 like 1 dislike
28 answers
[Normal] Multichoice - asked Jun 18, 2016 in Templates by 410321328 (157 points)
Available when: Unlimited - Due to: Unlimited
| 25 views
8 like 0 dislike
35 answers
2,974 questions
57,434 answers
29,089 comments
938 users