UrbanPro
true

Learn C++ Language from the Best Tutors

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

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

Memory Layout in C++ vis-a-vis Polymorphism and Padding bits.
I know there is no need for the knowledge of memory layout for a normal day-to-day development of C++. However, when someone goes in the bit/byte level of C++, he needs to look for such discussion. To...

C++ Program-Single Dimension Array
/* WAP to print sum and largest value in a single dimension array*/ //Header files #include<iostream.h>#include<conio.h> //Main function void main(){ //Array declaration int num,i,sum,large;...

Understanding Computer Science Concepts with Images and Videos..
All Computer science concepts relating to programming and software development are only virtual. It cannot be practically shown as a hardware parts of a computer. But for better understanding it should...

Why Indexing Should Start From Zero In Array ?
Why numbering should start at zero? To denote the subsequence of natural numbers 2, 3, ..., 12 without the pernicious three dots, fourconventions are open to usa) 2 ≤ i < 13b) 1 < i ≤ 12c)...

C++ Program Sample Application
//Standard Library Functions(Headers Files Used in C++) #include<iostream.h> #include<conio.h> //Main function int main() { //function for clearing the screen clrscr(); //function for...
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