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

C++ Program[Working with structure]
//Header files #include<iostream>#include<cstring> //Namespace usedusing namespace std; //Structure definition struct store{ int p_id; char p_name; int quantity; float price; float amount; }; //...
S

What Are IT Industries Performance Metrics?
1. Outstanding Expectation: Eligible to get Promotion easily and good salary hike. Always preferrable to go abroad. 2. Exceed Expectation: Can get Promotion as per schedule of company with good salary...

Programing Languages Learning Tricks
You want to learn that new language or library or framework as soon as possible, right? That’s understandable. Fortunately, there are a handful of tips that can help you to better retain all of that...
H

Harshal G.

0 0
0

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

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)...
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