0% found this document useful (0 votes)
28 views3 pages

Max and 2nd Max

Uploaded by

Tanvir Tareq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Max and 2nd Max

Uploaded by

Tanvir Tareq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

***1)Write a generic function which takes different types of arrays to find highest value.

Solution:

1. #include <iostream>
2. using namespace std;
3. template<typename T>
4. class Max{
5. public:
6. void max_function(T a, T b, T c, T d, T e){
7. T arr[5] = {a, b, c, d, e};
8. T max1 = arr[0];
9. for(int i = 0; i < 5; i++){
10. if(arr[i]>max1){
11. max1 = arr[i];
12. }
13. }
14. cout << "Maximum Number: " << max1 << endl;
15. }
16. };
17. int main() {
18. Max<int> m;
19. m.max_function(9,-2,3,-1,4);
20. }

***2) Write a generic function which takes different types of arrays to find second highest value.

Solution:

1. #include <iostream>
2. using namespace std;
3. template<typename T>
4. class Max{
5. public:
6. void max_function(T a, T b, T c, T d, T e){
7. T arr[5] = {a, b, c, d, e};
8. T max1 = arr[0];
9. T max2;
10. for(int i = 0; i < 5; i++){
11. if(arr[i]>max1){
12. max2 = max1;
13. max1 = arr[i];
14. }
15. else if (arr[i]>max2 && arr[i]<max1){
16. max2 = arr[i];
17. }
18. }
19. cout << "2nd Maximum Number: " << max2 << endl;
20. }
21. };
22. int main() {
23. Max<int> m;
24. m.max_function(9,-2,3,-1,4);
25. }

3) Define a class ‘Square ’ having two member variables ‘length’ and ‘width* and a member
function area(). Overload > operator to perform the following statements.

main(){
Square ob1(5.1),0b2(3.2);
if( ob1>0b2)
cout<<"ob1 is greater than ob2”;
}

Solution:
1. #include <iostream>
2. using namespace std;
3. class Square {
4. private:
5. int length;
6. int width;
7. public:
8. Square(int l, int w) {
9. length = l;
10. width = w;
11. }
12. int area() {
13. return length * width;
14. }
15. bool operator > (Square other) {
16. return area() > other.area();
17. }
18. };
19. int main() {
20. Square ob1(3, 2), ob2(3, 2);
21. if (ob1 > ob2)
22. cout << "ob1 is greater than ob2" << endl;
23. else if (ob2 > ob1)
24. cout << "ob2 is greater than ob1" << endl;
25. else
26. cout << "ob1 and ob2 are equal" << endl;
27. return 0;
28. }

4) Implement a code to execute the statement successfully:

1. 01 = (02+10)-O3;
Solution:

1. #include<iostream>
2. using namespace std;
3. class Area {
4. public:
5. int length, breadth;
6. Area(){}
7. Area(int l, int b){
8. length = l;
9. breadth = b;
10. }
11. Area operator + ( int a ){
12. Area temp;
13. temp.length= length + a;
14. temp.breadth= breadth + a;
15. return temp;
16. }
17. Area operator - ( Area obj ){
18. Area temp;
19. temp.length= length - obj.length;
20. temp.breadth= breadth - obj.breadth;
21. return temp;
22. }
23. void print(){
24. cout<<"length : "<<length<<endl;
25. cout<<"breadth : "<<breadth<<endl;
26. }
27. };
28. int main (){
29. Area o1, o2(10, 12), o3(2, 4);
30. o1 = (o2 + 5) - o3;
31. o1.print();
32. }

You might also like