SlideShare una empresa de Scribd logo
1 de 48
CS212: Data Structures and
Algorithms

    Lecture # 1
    Revision
      •   OOP Concepts
      •   Templates
Outline

   Class and Object
   Constructor
   Constructor Initializer List
   Destructor
   Default Initialization Values
   Function Templates
   Class Templates



                                    2
Class
   A class is a programmer-defined data type. It
    consists of data and functions which
    operate on that data.


   Placing data and functions together into a
    single entity is the central idea of object-
    oriented programming.

                                                   3
Class Declaration Syntax

class Name       // usually capitalized
{
  public:
      public members; // usually functions

     private:
         private members; // usually variables
};
        Note

                                                 4
Example of a Class Declaration
#include<iostream>
using namespace std;
class ASimpleClass //declare a class
{
private:
   int data; //a private member
public:
   void setData(int item)
         {
         data = item;
         }
   void displayData()
         {
         cout<<"Data is : "<<data<<endl;
         }
};                                       //program continues …   5
Example of a Class Declaration
void main()
{
ASimpleClass asp; //creates an object
asp.setData(10); //member access using dot operator
asp.displayData();
asp.setData(20);
asp.displayData();
}



                                          Output
                                          Data is : 10
                                          Data is : 20
                                          Press any key to continue
                                                                      6
A Few Terms

       class
       class member
       data member
       member function

       class object (class instance)


                                        7
Objects

   An object is an instance of a class.

   Similarity:
       int row;

   This creates an instance of int called “row”.




                                                    8
Calling Member Functions
   asp.setData(10);
   asp.displayData();
   These two statements are not like normal function calls.
   This strange syntax is used to call a member function that is
    associated with a specific object.
   A member function must always be called in connection with an
    object of the class.
   Because a member function is always called to act on a specific
    object, and not on the class in general.
   The dot operator connects the object name and the member
    function.
   Dot operator is also called class member access operator.

                                                                      9
Working with Objects

ASimpleClass asp1,asp2;
asp1.setData(10);
asp2.setData(20);
                                     ASimpleClass
                                     int data
                                     void setData();
                                     void displayData();


       Two objects of ASimpleClass

                                        data               data
                                         10                 20

                                        asp1               asp2
                                                                  10
Part Example
#include<iostream>                       partno=pn;
using namespace std;                     cost=c;
class Part                               }
{                                      void showPart()
                                       {
 int modelno;
                                       cout<<"Model :
 int partno;                           "<<modelno<<endl;
 float cost;                           cout<<"Part :
public:                                "<<partno<<endl;
    void setPart(int mn, int pn,       cout<<"Cost : "<<cost<<endl;
    float c)                           }
    {                              }; //program continues …
    modelno=mn;
    //continues on right side…                                    11
Part Example Cont…

void main()
{
Part part;
part.setPart(555,100,100);
part.showPart();
}


                             Output
                             Model : 555
                             Part : 100
                             Cost : 100
                             Press any key to continue
                                                         12
Constructor

   Setters provide a way of initializing class data
    members.


   However, sometimes it is convenient that an object
    can initialize itself when it is created, without the
    need to call setters.



                                                            13
Constructor

   Automatic initialization is carried out using a special
    member function called a constructor.

   A constructor is a special member function provided
    to allow initialization of variables.

   Constructor is executed automatically whenever an
    object is created.


                                                              14
Counter Example (with constructor)

//counter.h
class Counter
{
private:
     unsigned int count;
public:
     Counter() {count=0; } //constructor
     void incCount();
     unsigned int getCount();
};

                                           15
Counter Example (with constructor)

//counter.cpp
#include"counter.h"
unsigned int Counter::getCount()
   {
   return count;
   }
void Counter::incCount()
   {
   count++;
   }

                                     16
Counter Example (with constructor)

//driver.cpp
#include"counter.h"
#include<iostream>
using namespace std;
void main() {
    Counter c; //creates an object and initializes count
    cout<<c.getCount()<<endl;
    c.incCount();
    cout<<c.getCount()<<endl;
    }
                                                           17
Constructor

   Must have the same name as the class itself
       Name is one way the compiler recognizes them as
        constructors.


   Do NOT have a return type (not even void)
       Why not?
       Since the constructor is called automatically by the
        system.
       This is the 2nd way the compiler knows they are
        constructors.

