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

Static and dynamic libraries
A library is a package of code that is meant to be reused by many programs. A static library (also known as an archive) consists of routines that are compiled and linked directly into your program. When...

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

Lets learn why OOPS is popular?
1. Supports reusability- It means that once a class is designed it works just like a template which can be inherited. 2. Provides better management- It means that abstract classes and interfaces guide...
R

Do You Know How Is Size Of Structure Defined?
Size of the structure is defined based on multiplies of bigger data type member in the structure. Example: If a structure contains integer, char, short data type, then size of the Structure will be multiples...

Efficient Learning Strategies
Type your notes after class Write your notes onto flashcards - Scrabble -Make posters Review flashcards while walking, at gym, etc. Dog-ear pages in the reading where you can find...

Looking for C++ Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
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