advance concept class

Upload: nttfable

Post on 07-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Advance Concept Class

    1/68

    The features of c++

  • 8/6/2019 Advance Concept Class

    2/68

    Inline members functionsy The inline functions advise the compiler to insert the

    copy of the functions code inline within the code of

    the calling functiony The syntax

    y Inline (parameter_list)

    y {

    y

    //body of the inline functiony Return;

    y }

  • 8/6/2019 Advance Concept Class

    3/68

    y They save memory space as all the function calls to bespecified

    y The following activities make the inline functionslower & inefficient

    y Values must be copied into storage of function pointer

    y Machine register must be shaved

    y Transfer of control to the functiony Return value

    y Returning control back to the calling function

  • 8/6/2019 Advance Concept Class

    4/68

    Example:-

    #include

    inline float square (const float x) //inline function definition{

    return (x*x);}

    void main(){

    float num, sqr;cout

  • 8/6/2019 Advance Concept Class

    5/68

    Example:-

    #include

    inline float cube (const float x) //inline function definition{

    return (x*x*x);}

    void main(){

    float num, cub;cout

  • 8/6/2019 Advance Concept Class

    6/68

  • 8/6/2019 Advance Concept Class

    7/68

    Friendy The private members of a class cannot be accessed by

    the non-members functions

    y This can be archived by using friend keyword

  • 8/6/2019 Advance Concept Class

    8/68

    Example:-

    #include class ff

    {

    private: int x;

    int y;

    public: void accept (void){

    cout y;

    }

    friend int average (ff num);

    friend int largest (ff num);

    }; //End of class definition

  • 8/6/2019 Advance Concept Class

    9/68

    //friend functionsint average(ff num){

    return ((num.x + num.y) / 2.0);}

    int largest (ff num){if (num.x > num.y)

    return num.x;else

    return num.y;

    }

  • 8/6/2019 Advance Concept Class

    10/68

    void main(){

    ff cob; //object creation

    cob.accept();

    cout

  • 8/6/2019 Advance Concept Class

    11/68

    Notey The keyword friend is used to declare friend functions

    yA friend function is called just like normal functions

    yA friend function is not in scope of the class to which it

    have been declaredyA friend function can be declared in private or public

    section

    y Usually friend has a objects as its arguments

    yA friend function cannot access the class membersdirectly. An object name followed by dot operator

  • 8/6/2019 Advance Concept Class

    12/68

  • 8/6/2019 Advance Concept Class

    13/68

    The implicit this pointery Each object declared has its own copy of data members

    y There is only one copy of members functions & each

    object as a pointer which holds address of object itselfy The pointer is called this

    y The this is a built-in pointer

    y This pointer also used to access data members

    y Treated just like ordinary pointery This will be use full in returning values

  • 8/6/2019 Advance Concept Class

    14/68

    Example:-include

    class number{

    private: int x;

    public: void setValue (int d){

    this->x = d;}

    void displayValue(void){

    cout

  • 8/6/2019 Advance Concept Class

    15/68

    void main(){

    number n1, n2; //object creation

    n1.setValue(30);

    n2.setValue(80);

    n1.displayValue();n2.displayValue();

    }

  • 8/6/2019 Advance Concept Class

    16/68

  • 8/6/2019 Advance Concept Class

    17/68

    Static class membersy Static is the keyword

    y The value will be retained

    y The static keyword declared with data members arecalled static data members

    y The members functions are called static memberfunction

  • 8/6/2019 Advance Concept Class

    18/68

  • 8/6/2019 Advance Concept Class

    19/68

  • 8/6/2019 Advance Concept Class

    20/68

    Example:-#include class sdataItem{

    private: static int count;int numb;

    public: void readData (int ival){

    numb = ival;count = count + 1;

    }

    void displayCount(void){

    cout

  • 8/6/2019 Advance Concept Class

    21/68

    int sdataItem :: count=5; //static member definition

    void main(){

    sdataItem x,y,z; //object creation

    x.displayCount();y.displayCount();

    z.displayCount();

    x.readData(13);y.readData(27);z.readData(38);

    cout

  • 8/6/2019 Advance Concept Class

    22/68

    Static member functiony The static member function will act on static data

    members

    y

    Remember pointsy This will address only the static data members

    y This does not have this operator

    y Using dot operator we can access the public data

    members

  • 8/6/2019 Advance Concept Class

    23/68

    Example:-#include

    class checkObject{private: static int num;

    public: static int totalObjects(void){

    num = num + 1;return num;}

    void display (void){

    cout

  • 8/6/2019 Advance Concept Class

    24/68

    void main()

    {checkObject cob1;checkObject :: totalObjects();cob1.display();

    checkObject cob2;

    checkObject :: totalObjects();cob2.display();

    }

  • 8/6/2019 Advance Concept Class

    25/68

  • 8/6/2019 Advance Concept Class

    26/68

  • 8/6/2019 Advance Concept Class

    27/68

    Dynamic

    y

    The object are created & destroyed at run-time sincethe objects are allocated & de-allocation during theexecution of a programs they are called dynamicallocated objects

    y A dynamically allocated objects is allocated on the freespace

    y A dynamically allocated objects created using

    y new expression

    y A dynamically de-allocation object using

    y Delete expression

    yA dynamically allocated objects may be single or arrayof objects

  • 8/6/2019 Advance Concept Class

    28/68

    Dynamic allocationy The process of allocating & de-allocating memory to

    the objects during the execution of a program is

    referred to as dynamic memory allocationy Using new

    y Unnamed objects

    y Ex:- int *ptr=new int;

    y Un-Initialized memoryy Ex:- int *ptr1=new int(0);

    y Ex:- int *ptr2=new int(5);

  • 8/6/2019 Advance Concept Class

    29/68

    De-allocation memoryy The memory allocated to the objects from the free

    store is de-allocated

    y

    Using deletey delete ptr;

    yApplying a delete expression to the same memorytwice will cause error

    y We should place delete at last memory other wisecause leak of memory

  • 8/6/2019 Advance Concept Class

    30/68

    Example:-

    #include

    void main(){

    int *iptr = new int(100);float *fptr = new float(30.25);char *cptr = new char('p');

    cout

  • 8/6/2019 Advance Concept Class

    31/68

    Dynamic allocation & de-allocation ofarrays

    y Dynamic using new & delete

    y New

    y Ex:- int *ptr=new int[10];y Ex:- int (*ptr)[15]=new int[15][15];

    y Delete

    y Ex:-delete[] ptr

  • 8/6/2019 Advance Concept Class

    32/68

    Example:#include void main(){

    int *ptrA = new int[10]; //array of 10 integersint *pN = new int;int *psum = new int(0);int i, j;cout

  • 8/6/2019 Advance Concept Class

    33/68

    for( i = 0; i < *pN-1; i++){

    *psum=*psum+ptrA[i];}

    cout

  • 8/6/2019 Advance Concept Class

    34/68

  • 8/6/2019 Advance Concept Class

    35/68

    Referencesy It is new data kind of variable

    y It provides an alias for previous defined variable

    y Syntaxy Data_type & reference_name=variable_name;

    y Ex:-float f=100;

    y Float &sum=total;

  • 8/6/2019 Advance Concept Class

    36/68

    Example:

    #includeVoid main()

    {

    int x=5;

    int &y=x;cout

  • 8/6/2019 Advance Concept Class

    37/68

    E

    x:-1 int x=100; int &y=; y=x; errorEx:-2 int a=10; int &ref1=a,&ref2=a;

    Ex:-3 char *ptr=hello; char &*refptr=ptr;

    Ex:-4 int x,y,z; int &arr[3]={x,y,z}; error

  • 8/6/2019 Advance Concept Class

    38/68

    Functionsy Syntax

    y Return_type function_name(parameter_list)

    y

    {y //body of function;

    y }

    y Small sub-routine of a program

    y

    Pass by valuey Pass by pointer

    y Pass by references

  • 8/6/2019 Advance Concept Class

    39/68

    Pass by value#include void main(){

    int x,y;void swapxy (int a, int b); //Function prototypecout y;cout

  • 8/6/2019 Advance Concept Class

    40/68

    Pass by pointer#include

    void main(){

    int x,y;void swapxy (int *px, int *py); //Function prototypecout y;

    cout

  • 8/6/2019 Advance Concept Class

    41/68

    Passing by references

    #include void main(){

    int x,y;void swapxy (int &xref, int &yref); //Function prototypecout y;

    cout

  • 8/6/2019 Advance Concept Class

    42/68

    Differences between

    y

    Referencey A reference must be initialized

    once it is initialized to oneobject, it can never made torefer to another object

    y A function with referenceparameter does not require tocheck against reference object

    y A reference parameter isprefixed with & operator

    y It help in implementingoverloading functions

    y

    Pointersy A pointer can point to

    different object or no object

    y A function with pointercannot dereference the

    pointer unless it checks thatthe pointer is pointing theobject

    y A pointer parameter isprefixed with * operator

    y It is not used inimplementing overloadingfunctions

  • 8/6/2019 Advance Concept Class

    43/68

  • 8/6/2019 Advance Concept Class

    44/68

    Constructorsy The constructors is a special kind of member function

    which is used to initialize the data membersautomatically

    y So it got the name constructors

    Class box

    {private: int l,w,h;

    public: box() { l=0;w=0;b=0; }

    };

  • 8/6/2019 Advance Concept Class

    45/68

    Characteristics ofa constructors

    yA constructors have the same name as that of classyA constructors is declared in the public section of the

    class definition

    yA constructors is invoked automatically as soon as the

    class object is createdyA constructors does not have any return value

    y The programmer does not have control overconstructors

    y The constructors are not inherited

    yA constructors cannot be declared virtual

    y Constructors can be overloaded

  • 8/6/2019 Advance Concept Class

    46/68

    Constructorsy There are three types of constructors

    y Default constructors

    y E

    x: box(){ }y Parameterized constructors

    y Ex: box (int I, int j, int k)(l=I;w=j;b=k;}

    y Copy constructors

    y Ex: box(box &b){ l=b.l;w=b.w;b=b.b;}

  • 8/6/2019 Advance Concept Class

    47/68

    Default constructors#include

    class box{

    private: int length;int width;int height;

    public: box() //constructor

    { length = 0;width = 0;height = 0;

    }void print(void)

    { cout

  • 8/6/2019 Advance Concept Class

    48/68

    Parameterized constructors#include #include class employee{

    private: int empcode; char empname[20]; f loat salary;public: employee(int eno, char name[], float sal) //constructor

    {empcode = eno; strcpy(empname, name); salary = sal;

    }

    void display(){

    cout

  • 8/6/2019 Advance Concept Class

    49/68

    Overloading constructors#include class point{

    private: int x;int y;

    public: point() //default constructor{ x = 0; y = 0; }point (int xp, int yp) //parameterized constuctor{

    x = xp;y = yp;

    }void display(){

    cout

  • 8/6/2019 Advance Concept Class

    50/68

    void main(){

    point P1, P2(10,20); //objects P1 and P2 are createdcout

  • 8/6/2019 Advance Concept Class

    51/68

    Copy constructor

    y

    A copy constructor is used to initialize an object fromanother object;

    y Ex: box b1;

    y Box b2(b1); or box b3=b1;

    y

    B2=b1; will not invoke the copy constructory A copy constructor takes a reference to an object of

    the same class as itself as an argument

  • 8/6/2019 Advance Concept Class

    52/68

    Example:

    #include

    #include

    Class code

    {

    int id;

    public:

    code() { }

    code(int a){ id=a;}

    code(code &a){ id=a.id;}

    void display()

    { cout

  • 8/6/2019 Advance Concept Class

    53/68

    Void main(){

    code a(100);code b(a);code c=a;code d;clrscr();

    cout

  • 8/6/2019 Advance Concept Class

    54/68

  • 8/6/2019 Advance Concept Class

    55/68

    DestructoryA destructor is a special member function which de-

    allocate the memory

    y

    It has the same name of the class but with (tilde)~symbol

    y Ex: ~sample(){ }

  • 8/6/2019 Advance Concept Class

    56/68

    Characteristics ofa destructors

    yA destructor is invoked automatically by the compilerupon exit of program

    yA destructor does not return the value

    yA destructor cannot be declared as static, const

    yA destructor must be declared in public section

    y If the constructor use new to allocate the memory thendelete should be used to de-allocate the memory

  • 8/6/2019 Advance Concept Class

    57/68

    #include class sample{

    private: int x;public: sample() //constructor

    {x = 0;cout

  • 8/6/2019 Advance Concept Class

    58/68

    void main(){

    sample s1;s1.printx();

    }

  • 8/6/2019 Advance Concept Class

    59/68

    How the executionofconstructors& destructors

    #include class sample

    {public: sample(void) //constructor

    {count = count + 1;cout

  • 8/6/2019 Advance Concept Class

    60/68

  • 8/6/2019 Advance Concept Class

    61/68

    Class conversions with constructorsyAconverting constructoris a single-parameter

    constructor that is declared without the functionspecifies explicit. The compiler uses convertingconstructors to convert objects from the type of thefirst parameter to the type of the convertingconstructor's class

  • 8/6/2019 Advance Concept Class

    62/68

    Example:#include#include

    class Y {int a, b;char* name;

    public:Y(int i) { cout

  • 8/6/2019 Advance Concept Class

    63/68

    y The above example has the following two convertingconstructors:

    y Y(int i)which is used to convert integers to objects ofclass Y.

    y Y(const char* n, int j = 0) which is used to convertpointers to strings to objects of class Y.

  • 8/6/2019 Advance Concept Class

    64/68

    Conversion functionsyA conversion function that belongs to a class X

    specifies a conversion from the class type X to the typespecified by the conversion type.

  • 8/6/2019 Advance Concept Class

    65/68

    Example:#include

    #includeclass Y {int b;

    public:void acc(){

    cout

  • 8/6/2019 Advance Concept Class

    66/68

    void main(){

    clrscr();Y a;

    a.acc();f(a);getch();

    }

  • 8/6/2019 Advance Concept Class

    67/68

    NoteyAll three statements in function f(Y) use the

    conversion function Y::operator int().

    y Conversion functions have no arguments, and thereturn type is implicitly the conversion type.

    y Conversion functions can be inherited.

    yYou can have virtual conversion functions but notstatic ones.

    y

    You cannot use a conversion function to convert anobject of type A to type A, to a base class of A, or tovoid.

  • 8/6/2019 Advance Concept Class

    68/68