                                                               18
Default Constructors

   Default constructor
    parameter-less constructor, it initializes the variables
    to some default value, such as zero


   Example
        TimeType();
        Student();


                                                           19
Default Constructors

Example 1               Example 2

                        Student::Student( )
TimeType::TimeType( )
                           {
  {
                           GPA = 0;
  hrs = 0;                 num_of_grades = 130;
  mins = 0;                }
  secs = 0;
  }



                                                  20
Default Constructors

   An implicit default (no-argument) constructor is built
    into the program automatically by the compiler.


   This constructor is used to create the objects when
    we don’t write a constructor.




                                                             21
Initializer List
Counter():count(0)
  {
  } //constructor

Is equivalent to

Counter()
  {
  count=0;
  }
 Initialization takes place following the member function declaration

  but before the function body.
 It is preceded by a colon

 The value is placed in parentheses following the data member.
                                                                         22
Initializer List
   Initializer list is also called member-initialization list.

   If multiple members are to be initialized, they are
    separated by commas.

   Example
       someClass():m1(5),m2(42),m3(3) { }


   Actions complicated than simple initialization must
    be carried out in the constructor body.
                                                                  23
Overloaded Constructors
   A default constructor initializes data members at default values
    such as zero.
   Example
       Distance() : feet(0),inches(0.0){ } //default constructor
   It is convenient to be able to give data members a value (other
    than default value) when the objects are created.
   Example
       Distance dist1(5,6.25);
       Defines a Distance object named dist1 and initializes feet
        at 5, and inches at 6.25
       For this, we need a constructor like this:
       Distance (int f, float i) : feet(f),inches(i) { }
                                                                       24
Overloaded Constructors
   We have two constructors for Distance
       Distance() : feet(0),inches(0.0) { }//default constructor
       Distance(int f, float i) : feet(f),inches(i) { }
   These two explicit constructors with the same name Distance
    makes the constructor overloaded.
   Which of these is executed to create an object depends on
    how many arguments are used in the object creation.
       Distance dist1;//calls default constructor
       Distance dist2(11,6.25); //calls constructor with two
        arguments

                                                                    25
The Default Copy Constructor

   We have seen two variations of constructor.
   There is another way of initializing a constructor.
   An object can be initialized with another object of the same type.
   This type of constructor falls in the category of implicit
    constructors.
        We don’t need to create a constructor for this.
        One is already built into all classes.
   It is called the default copy constructor.
   It is one argument constructor whose argument is an object of
    the same class as the constructor.

                                                                     26
The Default Copy Constructor

   Default copy constructor can be invoked in two ways:
   Example:
       Distance dist1(11, 6.25);
       Distance dist2(dist1); //calling default copy constructor
       Distance dist3=dist1; //calling default copy constructor
   Both the ways have the same results and causes a member-to-
    member copy
   The second way looks like an assignment statement, but it is not.
   Both the formats invoke the default copy constructor.

                                                                    27
Destructors
   Another function is called automatically when an object is
    destroyed.


   Such a function is called a destructor.


   Destructor has the name as the constructor (i.e. the class name)
    but is preceded by a tilde (~).


   The most common use of destructor is to de-allocate memory
    that was allocated for the object by the constructor.

                                                                   28
Destructors

   ~Classname( )
       A default do-nothing destructor is provided by the
        compiler.
       Only one destructor per class
       No arguments
       No return type




                                                             29
Templates

   C++ supports code reuse in different ways.


   The template feature in C++ provides way to reuse
    source code.


   The concept of Template is applicable to:
       Functions
       Classes


                                                        30
Function Templates
   Suppose we want to write a function that returns the absolute
    value of a number.

   Ordinarily, this function would be written for a particular data
    type:
     int abs(int n) // absolute value of ints
     {
     return (n<0) ? -n : n; // if n is negative, return –n
     }

   Here the function is defined to take an argument of type int and
    to return a value of this same type.

                                                                       31
Function Templates
   But now suppose we want to find the absolute value of a type
    long.
   We need to write a completely new function:
     long abs(long n) // absolute value of longs
     {
     return (n<0) ? -n : n;
     }

