Templates or Generics in C++ Part 5 | C ++ Tutorial | Mr. Kishore

Поділитися
Вставка
  • Опубліковано 13 гру 2024

КОМЕНТАРІ • 27

  • @NareshIT
    @NareshIT  5 років тому +1

    Please Comment, Subscribe and Click Bell🔔🔔🔔 Icon for More Updates. To learn software course from our experts please register here for online training: goo.gl/HIB0wL

  • @radhagoyal3606
    @radhagoyal3606 2 роки тому +1

    Completed all the videos till here. Great journey

  • @JANNATULFARDOUS-ko5lx
    @JANNATULFARDOUS-ko5lx 2 роки тому

    I watched all of the videos on templates in c++
    All are awesome....Loved it.....
    Thanks you....

  • @Forpc-h9d
    @Forpc-h9d Рік тому

    Thankyouu so much kishore sir, your explanation is just amazing❤❤

  • @NarutoUzumaki-zv4wm
    @NarutoUzumaki-zv4wm 3 роки тому

    Thank you sir
    Good quality lectures

  • @raghavamopidevi9700
    @raghavamopidevi9700 Рік тому

    #include
    using namespace std;
    template
    class test
    {
    t a,b;
    public:
    void get()
    {
    cin>>a>>b;
    }
    t sum();
    };
    template
    t test::sum()
    {
    return (a+b);
    }
    int main()
    {
    testt1;
    testt2;
    cout

  • @badrinath4902
    @badrinath4902 Рік тому

    In this program a and b are private variables, then how can you use it in outside the class sir.

  • @chahatvamdev4088
    @chahatvamdev4088 2 роки тому

    Thanku So Much Sir

  • @madhavbovilla7363
    @madhavbovilla7363 6 років тому

    Excellent good basics

    • @anonymous13378
      @anonymous13378 7 місяців тому

      please tell where are you now after 6 years ....i am in first year btech cse ....plz tell the mistakes i should avoid in order to get better at coding

  • @mamathadasari6797
    @mamathadasari6797 3 роки тому

    I want to use different type datatypes using template class, for example one data member int, another one float. Is it possible, if possible how to declare object

    • @NarutoUzumaki-zv4wm
      @NarutoUzumaki-zv4wm 3 роки тому +1

      Declare another template variable, then compiler will automatically allow operations of two different datatypes.
      Ex: template< class t, *class t1* >

  • @neelrachh6956
    @neelrachh6956 5 років тому +1

    If a int and b float then ??

    • @shivaprasadgv8622
      @shivaprasadgv8622 5 років тому +1

      I too have same question

    • @shivaprasadgv8622
      @shivaprasadgv8622 5 років тому +1

      One possible solution would be.....if we enter one int type and another one float...it will go for float only

    • @nishantdeo1188
      @nishantdeo1188 4 роки тому

      somebody please answer this question

    • @kunal2145
      @kunal2145 3 роки тому +1

      So if a is int and b is float, first declare template with 2 placeholders ie
      Template
      Then declare the object using
      Test t;

  • @sasikumartangala5001
    @sasikumartangala5001 6 років тому +1

    Sir disadvantages of templates

    • @celiafernandes9084
      @celiafernandes9084 6 років тому +1

      program size is reduced ,you can create single function for several familiar functions.

    • @celiafernandes9084
      @celiafernandes9084 6 років тому +3

      Disadvantages
      Historically, some compilers exhibited poor support for templates. So, the use of templates could decrease code portability.
      Many compilers lack clear instructions when they detect a template definition error. This can increase the effort of developing templates, and has prompted the development of Concepts for possible inclusion in a future C++ standard.
      Since the compiler generates additional code for each template type, indiscriminate use of templates can lead to code bloat, resulting in larger executables. For example, used in Adobe products "… GIL (Generic Image Library) implements type generators.
      One of these generators generates all image types that are combinations of given sets of color spaces and channels.
      This code defines any image t to be one of 4×3×2×2 = 48 possible image types. It can have any of the four listed color spaces, any of the three listed channel depths, it can be interleaved or planar and its pixels can be adjacent or non-adjacent in memory.
      The above code generates 48 × 48 = 2304 instantiations. Without any special handling, the code bloat will be out of control."
      See Efficient Run-Time Dispatching in Generic Programming with Minimal Code Bloat, 2004.
      Because a template by its nature exposes its implementation, injudicious use in large systems can lead to longer build times.
      It can be difficult to debug code that is developed using templates. Since the compiler replaces the templates, it becomes difficult for the debugger to locate the code at runtime.
      Templates are in the headers, which require a complete rebuild of all project pieces when changes are made.
      No information hiding. All code is exposed in the header file. No one library can solely contain the code (from Wikipedia).
      Though STL itself is a collection of template classes, templates are not used to write conventional libraries.
      The libraries of templates are header-only: the library code is included in and compiled with the user's code.
      Though, this makes installation and usage of the libraries relatively easy.
      In The C++ Programming Language (3rd Edition), B.Stroustrup presents over 20 factors to take into account when programming templates.
      Many of them have to do with ensuring that your code is reliable for all input classes, and maintainable.
      B. Stroustrup recognizes these pitfalls:
      The ease with which unmaintainable "spaghetti code" can be generated
      Automatically generated source code can become overwhelmingly huge
      Compile-time processing of templates can be extremely time consuming
      Debugging is not intuitive for most programmers
      Context dependencies can be difficult to diagnose and even harder to correct (from comments here)
      And here is B. Stroustrup's more recent view on templates usage:
      Prefer a template over derived classes when run-time efficiency is at a premium
      Prefer derived classes over a template if adding new variants without recompilation is important
      Prefer a template over derived classes when no common base can be defined
      Prefer a template over derived classes when built-in types and structures with compatibility constraints are important
      See Joint strike fighter air vehicle C++ coding standards, December 2005.
      In sharp contrast to the claim that templates cause code bloat, it so happens that templates can be used to save code space.
      C++ compiler is not allowed to generate code for an unused template function. This implies that if a program uses only 3 of a template class’ 7 member functions, only those three functions will occupy space in memory. The equivalent optimization for non-template classes is not common (the standard doesn’t require it) and extremely hard to achieve for virtual functions.

  • @prithvirajsingh8537
    @prithvirajsingh8537 6 років тому

    Thank u sir .....

  • @spoorthis7732
    @spoorthis7732 6 років тому

    can we create two template variable in class .

  • @whoisnegii
    @whoisnegii 5 років тому

    is "t sum( )" is a member function ?

    • @006daredevil
      @006daredevil 5 років тому +1

      Yes, Any functions which are declared in same class is known as member functions of that class.

    • @kartikpandey1950
      @kartikpandey1950 4 роки тому +1

      @@006daredevil in this case it is member funtion but not in every case it is true for eg say friend function it is also declared inside class
      but define outside class

    • @006daredevil
      @006daredevil 2 роки тому

      Yes Except Friend Function which is a non member function of class. But I have to correct you in this that friend function can be defined inside a class also.