CPP Basics

Embed Size (px)

Citation preview

  • 7/29/2019 CPP Basics

    1/10

    Concepts of OOPs

    C++

    Object Oriented Programming

    Object Oriented Programming is a powerful way to approach the task

    of programming. Object Oriented Programming encourages you to

    decompose a problem into its constituent parts. Each component becomes a

    self-contained object that contains its own instructions and data that relate to

    that object.

    Concepts of OOPs

    1. Encapsulation.- Encapsulation is the mechanism that binds together code

    and the data it manipulates, and keeps them safe from theoutside reference and misuse.

    - When code and data are linked together, an objectiscreated. In other words, an object is the device that supports

    encapsulation.

    2. Inheritance.- Inheritance is the process by which one object can acquire

    the properties of another. More specifically, an object caninherit a general set of properties to which it can add thosefeatures that are specific only to it.

    - It allows an object to support the concept ofhierarchicalclassification.

    - E.g. House is part of a general class Building -> Structure ->Man-made.

    3. Polymorphism.- Polymorphism (In Greek Poly means manyand Morphs

    means forms). Hence Polymorphism means many forms.- The advantage of polymorphism is that it reduces the

    complexity by allowing one interface to specify a general

  • 7/29/2019 CPP Basics

    2/10

    Object Oriented Programming through C++

    class of action. It is the compilers job to select the specificaction as it applies to each situation.

    - E.g. The functions abs( ), labs( ) and fabs( ) computes andreturns the absolute value of an integer, long and float value.However, in C++, which supports polymorphism, each

    function can be called by same name, such as abs( ). Thetype of the data used to call the function determines whichspecific version of the function is actually executed.

    cout

    - It is predefined stream that is automatically linked to theconsole when a C++ program begins execution.

    - E.g. cout variable;- >> is an operator known as extraction operator.

    To use the C++ I/O operators, the header file iostream.h must be

    included in the program.

    class

    - The most important feature of C++ is the class. The class isthe mechanism that is used to create objects.

    - It basically defines a new type. It determines what an objectof that type will look like.

    - Syntax:class class-name

    {

    // private variables and functions

    Created by: - Bhaumik Patel

    2

  • 7/29/2019 CPP Basics

    3/10

    Object Oriented Programming through C++

    public:

    // public variables and functions

    } object-list;

    - The object - listis optional.- The variables and functions declared inside a class are said

    to be members of that class. By default, all variable andfunctions declared inside a class are private to that class.

    This means that they are accessible only by the members ofthat class. To declare public class members, the publickeyword is used, followed by a colon. All variables andfunctions declared after the public specifier are accessibleboth by the members of the class and by any other part ofthe program that contains the class.

    Object

    - Object is an instance of a class.- We can consider it as a variable of the class type.- An object declaration creates a physical entity of the class

    type. That is an object occupies physical memory space, butthe class does not.

    - Each object of a class has its own copy of every variabledeclared within the class. But the member functions are not

    copied, instead they are shared by every object.

    Access Modifiers

    - private, public and protected are known as Access Modifiersas they modify the access privileges of the members of aparticular class.

    Reference

    - Reference is an implicit pointer that acts like another namefor a variable.

    - That is, reference is an alias to an existing vaiable.- E.g. int x = 5;

    Created by: - Bhaumik Patel

    3

  • 7/29/2019 CPP Basics

    4/10

    Object Oriented Programming through C++

    int &y = x;

    Notice the & in front of y. Here y is a reference to x.

    Thus any change to y will affect x and vice versa.

    - Reference variables cani. Be passed to a function.ii. A reference can be returned by a function.iii. An independent reference can also be created.

    Call by Reference to a function.

    - E.g. Consider the following function:

    void swap( int &a, int &b )

    {

    .

    }

    When the function is called as show below:

    int x = 5;

    int y = 7;

    swap( x, y );

    it exhibits a call by reference as

    a references x and b references y.

    Function Overloading

    - When two or more functions are having same name butdifferent signature then such functions are said to beoverloaded functions.

    - The signature of the functions can differ in three ways:i. Number of parameters.ii. Type of parameters.iii. Sequence of parameters.

    Created by: - Bhaumik Patel

    4

  • 7/29/2019 CPP Basics

    5/10

    Object Oriented Programming through C++

    E.g.

    iv. void swap( int, int );v. void swap( float, float );vi. void swap( char, char );

    vii. void swap( char *,char *);viii. int abs( int );ix. float abs( float );x. double abs( double );xi. void set( int );xii. void set( int, int );xiii. void set( int, char );xiv. void set( char, int );

    Default Arguments

    - Default arguments allow you to give a parameter a defaultvalue when no value is specified to that parameter at thetime of the function call.

    - E.g. void f ( int a, int b=7 );Now this function can be called in the following ways:

    f( 5,7 );

    f( 4 ); // here the second argument b is not passed

    So b is assigned the default value 7.

    - The default arguments can not be followed by ordinaryarguments.

    i.e. void f ( int a, int b=0, int c ); // not valid

    void f ( int a, int b=0, int c=2 ); // valid

    void f ( int a=2, int b=1, int c=4 ); // valid

    Inline Functions

    - Inline functions are not actually called but, rather, areexpanded in line, at the point of each call.

    - This is much same as the macros in C.- The advantage ofin-line functions is that they have no

    overhead associated with the function call and return

    Created by: - Bhaumik Patel

    5

  • 7/29/2019 CPP Basics

    6/10

    Object Oriented Programming through C++

    mechanism which means that they can be executed muchfaster than the normal functions.

    - The disadvantage is that if they are too large and called toooften, then the program grows larger and so only shortfunctions are declared inline.

    - E.g. inline int even( int x ){ return !(x%2); }ioid main( ){

    if( even(10) ) cout

  • 7/29/2019 CPP Basics

    7/10

    Object Oriented Programming through C++

    - Constructor is automatically called each time an object of theclass is created.

    - It is generally used for initializing the member variables.- As constructors are also functions, constructor overloading is

    also possible.

    - E.g.class Student

    {

    int rollno;

    public:

    Student( )

    {

    rollno = 1;

    }

    Student( int a )

    {

    rollno = a;

    }

    };

    Student s; // This line creates an object and

    hence invokes the constructor

    Student s1( 5 ); // Invokes parameterized constructor.

    new

    - new is operator which is used for dynamic memoryallocation.

    - Syntax: pointer_variable = new

    - E.g.int *n = new int;

    Created by: - Bhaumik Patel

    7

  • 7/29/2019 CPP Basics

    8/10

    Object Oriented Programming through C++

    long *arr = new long[ 5 ] ;

    - The case of dynamic object creation is as follows:Student *s;

    // This line does not invoke the constructor as s isonly a pointer which can point to a Student object.

    s = new Student;

    // Here the new operator creates an object and

    hence the constructor is invoked.

    delete

    - delete is an operator which is used to free the occupiedmemory.

    - Syntax : delete ;- E.g. int *p;

    void main( )

    {

    p = new int;

    delete p;

    }

    - Whenever the scope of a variable completes the variablegets deleted automatically and the memory occupied by thatvariable is also cleared.

    - But when the memory allocation is done dynamic as shownin above example, when the scope completes only thepointer gets deleted but the memory location to which thepointer was pointing is not made free automatically. It has tobe done explicitly and is done with the use of

    deleteoperator.- E.g. int *p;

    void main( )

    {

    P = new int[ 5] ;

    delete [ ] p;

    Created by: - Bhaumik Patel

    8

  • 7/29/2019 CPP Basics

    9/10

    Object Oriented Programming through C++

    }

    Here the statement delete [ ] p deletes each

    element in the array. It does not cause p to be freed

    multiple times. p is still freed only once.

    Destructor

    - Destructor is a member function whichi. Has same name as the class and prefixed by ~ tilde

    character.ii. Does not have a return type.

    - It is automatically invoked whenever an object is deleted.- It is basically used to free the memory occupied the

    members.- E.g.class Array

    {

    int *arr;

    public:

    Array( ) { arr = new int[ 7 ]; }

    ~Array( ) { delete arr; }

    };

    this

    - this is a pointer which point to the current object.- It is automatically passed to the any member function when

    the function is called.- E.g.

    class Inventory

    {

    double cost;

    public:

    Inventory( double cost )

    Created by: - Bhaumik Patel

    9

  • 7/29/2019 CPP Basics

    10/10

    Object Oriented Programming through C++

    {

    this->cost = cost;

    // R.H.S cost variable is parameter

    variable.

    // L.H.S cost variable is member

    variable.

    }

    };

    void main( )

    {

    Inventory iobj ( 500 );

    }

    Object Oriented Programming through C++

    Created by: - Bhaumik Patel

    10