   And again, for type float:
     float abs(float n) // absolute value of floats
     {
     return (n<0) ? -n : n;
     }
                                                                   32
Function Templates
   The body of the function is same in each case, but they must be
    separate functions because they handle variables of different
    types.


   It’s true that in C++ these functions can all be overloaded to have
    the same name, but we must nevertheless write a separate
    definition for each one.


   Rewriting the same function body over and over for different
    types wastes time as well as space in the listing.


                                                                      33
Function Templates
   Also, if we find we’ve made an error in one such function, we’ll
    need to remember to correct it in each function body.

   Failing to do this correctly is a good way to introduce
    inconsistencies into our programs.

   It would be nice if there were a way to write such a function just
    once and have it work for many different data types.

   This is exactly what Function templates do for us.



                                                                         34
Function Templates
#include <iostream.h>
template <class T> // function template             OUTPUT
T abs(T n)
                                                    abs(5)=5
{ return (n < 0) ? -n : n; }
                                                    abs(-6)=6
void main() {
    int int1 = 5;                                   abs(70000)=70000
    int int2 = -6;                                  abs(-80000)=80000
    long lon1 = 70000;                              abs(9.95)=9.95
    long lon2 = -80000;
                                                    abs(-10.15)=10.15
    double dub1 = 9.95;
    double dub2 = -10.15;
    cout << "abs(" << int1 << ")=" << abs(int1) << endl; // abs(int)
    cout << "abs(" << int2 << ")=" << abs(int2) << endl; // abs(int)
    cout << "abs(" << lon1 << ")=" << abs(lon1) << endl; // abs(long)
    cout << "abs(" << lon2 << ")=" << abs(lon2) << endl; // abs(long)
    cout << "abs(" << dub1 << ")=" << abs(dub1) << endl; // abs(double)
    cout << "abs(" << dub2 << ")=" << abs(dub2) << endl; // abs(double)
}                                                                         35
Function Templates
   The key innovation in function templates is to represent the data
    type used by the function not as a specific type such as int, but by a
    name that can stand for any type.
   In the function template above, this name is T.
   The template keyword signals the compiler that we are about to
    define a Function template.
   The keyword class, within the angle brackets, might just as well be
    called type.
   As we’ve seen, we can define our own data types using classes, so
    there’s really no distinction between types and classes.
   The variable following the keyword class (T in this example) is called
    the template argument.

                                                                          36
Function Templates
   What does the compiler do when it sees the template keyword and
    the Function definition that follows it?
   The function template itself doesn’t cause the compiler to generate
    any code.
   It can’t generate code because it doesn’t know yet what data type
    the function will be working with.
   It simply remembers the template for possible future use.
   Code generation doesn’t take place until the function is actually
    called (invoked) by a statement within the program.
   This happens in expressions such as abs(int1) in the statement
        cout << "abs(" << int << ")=" << abs(int1);

                                                                          37
Function Templates

   When the compiler sees a function call, it knows that
    the type to use is int, because that’s the type of the
    argument int1.

   So it generates a specific version of the abs()
    function for type int, substituting int wherever it sees
    the name T in the function template.

   This is called instantiating the function template, and
    each instantiated version of the function is called a
    template function.                                    38
Function Templates

   Notice that the amount of RAM used by the program
    is the same whether we use the template approach
    or write three separate functions.
   What we’ve saved is having to type three separate
    functions into the source file. This makes the listing
    shorter and easier to understand.
   Also, if we want to change the way the function
    works, we need to make the change in only one
    place in the listing instead of three.

