You are on page 1of 3

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 12
(Spring 2018)

Course: Object Oriented Programming Lab Date:


Course Code: CSL - 210 Max Marks: 40
Faculty’s Name: Aasim Ali Lab Engineer: Shoaib Khan

Name: Mohammad Jasim Waqar Enroll No: 03-135172-055

Task 1:
Create a class Rectangle with data members length, breath and height. Members
functions of class should be Volume and Compare, to calculate volume of the box and
compare two objects’ volume to check the object with greater volume.
(Implementation : using this pointer)

ANSWER:
#include<iostream>
using namespace std;

template <class T>


class rectangle{
T length ;
T breadth ;
T height ;

public :
rectangle(){}
T volume (T a, T b , T c){
length=a ;
breadth=b ;
height =c;
return length*breadth*length;
}

};
template <class T>
T compare (T obj , T obj2){

if (obj> obj2){
cout<<obj<<endl;
}

else
cout<<obj2<<endl;
return 0;
}

Department of Computer Science, BULC


int main(){
rectangle <double> obj;
rectangle <double>obj2;
double a=obj.volume(12.5,3.4,5.5);
double b= obj2.volume(3.2,8.4,0.15);
compare<double>(a,b);
return 0;
}

Task 2:
Write a simple function template for predicate function isEqualTo that compares its two
arguments of the same type with the equality operator (==) and returns true if they are equal
and false otherwise. Use this function template in a program that calls isEqualTo only with a
variety of fundamental types.

ANSWER:
#include<iostream>
using namespace std;

template <class T>


T isEqualto (T value1,T value2){

if (value1 == value2)
return true;

else
return false ;
}

int main(){
int a =67, b=77;
double m=55,n=55;
float c=33 , d=65;
cout<<isEqualto<int>(a,b)<<endl;
cout<<isEqualto<double>(m,n)<<endl;
cout<<isEqualto<float>(c,d)<<endl;
return 0;
}

Task 3:
Write a template function that returns the average of all the elements of an array. The
arguments to the function should be the array name and the size of the array (type int). In
main(), exercise the function with arrays of type int, long, double, and char.
ANSWER:
#include<iostream>
using namespace std;

template <class T>


T isEqualto (T *arr){
T sum=0;

Department of Computer Science, BULC


T avg=0;
for (int i=0 ; i<5;i++){
sum= sum+arr[i];
}
return sum/5;

int main(){
int arr[5]= {44,55,3,2,1};
float crr[5]= {4.4,5.5,3.2,2.7,1.1};
cout<<isEqualto<int>(arr)<<endl;
cout<<isEqualto<float>(crr)<<endl;
return 0;
}

Lab Grading Sheet :


Max Obtained
Task Comments(if any)
Marks Marks
1. 10
2. 10
3. 10
4. 10

Total 40 Signature

Note : Attempt all tasks and get them checked by your Lab Instructor.

Department of Computer Science, BULC

You might also like