Object Oriented Programming: Name: Muhammad Huzaifa Hassan Khan Class: BCE - 4 Enrollment No: 01-132192-043

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Object oriented programming

Name: Muhammad Huzaifa Hassan khan


Class: BCE_4
Enrollment No: 01-132192-043
ASSIGNMENT 4

TEMPLATES

A template is a simple and a powerful tool in C++. The idea of using the template is that we pass the
data type as a parameter so that we don’t need to write the same code of different types. In simple
term you can create a single function or a class to work with different data types using templates.

Templates are used in larger codebase for the purpose to rerun the code again and again.

Templates can be used in two ways:

1. Function templates
2. Class templates

Function templates:

A function template is simple to normal function.

A single function template can work with different data types at once but, a single normal function can
only work with one set of data types.

How to declare a function template:

#include <iostream>

using namespace std;

// template function

template <class T>

T Large(T n1, T n2)

return (n1 > n2) ? n1 : n2;


}

int main()

int i1, i2;

float f1, f2;

char c1, c2;

cout << "Enter two integers:\n";

cin >> i1 >> i2;

cout << Large(i1, i2) <<" is larger." << endl;

cout << "\nEnter two floating-point numbers:\n";

cin >> f1 >> f2;

cout << Large(f1, f2) <<" is larger." << endl;

cout << "\nEnter two characters:\n";

cin >> c1 >> c2;

cout << Large(c1, c2) << " has larger ASCII value.";

return 0;

}
A function template starts with the keyword template followed by template parameter/s inside

 < > 

which is followed by function declaration.

Working of function template:

#include <iostream>  
Using namespacestd;  
template<class X,class Y> void fun(X a,Y b)  
{  
    std::cout << "Value of a is : " <<a<< std::endl;  
    std::cout << "Value of b is : " <<b<< std::endl;  
}  
  
int main()  
{  
   fun(15,12.3);  
   
   return 0;  
}  

How do template work?

The idea is that source code contains only function/class, but compiled code may multiple copies of
same function /class.
Class template

Class template like function template are useful in when a class defines something that is independent
of the data type.

Syntax

Template<class type>

Class class_name

Another example
#include<iostream>

Using namespace std;

Template<class T>

Class M {

Public:

T num1 = 5;

T num1=6;

Void add()

Std::cout<<”Addition of num1 and num2 :”<<num1+num2<<std::endl;

};

int main()

A<int>d;

d.add();

return0;

Example

#include<iostream>

using namespace std;


 
template<class T, class U>
class A  {
    T x;
    U y;
public:
    A() {    cout<<"Constructor Called"<<endl;   }
};
 
int main()  {
   A<char, c(har> a;
   A<int, double> b;
   return 0;
}
Q2

Writing as a to create and write a file

To create a file, use either ofstream or fstream class, and specify the name of the file.

To write to file, use the insertion operator(<<).

#include<iostream>

Using namespace std;

Int main()

Ofstream myfile(“filename.txt”);

Myfile<<”files are created in this way”;

My file.close();

Reading the file

To read the file we use either ifstream or fstream class and the name of class.

Ofstream Creates and writes to file


Ifstream Read from files
Fstream A combination of fstream and ifstream:
creates,reads and writes to file

You might also like