                                                             39
Function Templates with Multiple
Arguments
   Let’s look at another example of a function template.
   This one takes three arguments: two template
    arguments and one basic type.
   The purpose of this function is to search an array for
    a specific value.
   The function returns the array index for that value if
    it finds it, or -1 if it can’t find it.
   The arguments are a pointer to the array, the value
    to search for, and the size of the array.
                                                             40
Function Templates with Multiple
Arguments
template <class atype>
int find(const atype* array, atype value, int size) {         Output
    for(int j=0; j<size; j++)                                 'f' in chrArray: index=2
           if(array[ j ]==value) return j;                    6 in intArray: index=-1
    return -1;                                                4 in dubArray: index=-1
}
int main() {
    char chrArr[ ] = {'a', 'c', 'f', 's', 'u', 'z'}; // array
    char ch = 'f'; // value to find
    int intArr[ ] = {1, 3, 5, 9, 11, 13};
    int in = 6;
    double dubArr[ ] = {1.0, 3.0, 5.0, 9.0, 11.0, 13.0};
    double db = 4.0;
    cout << "n 'f' in chrArray: index=" << find(chrArr, ch, 6);
    cout << "n 6 in intArray: index=" << find(intArr, in, 6);
    cout << "n 4 in dubArray: index=" << find(dubArr, db, 6);
    return 0;
}                                                                                     41
Template Arguments Must Match
   When a template function is invoked, all instances of the same
    template argument must be of the same type.
   For example, in find(), if the array is of type int, the value to
    search for must also be of type int. We can’t say
        int intarray[ ] = {1, 3, 5, 7}; // int array
        float f1 = 5.0; // float value
        int value = find(intarray, f1, 4); // error
   Because the compiler expects all instances of atype to be the
    same type.
        find(int*, int, int); // It can generate a function
        find(int*, float, int); // It can’t generate a function
   Because the first and second arguments must be the same type.
                                                                        42
Class Templates

   The template concept can be applied to classes as
    well as to functions.

   Class templates are generally used for data storage
    (container) classes.

   Stacks and linked lists, are examples of data
    storage classes.



                                                        43
Class Templates
   The Stack class below, could store data only of type int.


    class Stack
    {
       int st[10]; // array of ints
       int top; // index number of top of stack
    public:
       Stack(); // constructor
       void push(int var); // takes int as argument
       int pop(); // returns int value
    };

                                                                44
Class Templates
   If we wanted to store data of type long in a stack, we would need
    to define a completely new class.

    class LongStack
    {
       long st[10]; // array of longs
       int top; // index number of top of stack
    public:
       LongStack(); // constructor
       void push(long var); // takes long as argument
       long pop(); // returns long value
    };
                                                                    45
Class Templates

//Solution with a class template
template <class Type>
class Stack{
     Type st[10]; // stack: array of any type
     int top; // number of top of stack
public:
     Stack(){top = 0;} // constructor
     void push(Type); // put number on stack
     Type pop(); // take number off stack
};


                                                46
Class Templates
template<class Type>
void Stack<Type>::push(Type var) // put number on stack
{ if(top > 10-1) // if stack full,
        cout<< "Stack is full!";
   st[top++] = var;
}

template<class Type>
Type Stack<Type>::pop() // take number off stack
{ if(top <= 0) // if stack empty,
  cout<< "Stack is empty!";
  return st[--top];
}

                                                          47
Class Templates
int main() {
Stack<float> s1; // s1 is object of class Stack<float>
// push 2 floats, pop 2 floats
s1.push(1111.1);
s1.push(2222.2);
cout << "1: " << s1.pop() << endl;
cout << "2: " << s1.pop() << endl;

Stack<long> s2; // s2 is object of class Stack<long>
// push 2 longs, pop 2 longs
s2.push(123123123L);
s2.push(234234234L);
cout << "1: " << s2.pop() << endl;
cout << "2: " << s2.pop() << endl;
return 0;
}                                                        48

Más contenido relacionado

La actualidad más candente

New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - PraticalsFahad Shaikh
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersBartosz Kosarzycki
 
Understanding JavaScript
Understanding JavaScriptUnderstanding JavaScript
Understanding JavaScriptnodejsbcn
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and UtilitiesPramod Kumar
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Abu Saleh
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesDragos Ionita
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptJulie Iskander
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP Zaenal Arifin
 

La actualidad más candente (20)

Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 
Operator overload rr
Operator overload  rrOperator overload  rr
Operator overload rr
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - Praticals
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Understanding JavaScript
Understanding JavaScriptUnderstanding JavaScript
Understanding JavaScript
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
Core concepts-javascript
Core concepts-javascriptCore concepts-javascript
Core concepts-javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUALCS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 

Destacado

Procedural to oop in php
Procedural to oop in phpProcedural to oop in php
Procedural to oop in phpBarrett Avery
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnTypeMuhammad Hammad Waseem
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Clinical Anatomy Circle Of Willis & Cavernous Sinus
Clinical Anatomy Circle Of Willis & Cavernous SinusClinical Anatomy Circle Of Willis & Cavernous Sinus
Clinical Anatomy Circle Of Willis & Cavernous SinusAnkit Punjabi
 
Product and service design
Product and service designProduct and service design
Product and service designGrace Falcis
 

Destacado (8)

Procedural to oop in php
Procedural to oop in phpProcedural to oop in php
Procedural to oop in php
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
 
Forest Resources
Forest ResourcesForest Resources
Forest Resources
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Forest Resources
Forest  ResourcesForest  Resources
Forest Resources
 
Forest Resources
Forest ResourcesForest Resources
Forest Resources
 
Clinical Anatomy Circle Of Willis & Cavernous Sinus
Clinical Anatomy Circle Of Willis & Cavernous SinusClinical Anatomy Circle Of Willis & Cavernous Sinus
Clinical Anatomy Circle Of Willis & Cavernous Sinus
 
Product and service design
Product and service designProduct and service design
Product and service design
 

Similar a CS212: Data Structures and Algorithms Lecture #1 - OOP Concepts and Templates

chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxSirRafiLectures
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptishan743441
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++RAJ KUMAR
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfLadallaRajKumar
 

Similar a CS212: Data Structures and Algorithms Lecture #1 - OOP Concepts and Templates (20)

Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Day 1
Day 1Day 1
Day 1
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
srgoc
srgocsrgoc
srgoc
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
Op ps
Op psOp ps
Op ps
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
02.adt
02.adt02.adt
02.adt
 

Más de Teksify

HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM) Teksify
 
