UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Templates

Darpan Kumar
27/05/2017 0 0

Templates 

Advantages of templates:

  • Code reuse
  • Allows container classes (e.g. lists, arrays, etc.) to be simply defined without loss of static type checking or run-time efficiency.
  • Allows definition of generic functions (e.g., sort) to be defined once for a family of types

Function template and template function:

  • Function templates are used to implement an entire range of related(overloaded) functions.
  • When the actual parameter is substituted in the template the process is called instantiating the function template and each instantiated version is called a template function. i.e. template function is a specific instance of function template.

Class templates and template class:

  • Class templates are used to implement an entire range of related classes.
  • Template class is the instantiation with the type argument.

 Auto_ptr:

  • Auto_ptr is a template class and is initialized with a pointer and can be derefenced in the way a pointer can.
  • Auto_ptr supports resource acquisition is initialization technique.
  • Auto_ptr has ownership semantics or destructive copy semantics: When an auto_ptr is copied into another, the source no longer points to anything.
  • Copying a auto_ptr modifies it – a const auto_ptr cannot be copied.
  • The purpose of the auto_ptr scheme is that dynamically allocated objects are properly destroyed in all circumstances i.e. the object’s destructor is properly executed.
  • Template specification of auto_ptr:

 

template

class auto_ptr

{

public:

        typedef T element_type;

       

        explicit auto_ptr(T *p = 0) throw();

       

        auto_ptr(const auto_ptr& rhs) throw();

        auto_ptr& operator=(auto_ptr& rhs) throw();

       

        ~auto_ptr();

       

        T& operator*() const throw();

        T *operator->() const throw();

       

        T *get() const throw();

        T *release() const throw();

};

 

 

Example:

 

#include

#include

using namespace std;

 

class X

{

public:

X() { cout << “X::X()\n” ;}

~X() { cout << “X::~X()\n” ;}

void f() { cout << “X::f()\n”; }

};

 

int main()

{

auto_ptr p1(new X), p2;

 

p2 = p1;       // ownership transfer

p2->f();

 

X *ptr = p2.get();

ptr ->f();

 

return 0;

}

 

Result:

X::X()

X::f()

X::f()

X::~X()         (Destructor called when scope ends)

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

PRACTISE makes you PERFECT ; ; ; There is no SUBSTITUTE for HARD WORK ;;;;Breathe SUCCESS like OXYGEN
Proper Planning ( reg what portions to be covered today) revising today's class portions & clarifying doubts solving Maths problems regularly ,noting down formulae separately trying to understand...

Necessity of Theory and Practical in Computer Science.
Upon studying a subject both theory and practical are important. Usually many schools concentrate more on theory and the marks not on the practical. Other and opposite kind of people prefer practical...

Compiler vs Interpreter
Compiler Interpreter Convert all the code into binary format and execute. Convert one statement at a time and execute, then Convert next statement and execute. After conversion, source...

C++ Program-Factorial
//Header files #include<iostream.h>#include<conio.h> //Main function void main(){ int num,i; long factorial=1; //Function for clearing screen clrscr(); cout<< "Enter a number:";...

File Handling in C++ : Lesson 1 Data Persistence
Data persistence refers to the existence of data in the program. In other words, it means the life of data. Data is the most significant part of a program and it must be stored properly and easily retrieved...
J
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more