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

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:";...
S

Polymorphism In C++
Basically polymorphism represents poly means many and morph means forms that many forms. In which we are passing same message to different objets but every object will work for that message in their own...

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

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-Working with constant using const keyword
//Header files #include<iostream.h>#include<conio.h> //Main function void main(){ //using const keyword to declare constants with a specific type const int len=10; const int br=5; const...
S

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