Data Structure Lecture 7
Data Structure Lecture 7Data Structure Lecture 7
Data Structure Lecture 7Teksify
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6Teksify
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5Teksify
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4Teksify
 
Data Structure Lecture 3
Data Structure Lecture 3Data Structure Lecture 3
Data Structure Lecture 3Teksify
 
Data Structure Lecture 2
Data Structure Lecture 2Data Structure Lecture 2
Data Structure Lecture 2Teksify
 
Variable power supply
Variable power supplyVariable power supply
Variable power supplyTeksify
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro eTeksify
 
Make lens of mobile by pro e
Make lens of mobile by pro eMake lens of mobile by pro e
Make lens of mobile by pro eTeksify
 

Más de Teksify (12)

HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM)
 
Data Structure Lecture 7
Data Structure Lecture 7Data Structure Lecture 7
Data Structure Lecture 7
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
 
Data Structure Lecture 3
Data Structure Lecture 3Data Structure Lecture 3
Data Structure Lecture 3
 
Data Structure Lecture 2
Data Structure Lecture 2Data Structure Lecture 2
Data Structure Lecture 2
 
Ch3
Ch3Ch3
Ch3
 
Ch1 2
Ch1 2Ch1 2
Ch1 2
 
Variable power supply
Variable power supplyVariable power supply
Variable power supply
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro e
 
Make lens of mobile by pro e
Make lens of mobile by pro eMake lens of mobile by pro e
Make lens of mobile by pro e
 

Último

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 

Último (20)

INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 

