c++ presentatn

Upload: esther-ananthi

Post on 08-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 C++ PRESENTATN

    1/16

    ACCESS

    SPECIFIERS

  • 8/7/2019 C++ PRESENTATN

    2/16

    A class is an expanded concept of a datastructure: instead of holding only data, it canhold both data and functions.

    Classes are generally declared using thekeyword class, with the following format

    class class_name {

    access_specifier_1:

    member1;

    access_specifier_2:

    member2; ...

    } object_names;

  • 8/7/2019 C++ PRESENTATN

    3/16

    private members of a class areaccessible only f

    rom within other membersof the same class or from theirfriends.

    protected members are accessible frommembers of

    their same class and fromtheir friends, but also from members oftheir derived classes.

    Finally, public members are accessiblefrom anywhere where the object is visible.

  • 8/7/2019 C++ PRESENTATN

    4/16

    PRIVATE

    The private keyword

    specifies that thosemembers are accessibleonly from member functionsand friends of the class.

  • 8/7/2019 C++ PRESENTATN

    5/16

    #include

    using namespace std;

    class CRectangle

    {

    int width, height; output:

    public:CRectangle (); rect area: 25

    CRectangle (int,int);

    int area (void) {

    return (width*height); rectb area: 12

    }

    };

    CRectangle::CRectangle ()

    { width = 5;height = 5;

    }

    CRectangle::CRectangle (int a, int b)

    {

    width = a;

    height = b;

    }

    int main (){

    CRectangle rect (3,4); // implicit call

    CRectangle rectb; //calls default constructor

    cout

  • 8/7/2019 C++ PRESENTATN

    6/16

    PUBLIC

    public members are accessible from

    anywhere where the object is visible

    Egpublic :

    int a, b;

    void getdata();

  • 8/7/2019 C++ PRESENTATN

    7/16

    #include using namespace std;

    class CRectangle

    {

    int x, y;public:

    void setvalues (int,int);

    int area () OUTPUT:

    {

    return (x*y); area: 12}};

    void CRectangle::set_values (int a, int b)

    {

    x = a;

    y = b;

    }

    int main ()

    {

    CRectangle rect;

    rect.set_values (3,4);

    cout

  • 8/7/2019 C++ PRESENTATN

    8/16

    PROTECTED ACCESS

    SPECIFIER:

    Accessible by member functionswithin its class and any classimmediately derived from it

    When protected member inherited in:1. public mode : It becomes protected in

    derived class

    2. private mode:It becomes private inderived class

    3. protected mode: It becomes protectedin derived class

  • 8/7/2019 C++ PRESENTATN

    9/16

    When it is appropriate for a

    class's subclasses to haveaccess to the

    method or field ,but not forunrelated classes.

    WHEN TO USE PROTECTED ACCESS

    SPECIFIER ?..

  • 8/7/2019 C++ PRESENTATN

    10/16

    #include

    #include

    class shape

    {

    protected:

    char colour[30];

    public:

    void get_colour()

    {

    coutcolour;

    cout

  • 8/7/2019 C++ PRESENTATN

    11/16

    class three_d:public shape

    {

    protected:

    int c_len;

    public:

    void get_clen()

    {

    cout

  • 8/7/2019 C++ PRESENTATN

    12/16

    OUTPUT:Enter colour:

    BLUE

    Inside shape class::BLUE

    Enter length for square:

    20.0

    Area of square : 400

    Enter colour:

    RED

    Inside shape class::RED

    Enter the lenth of cube:

    25.0

    Volume of cube : 15625

  • 8/7/2019 C++ PRESENTATN

    13/16

    #include

    #include

    class shape

    {

    protected:

    char colour[30];

    public:

    void get_colour()

    {

    coutcolour;

    cout

  • 8/7/2019 C++ PRESENTATN

    14/16

    class three_d:public shape

    {

    protected:

    int c_len;

    public:

    void get_clen()

    {

    cout

  • 8/7/2019 C++ PRESENTATN

    15/16

    ErrorPROGRAM.CPP: 'shape::get_colour()' is not accessible

    ERROR ISSHOWN AS:

  • 8/7/2019 C++ PRESENTATN

    16/16

    THANK YOU!