Top Banner

of 17

12438_lecture-08-oop

Apr 04, 2018

Download

Documents

Ankesh Kunwar
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/29/2019 12438_lecture-08-oop

    1/17

    Object Oriented Programming using C++

    lecture 08(Access Specifiers)

  • 7/29/2019 12438_lecture-08-oop

    2/17

    Object Oriented Concepts

    Compare and contrast the structure, and

    class in C++

    OOP Features

    What is the concept of CLASS?

    How to define and implement a class?

    Can we access the class members? Why Public or Private?

  • 7/29/2019 12438_lecture-08-oop

    3/17

    Structure in C++ vs. Classes

    Similarities Both are derivedtypes. Both present similar features Both are automatictypedefs

    Class private members by default

    Structure public members by default

    The data members are thus accessible out of thestructure implementation, e.g., Client program

    Classes are more advanced than structures

  • 7/29/2019 12438_lecture-08-oop

    4/17

    OOP Features

    Abstraction

    Encapsulation and data hiding

    InheritancePolymorphism

    Reusable CODE

  • 7/29/2019 12438_lecture-08-oop

    5/17

    Class

    Class is an Object-Oriented ProgrammingConcept.

    A class is a programmer-defined data

    type. It consists of data members andfunctions which operate on that data.

    Data Members define Object attributes

    Functions define Object Behavior

  • 7/29/2019 12438_lecture-08-oop

    6/17

    C++ Class

    Classes are derivedtypes.

    Class enables the programmer to model

    objects that have attributes (represented as

    data members), and behaviors or

    operations or functionality(represented asmember functions).

    Member functions are sometimes calledmethodsin otherobject-oriented languages.

  • 7/29/2019 12438_lecture-08-oop

    7/17

    C++ Class Example1 class Time {

    2public:

    3 Time();

    4 void setTime( int, int, int );

    5 void printMilitary();

    6 void printStandard();

    7private:

    8 int hour; // 0 - 23

    9 int minute; // 0 - 59

    10 int second; // 0 - 59

    11};

    Public: and Private:

    are member-access

    specifiers.

    setTime,printMilitary,

    andprintStandardare

    member functions.Time is the constructor.

    hour,minute, and

    secondare data

    members.

  • 7/29/2019 12438_lecture-08-oop

    8/17

    C++ Class Declaration

    To declare a class

    Theclass keyword is used

    The declaration is enclosed in braces {};

    The class definition ends with a semicolonEvery piece of code inside the braces is

    called class body

    The class body thus contains classmembers

  • 7/29/2019 12438_lecture-08-oop

    9/17

    C++ Class Class Members

    A classs declaration declares the classs

    members, which may consist of

    The data members

    The constructor & destructorfunctions

    Specialized member functions

    Othermember functionsInclude the functionality that can be invokedon the object of this class (or type)

  • 7/29/2019 12438_lecture-08-oop

    10/17

    Data Members

    A data member can be of any valid C++

    data type. For example

    Ordinary variableprimitive type

    An instance variable of another class

    programmer defined type

    A pointer or reference variable

  • 7/29/2019 12438_lecture-08-oop

    11/17

  • 7/29/2019 12438_lecture-08-oop

    12/17

    public:

    Public members can be accessed bymember functions as well as by functions

    that declare the objects of that class.

    A struct has all members public bydefault.

    Normally, for classes, those member

    functions are made public which mustinteract with the outside world.

  • 7/29/2019 12438_lecture-08-oop

    13/17

    private:

    Private memberscan only be accessed

    by member functions.

    This means they can only be accessed inthe member function body.

    Remember the members mean both

    variables and functions.By default all class members are private,

    unless declared otherwise.

  • 7/29/2019 12438_lecture-08-oop

    14/17

    public: OR private:

    It is not compulsory for all members

    functions to be public and all data

    members to be private.

    Some member functions can also be

    private.

    Similarly, data members can also be

    public.

    Violation ofEncapsulationprinciple

  • 7/29/2019 12438_lecture-08-oop

    15/17

    protected:

    Similar to private they cannot be

    accessed directly from the outside world

    Mainly used with inheritance

    We will study this later Inheritance in

    C++

  • 7/29/2019 12438_lecture-08-oop

    16/17

    Initialization

    Class data membercannot be initializedat declaration time.

    By default all data members have

    garbage value.If all members are declared public then,they can be initialized at objectdeclaration time.

    Similar to structure initialization time.

    Otherwise we require a constructorforthis.

  • 7/29/2019 12438_lecture-08-oop

    17/17

    MemberFunctions

    Member functions are those functions that you

    declare within the class definition.

    Definition of these functions must be provided,

    like any other function.There are a number of categories of functions.

    Constructor/Destructor

    Regular member functions

    Overloaded operator functions

    Virtual functions

    Friend functions