CS212: Data Structures and Algorithms Lecture #1 - OOP Concepts and Templates

  • 1. CS212: Data Structures and Algorithms Lecture # 1 Revision • OOP Concepts • Templates
  • 2. Outline  Class and Object  Constructor  Constructor Initializer List  Destructor  Default Initialization Values  Function Templates  Class Templates 2
  • 3. Class  A class is a programmer-defined data type. It consists of data and functions which operate on that data.  Placing data and functions together into a single entity is the central idea of object- oriented programming. 3
  • 4. Class Declaration Syntax class Name // usually capitalized { public: public members; // usually functions private: private members; // usually variables }; Note 4
  • 5. Example of a Class Declaration #include<iostream> using namespace std; class ASimpleClass //declare a class { private: int data; //a private member public: void setData(int item) { data = item; } void displayData() { cout<<"Data is : "<<data<<endl; } }; //program continues … 5
  • 6. Example of a Class Declaration void main() { ASimpleClass asp; //creates an object asp.setData(10); //member access using dot operator asp.displayData(); asp.setData(20); asp.displayData(); } Output Data is : 10 Data is : 20 Press any key to continue 6
  • 7. A Few Terms  class  class member  data member  member function  class object (class instance) 7
  • 8. Objects  An object is an instance of a class.  Similarity: int row;  This creates an instance of int called “row”. 8
  • 9. Calling Member Functions  asp.setData(10);  asp.displayData();  These two statements are not like normal function calls.  This strange syntax is used to call a member function that is associated with a specific object.  A member function must always be called in connection with an object of the class.  Because a member function is always called to act on a specific object, and not on the class in general.  The dot operator connects the object name and the member function.  Dot operator is also called class member access operator. 9
  • 10. Working with Objects ASimpleClass asp1,asp2; asp1.setData(10); asp2.setData(20); ASimpleClass int data void setData(); void displayData(); Two objects of ASimpleClass data data 10 20 asp1 asp2 10
  • 11. Part Example #include<iostream> partno=pn; using namespace std; cost=c; class Part } { void showPart() { int modelno; cout<<"Model : int partno; "<<modelno<<endl; float cost; cout<<"Part : public: "<<partno<<endl; void setPart(int mn, int pn, cout<<"Cost : "<<cost<<endl; float c) } { }; //program continues … modelno=mn; //continues on right side… 11
  • 12. Part Example Cont… void main() { Part part; part.setPart(555,100,100); part.showPart(); } Output Model : 555 Part : 100 Cost : 100 Press any key to continue 12
  • 13. Constructor  Setters provide a way of initializing class data members.  However, sometimes it is convenient that an object can initialize itself when it is created, without the need to call setters. 13
  • 14. Constructor  Automatic initialization is carried out using a special member function called a constructor.  A constructor is a special member function provided to allow initialization of variables.  Constructor is executed automatically whenever an object is created. 14
  • 15. Counter Example (with constructor) //counter.h class Counter { private: unsigned int count; public: Counter() {count=0; } //constructor void incCount(); unsigned int getCount(); }; 15
  • 16. Counter Example (with constructor) //counter.cpp #include"counter.h" unsigned int Counter::getCount() { return count; } void Counter::incCount() { count++; } 16
  • 17. Counter Example (with constructor) //driver.cpp #include"counter.h" #include<iostream> using namespace std; void main() { Counter c; //creates an object and initializes count cout<<c.getCount()<<endl; c.incCount(); cout<<c.getCount()<<endl; } 17
  • 18. Constructor  Must have the same name as the class itself  Name is one way the compiler recognizes them as constructors.  Do NOT have a return type (not even void)  Why not?  Since the constructor is called automatically by the system.  This is the 2nd way the compiler knows they are constructors. 18
  • 19. Default Constructors  Default constructor parameter-less constructor, it initializes the variables to some default value, such as zero  Example TimeType(); Student(); 19
  • 20. Default Constructors Example 1 Example 2 Student::Student( ) TimeType::TimeType( ) { { GPA = 0; hrs = 0; num_of_grades = 130; mins = 0; } secs = 0; } 20
  • 21. Default Constructors  An implicit default (no-argument) constructor is built into the program automatically by the compiler.  This constructor is used to create the objects when we don’t write a constructor. 21
  • 22. Initializer List Counter():count(0) { } //constructor Is equivalent to Counter() { count=0; }  Initialization takes place following the member function declaration but before the function body.  It is preceded by a colon  The value is placed in parentheses following the data member. 22
  • 23. Initializer List  Initializer list is also called member-initialization list.  If multiple members are to be initialized, they are separated by commas.  Example someClass():m1(5),m2(42),m3(3) { }  Actions complicated than simple initialization must be carried out in the constructor body. 23
  • 24. Overloaded Constructors  A default constructor initializes data members at default values such as zero.  Example  Distance() : feet(0),inches(0.0){ } //default constructor  It is convenient to be able to give data members a value (other than default value) when the objects are created.  Example  Distance dist1(5,6.25);  Defines a Distance object named dist1 and initializes feet at 5, and inches at 6.25  For this, we need a constructor like this:  Distance (int f, float i) : feet(f),inches(i) { } 24
  • 25. Overloaded Constructors  We have two constructors for Distance  Distance() : feet(0),inches(0.0) { }//default constructor  Distance(int f, float i) : feet(f),inches(i) { }  These two explicit constructors with the same name Distance makes the constructor overloaded.  Which of these is executed to create an object depends on how many arguments are used in the object creation.  Distance dist1;//calls default constructor  Distance dist2(11,6.25); //calls constructor with two arguments 25
  • 26. The Default Copy Constructor  We have seen two variations of constructor.  There is another way of initializing a constructor.  An object can be initialized with another object of the same type.  This type of constructor falls in the category of implicit constructors.  We don’t need to create a constructor for this.  One is already built into all classes.  It is called the default copy constructor.  It is one argument constructor whose argument is an object of the same class as the constructor. 26
  • 27. The Default Copy Constructor  Default copy constructor can be invoked in two ways:  Example:  Distance dist1(11, 6.25);  Distance dist2(dist1); //calling default copy constructor  Distance dist3=dist1; //calling default copy constructor  Both the ways have the same results and causes a member-to- member copy  The second way looks like an assignment statement, but it is not.  Both the formats invoke the default copy constructor. 27
  • 28. Destructors  Another function is called automatically when an object is destroyed.  Such a function is called a destructor.  Destructor has the name as the constructor (i.e. the class name) but is preceded by a tilde (~).  The most common use of destructor is to de-allocate memory that was allocated for the object by the constructor. 28
  • 29. Destructors  ~Classname( )  A default do-nothing destructor is provided by the compiler.  Only one destructor per class  No arguments  No return type 29
  • 30. Templates  C++ supports code reuse in different ways.  The template feature in C++ provides way to reuse source code.  The concept of Template is applicable to:  Functions  Classes 30
  • 31. Function Templates  Suppose we want to write a function that returns the absolute value of a number.  Ordinarily, this function would be written for a particular data type: int abs(int n) // absolute value of ints { return (n<0) ? -n : n; // if n is negative, return –n }  Here the function is defined to take an argument of type int and to return a value of this same type. 31
  • 32. Function Templates  But now suppose we want to find the absolute value of a type long.  We need to write a completely new function: long abs(long n) // absolute value of longs { return (n<0) ? -n : n; }  And again, for type float: float abs(float n) // absolute value of floats { return (n<0) ? -n : n; } 32
  • 33. Function Templates  The body of the function is same in each case, but they must be separate functions because they handle variables of different types.  It’s true that in C++ these functions can all be overloaded to have the same name, but we must nevertheless write a separate definition for each one.  Rewriting the same function body over and over for different types wastes time as well as space in the listing. 33
  • 34. Function Templates  Also, if we find we’ve made an error in one such function, we’ll need to remember to correct it in each function body.  Failing to do this correctly is a good way to introduce inconsistencies into our programs.  It would be nice if there were a way to write such a function just once and have it work for many different data types.  This is exactly what Function templates do for us. 34
  • 35. Function Templates #include <iostream.h> template <class T> // function template OUTPUT T abs(T n) abs(5)=5 { return (n < 0) ? -n : n; } abs(-6)=6 void main() { int int1 = 5; abs(70000)=70000 int int2 = -6; abs(-80000)=80000 long lon1 = 70000; abs(9.95)=9.95 long lon2 = -80000; abs(-10.15)=10.15 double dub1 = 9.95; double dub2 = -10.15; cout << "abs(" << int1 << ")=" << abs(int1) << endl; // abs(int) cout << "abs(" << int2 << ")=" << abs(int2) << endl; // abs(int) cout << "abs(" << lon1 << ")=" << abs(lon1) << endl; // abs(long) cout << "abs(" << lon2 << ")=" << abs(lon2) << endl; // abs(long) cout << "abs(" << dub1 << ")=" << abs(dub1) << endl; // abs(double) cout << "abs(" << dub2 << ")=" << abs(dub2) << endl; // abs(double) } 35
  • 36. Function Templates  The key innovation in function templates is to represent the data type used by the function not as a specific type such as int, but by a name that can stand for any type.  In the function template above, this name is T.  The template keyword signals the compiler that we are about to define a Function template.  The keyword class, within the angle brackets, might just as well be called type.  As we’ve seen, we can define our own data types using classes, so there’s really no distinction between types and classes.  The variable following the keyword class (T in this example) is called the template argument. 36
  • 37. Function Templates  What does the compiler do when it sees the template keyword and the Function definition that follows it?  The function template itself doesn’t cause the compiler to generate any code.  It can’t generate code because it doesn’t know yet what data type the function will be working with.  It simply remembers the template for possible future use.  Code generation doesn’t take place until the function is actually called (invoked) by a statement within the program.  This happens in expressions such as abs(int1) in the statement  cout << "abs(" << int << ")=" << abs(int1); 37
  • 38. Function Templates  When the compiler sees a function call, it knows that the type to use is int, because that’s the type of the argument int1.  So it generates a specific version of the abs() function for type int, substituting int wherever it sees the name T in the function template.  This is called instantiating the function template, and each instantiated version of the function is called a template function. 38
  • 39. Function Templates  Notice that the amount of RAM used by the program is the same whether we use the template approach or write three separate functions.  What we’ve saved is having to type three separate functions into the source file. This makes the listing shorter and easier to understand.  Also, if we want to change the way the function works, we need to make the change in only one place in the listing instead of three. 39
  • 40. Function Templates with Multiple Arguments  Let’s look at another example of a function template.  This one takes three arguments: two template arguments and one basic type.  The purpose of this function is to search an array for a specific value.  The function returns the array index for that value if it finds it, or -1 if it can’t find it.  The arguments are a pointer to the array, the value to search for, and the size of the array. 40
  • 41. Function Templates with Multiple Arguments template <class atype> int find(const atype* array, atype value, int size) { Output for(int j=0; j<size; j++) 'f' in chrArray: index=2 if(array[ j ]==value) return j; 6 in intArray: index=-1 return -1; 4 in dubArray: index=-1 } int main() { char chrArr[ ] = {'a', 'c', 'f', 's', 'u', 'z'}; // array char ch = 'f'; // value to find int intArr[ ] = {1, 3, 5, 9, 11, 13}; int in = 6; double dubArr[ ] = {1.0, 3.0, 5.0, 9.0, 11.0, 13.0}; double db = 4.0; cout << "n 'f' in chrArray: index=" << find(chrArr, ch, 6); cout << "n 6 in intArray: index=" << find(intArr, in, 6); cout << "n 4 in dubArray: index=" << find(dubArr, db, 6); return 0; } 41
  • 42. Template Arguments Must Match  When a template function is invoked, all instances of the same template argument must be of the same type.  For example, in find(), if the array is of type int, the value to search for must also be of type int. We can’t say  int intarray[ ] = {1, 3, 5, 7}; // int array  float f1 = 5.0; // float value  int value = find(intarray, f1, 4); // error  Because the compiler expects all instances of atype to be the same type.  find(int*, int, int); // It can generate a function  find(int*, float, int); // It can’t generate a function  Because the first and second arguments must be the same type. 42
  • 43. Class Templates  The template concept can be applied to classes as well as to functions.  Class templates are generally used for data storage (container) classes.  Stacks and linked lists, are examples of data storage classes. 43
  • 44. Class Templates  The Stack class below, could store data only of type int. class Stack { int st[10]; // array of ints int top; // index number of top of stack public: Stack(); // constructor void push(int var); // takes int as argument int pop(); // returns int value }; 44
  • 45. Class Templates  If we wanted to store data of type long in a stack, we would need to define a completely new class. class LongStack { long st[10]; // array of longs int top; // index number of top of stack public: LongStack(); // constructor void push(long var); // takes long as argument long pop(); // returns long value }; 45
  • 46. Class Templates //Solution with a class template template <class Type> class Stack{ Type st[10]; // stack: array of any type int top; // number of top of stack public: Stack(){top = 0;} // constructor void push(Type); // put number on stack Type pop(); // take number off stack }; 46
  • 47. Class Templates template<class Type> void Stack<Type>::push(Type var) // put number on stack { if(top > 10-1) // if stack full, cout<< "Stack is full!"; st[top++] = var; } template<class Type> Type Stack<Type>::pop() // take number off stack { if(top <= 0) // if stack empty, cout<< "Stack is empty!"; return st[--top]; } 47
  • 48. Class Templates int main() { Stack<float> s1; // s1 is object of class Stack<float> // push 2 floats, pop 2 floats s1.push(1111.1); s1.push(2222.2); cout << "1: " << s1.pop() << endl; cout << "2: " << s1.pop() << endl; Stack<long> s2; // s2 is object of class Stack<long> // push 2 longs, pop 2 longs s2.push(123123123L); s2.push(234234234L); cout << "1: " << s2.pop() << endl; cout << "2: " << s2.pop() << endl; return 0; } 48