functions, pointers, oops, classes

Upload: hassan-usaid

Post on 05-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    1/55

    Functions, Pointers, Object Oriented

    Programming, Classes and Objects,

    Constructors and DestructorsPraveen R S

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    2/55

    Functions

    Formal and Actual Parameters, Call by value Call by

    reference, Function Overloading, Function with defaultarguments, Recursive Functions, Inline Functions

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    3/55

    Functions

    Classification

    1. Non Returning Functions. eg: clrscr(), getch()

    2. Returning Functions. eg: sqrt(), pow()

    Function Definition

    Function Calling Function Declaration

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    4/55

    Function Definition

    Function Definition Syntax

    (arguments)

    { body; }

    Example

    int sum (int a, int b)

    { int c;

    c=a+b;

    return c; }

    Function Declaration

    int sum (int, int);

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    5/55

    //Program to add two numbers using a function

    #include

    #include

    int sum (int, int); //Function declarationvoid main()

    { clrscr();

    int x, y, s;

    coutx>>y;s= sum(x, y); // Function Calling

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    6/55

    Call by Value and Call by reference

    Swap Function

    void swap(int a, int b)

    { int temp;

    temp = a;

    a = b;

    b= temp; }

    void main()

    { int x = 5, y = 10;

    swap (x, y);

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    7/55

    FUNCTION OVERLOADING Functions with same name and different definitions, which behave

    different for different set of arguments is called an Overloaded function

    A function can be overloaded to following criteria:

    1. Number of arguments

    2. Type of arguments

    3. Order of appearance

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    8/55

    FUNCTION OVERLOADING-EXAMPLE

    // Function declaration

    void fun(int); //Prototype 1

    void fun(int, int); //Prototype 2

    void fun(int, float); //Prototype 3

    void fun(float, int); //Prototype 4

    void fun(float, float); //Prototype 5

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    9/55

    #include

    void fun(int x)

    { cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    10/55

    FUNCTION WITH DEFAULT ARGUMENTS

    C++ allows to call a function without specifying all its arguments

    Default values can be specified at the time of function declaration or

    function definition

    Only the trailing arguments(arguments at the right side) can have

    default values

    Examplefloat result(int marks1, int marks2, int marks3=70)

    {-----------}

    Function call

    result(60, 55);

    result(60, 55, 69);

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    11/55

    #includevoid repchar(char ch= =, int x=30)

    {for(i=1;i

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    12/55

    #includevoid repchar(char = =, int = 30);

    void main(){

    clrscr();repchar();repchar(*);repchar(#, 20);

    getch();}

    void repchar(char ch, int x){

    for(i=1;i

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    13/55

    //Function with Default Arguments Example

    //Program to find the area of a circle, rectangle and triangle using a single function

    #include

    #include

    #include

    float area(int x, int y=0, int z=0)

    {

    if (y==0 && z==0)

    {

    return (3.14*x*x);

    }

    else if(z==0)

    {

    return (x*y);

    }

    else

    { float s;

    s=(x+y+z)/2.0;

    return (sqrt(s*(s-x)*(s-y)*(s-z)));

    }

    }

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    14/55

    void main()

    {

    int a,b,c, choice;

    clrscr();

    coutchoice;clrscr();

    switch(choice)

    {

    case 1: couta;

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    15/55

    // Function Overloading Example

    float area(int x){ return (3.14*x*x); }

    float area(int x, int y)

    { return (x*y); }

    float area(int x, int y, int z)

    { float s;

    s = (x+y+z)/2.0;

    return (sqrt(s*(s-x)*(s-y)*(s-z))); }

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    16/55

    Recursive Function

    A function called within itself

    //Recursive function for factorial

    long int fact (int n)

    { if (n==0)

    return 1;else

    return (n * fact (n-1));

    }

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    17/55

    //Program with Recursive function for Fibonacci series

    #include

    #include

    int fib (int a){ if (a==0)

    return 0;

    else if (a==1)

    return 1;

    elsereturn (fib(a-1)+ fib(a-2)); }

    void main()

    { clrscr();

    int n;

    coutn;

    for (int i =0; i

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    18/55

    INLINE FUNCTIONS

    Normal Functions save memory space because all the calls to the

    function cause the same code to be executed

    The functions body need not be duplicated in the main function

    When the compiler sees a function call, it normally jumps to the

    function and at the end of the function, the compiler return back to the

    statement in the main program While the sequence of events may save memory space, it may take extra

    time for this jumping

    In Inline functions, each time there is a function call, the entire code in

    the function is inserted, instead of jumping

    Inline function is used only for shorter code

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    19/55

    //Program to demonstrate inline function

    #include

    #include

    inline cube(int x){

    return(r*r*r);

    }

    void main()

    {

    int a,b;

    couta;

    b=cube(a);

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    20/55

    //Program to find the area of a triangle using inline function

    #include

    inline float triangle(int x, int y, int z, float s)

    {

    return(sqrt(s*(s-x)*(s-y)*(s-z)));

    }

    void main()

    {

    int a,b,c;

    float sem, area;couta>>b>>c;

    sem=(a+b+c)/2.0;

    area=triangle(a,b,c,sem);

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    21/55

    Pointers

    Static Memory Allocation, Dynamic Memory Allocation,

    Pointer Arithmetic

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    22/55

    Static & Dynamic Memory Allocation

    Static Memory Allocation The amount of memory to be allocated is known before the

    execution of the program

    Memory is allocated during compilation

    Dynamic Memory Allocation Memory is allocated during execution, as and when required

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    23/55

    Pointers

    A pointer is a variable that contains an address Eg:

    int *ptr;

    ptr is a pointer to an integer variable

    &- address of operator

    *- value at operator

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    24/55

    Example

    int num = 25, dup;

    int *ptr;

    ptr = # //ptr = address of num

    dup = *ptr; //dup = value at ptr, i.e. 25

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    25/55

    Pointer Arithmetic

    ptr + n = ptr + n*size of the base type of ptr

    Eg:

    int *ptr=&a; // assume ptr = 1004

    ptr + 3 = 1004 + (3*2) = 1010

    float *ptr=&a;

    ptr + 3 = 1004 + (3*4) = 1016

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    26/55

    Dynamic Memory Allocation

    Dynamic Memory Allocation Operators: new operator for allocation

    delete operator for deallocation

    Synax:

    PointerVariable = new DataType; Example:

    int *ptr;

    ptr = new int;

    Arrays using Pointer: ptr = new int[size];

    Delete Operator: delete ptr; OR delete [] ptr;

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    27/55

    //Program to read N numbers and display them in reverse order without using arrays

    #include

    #include

    void main()

    { clrscr();int *ptr;

    int i, n;

    coutn;

    ptr = new int[n];

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    28/55

    //Program to read N numbers and display the largest among them#include#includevoid main()

    { clrscr();

    int *ptr;int i, n, large;coutn;ptr = new int[n];cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    29/55

    Object Oriented Programming

    Data Abstraction, Data Encapsulation, Inheritance,

    Polymorphism, Modularity, Delegation

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    30/55

    DATA ABSTRACTION It refers to the act of representing essential features without including the

    background details

    It is the concept of simplifying a real world concept into its essential

    elements

    Starting of a motor bike

    We need not bother about the function inside the engine. We just need to

    kick the kicker lever.

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    31/55

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    32/55

    INHERITANCE Inheritance is the capability of one class of things to inherit properties from

    another class

    Base class and Derived class

    It facilitates reusability of code

    It supports extensibility

    Inheritance is transitive in nature Examples

    Base class- Automobiles

    Derived classes- Motorbike, Bus, Car, etc.

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    33/55

    POLYMORPHISM Polymorphism is the ability to process the data in more than one form

    It is achieved in C++ by the following:

    (i) Function Overloading

    (ii) Operator Overloading

    (iii) Dynamic binding

    Examples

    Key of a motorbike

    Bell in a school

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    34/55

    MODULARITY Modularity is the property of a system that has been decomposed into a set

    of unified loosely coupled modules

    What we need is to assemble these modules in the proper order

    The programmer is required to write functions for each of these modules

    and finally arrange these functions in the proper order

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    35/55

    DELEGATION Delegation is an alternative to class inheritance

    Class within class- Containership

    An object can be a collection of many objects

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    36/55

    Classes and Objects

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    37/55

    CLASS

    Class is a way that bind the data and functions together

    Class declaration

    class class_name

    {

    private:variable declarations;

    function declarations;

    public:

    variable declarations;

    function declarations;

    protected:

    variable declarations;

    function declarations;

    };

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    38/55

    ELEMENTS OF A CLASS

    Class Name Access Labels

    Data Members

    Member Functions

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    39/55

    //Program to collect the details of student through class

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    40/55

    #include

    #include

    #include

    class student

    { private:

    int roll;

    char name[20];

    int attendance;

    public:

    void read_data();void disp_data();

    };

    void student::read_data()

    {

    coutroll;cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    41/55

    void student::disp_data()

    {

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    42/55

    void main(){

    clrscr();

    student s1,s2;

    s1.read_data();

    s2.read_data();

    s1.disp_data();s2.disp_data();

    getch();

    }

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    43/55

    void main()

    {

    clrscr();

    int n;

    student S[30];coutn;

    for(int i=0; i

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    44/55

    g p y pp

    #include

    #include

    #include

    class student

    {

    private:int roll;

    char name[20];

    int scores[6];

    public:

    void read_data();

    void disp_data();

    int total();

    };

    void student::read_data()

    {

    coutroll;

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    45/55

    void student::disp_data()

    {

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    46/55

    void main()

    {

    clrscr();

    int n, max;

    student S[40];

    coutn;

    for(int i=0, i

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    47/55

    g

    #include

    #include

    #include

    class distance

    {private:

    int mts;

    int cms;

    public:

    void read_data();

    void disp_data();void add(distance, distance);

    };

    void distance::read_data()

    {

    coutmts>>cms;}

    void distance::disp_data()

    {

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    48/55

    {

    mts=d1.mts+d2.mts;

    cms=d1.cms+d2.cms;

    if (cms>100)

    {mts=mts+(cms/100);

    cms=cms%100;

    }

    }

    void main()

    {distance ob1, ob2, ob3;

    ob1.read_data();

    ob2.read_data();

    ob3.add(ob1,ob2);

    cout

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    49/55

    CONSTRUCTORS ANDDESTRUCTORS

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    50/55

    Constructors

    It is a special member function which is used to initialize the objects of

    its class

    The name of this function will be same as that of its class

    It can be defined inside as well as outside the class definition

    Types of Constructors

    Default Constructors Parameterized Constructors

    Copy Constructors

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    51/55

    Characteristics of Constructors

    The should be declared as public

    It has the same name as that of its class

    It is executed automatically when an object is created

    It does not have return type

    Constructors can have default arguments

    They cannot be inherited through a derived class, but the base classconstructor can be accessed through the derived class

    class student

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    52/55

    {

    private:

    int roll;

    float attendance;

    public:void read_data();

    void disp_data();

    student() //Default Constructor

    {

    roll=0; attendance=0.0;

    }

    student(int x, float y) //Parameterized Constructor

    {

    roll=x; attendance=y;

    }

    student(student &a) //Copy Constructor{

    roll = a.roll;

    attendance = a.attendance;

    }

    };

    student ob1, ob2, ob3;

    student ob4 (5, 95.5), ob5 (15, 87.5);

    student ob6 (ob4), ob7(ob1);

    //Constructors with Default Arguments

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    53/55

    class ABC

    {

    private:

    int a;float b;

    public:

    ABC(int x, float y=0.0)

    {

    a=x; b=y;

    }};

    void main()

    {

    ABC ob1(3);

    ABC ob2(3, 4.6);

    }

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    54/55

    Destructors

    Destructors are used to destroy the objects that have been created by a

    constructor

    Like a constructor, the destructor is a member function whose name is

    the same as the class name but preceded by tilde(~) symbol

    ~ABC ();

    Characteristics of Destructors It has the same name as that of its class

    It is executed automatically after the last use of the object

    It does not have return type

    No argument can be provided to a destructor

    If there is no destructor in a class, a default destructor is generated by the compiler A destructor cannot be inherited

  • 7/31/2019 Functions, Pointers, OOPS, Classes

    55/55

    THANK YOU