Lecture 09 Oop 2010

download Lecture 09 Oop 2010

of 43

Transcript of Lecture 09 Oop 2010

  • 8/2/2019 Lecture 09 Oop 2010

    1/43

    Introduction to ProgrammingEnvironments (C++/UNIX)

    IPE 115, Semester 1, 2010

    Lecture 09

    Structures, OOP

  • 8/2/2019 Lecture 09 Oop 2010

    2/43

    Structures

  • 8/2/2019 Lecture 09 Oop 2010

    3/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    3

    Structures

    A structure is a collection of variables

    referenced under one name.

    The variables that make up the structure are

    called members, elements or fields.

    Generally, all the members of a structure are

    logically related.

    Ex: a structure can contain information about a

    persons name, address, age, height etc.

  • 8/2/2019 Lecture 09 Oop 2010

    4/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    4

    Declaration of A Structure

    struct car_part

    {

    int part_num;

    char color;

    double price;};

    //To declare a variable of type car_part,

    struct car_part partInfor;

    Name of thestructure

    Name of thevariable

  • 8/2/2019 Lecture 09 Oop 2010

    5/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    5

    Accessing Members of A Structure

    The . operator (dot operator) is used to access the structuremembers.

    Values can be inserted into the members of the structure as,

    partInfor.part_num = 100012;partInfor.color = r;

    partInfor.priice = 4000.00;

    Similarly, values can be read from the members of a structure

    as,

    int temp;

    temp = partInfor.part_num;

  • 8/2/2019 Lecture 09 Oop 2010

    6/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    6

    Abstract Data Types

    We can develop new data types, which consist

    of data elements and operations on those data

    elements.

    We describe these data types in a general

    manner, which is not implementation

    dependent.

    In C++, we will use the Class feature to

    implement our abstract data types.

  • 8/2/2019 Lecture 09 Oop 2010

    7/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    7

    C++ is a hybrid language

    Up to now, we have dealt with C++ using theprocedural paradigm.

    We break our problem into subtasks and implement thesubtasks using functions, building our main programfrom these functions.

    C++ also allows us to build programs using theobject-oriented paradigm.

    Also takes into account both code and data related toeach subtask/subgroup.

    Organize these subgroups into a hierarchical structure

    Translate these subgroups into self-contained units calledobjects.

  • 8/2/2019 Lecture 09 Oop 2010

    8/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    8

    Object Oriented Programming

  • 8/2/2019 Lecture 09 Oop 2010

    9/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    9

    Classes and Objects

    A class is the abstract definition of the data

    type. It includes the data elements that are part

    of the data type, and the operations which are

    defined on the data type.

    An object is a specific instance of the data

    type.

  • 8/2/2019 Lecture 09 Oop 2010

    10/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    10

    Classes and Objects

    An Object is an entity. It could be a realentity like a person a plane or somethingthat is imaginary.

  • 8/2/2019 Lecture 09 Oop 2010

    11/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    11

    Classes and Objects

    You can describe theentity by itsproperties and its

    behaviors.

    Plane

    ModelAirLineNoOfPassengersFromTo

    TakeOff

    FlyLand

    Properties

    Methods

  • 8/2/2019 Lecture 09 Oop 2010

    12/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    12

    Classes and Objects

    A person working in a company

    Person

    EmpNoNameAddressBasicSal

    OtHrsOtRate

    CalcOtAmtCalcNetSal

    Properties

    Methods

  • 8/2/2019 Lecture 09 Oop 2010

    13/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    13

    Classes and Objects

    You describe the objectsdetails in a Class. Aclass is a blue print of anobject.

    Blue PrintHouse3

    House2

    House1

  • 8/2/2019 Lecture 09 Oop 2010

    14/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    14

    Objects

    In C++ we have the data type integer and

    various operations we can perform on integers.

    However we do not get a specific instance of

    an integer until we declare an integer variable.

    Likewise to define a specific instance of a

    class we declare an object (class variable).

  • 8/2/2019 Lecture 09 Oop 2010

    15/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    15

    Defining and Instantiating Objects

    class HouseHouse2

    House1

    House1, House2, House3 are objects.They are instances of the house class.

    House House1, House2;In C++ you can also useHouse *House1 = new House()

  • 8/2/2019 Lecture 09 Oop 2010

    16/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    16

    Properties are Private

    Typically Properties are keptprivate. We use methods toaccess the value of theproperties.

    e.g. setName, getName,

    setEmpNo, getEmpNo

    Person

    EmpNoNameAddress

    BasicSalOtHrsOtRate

    CalcOtAmtCalcNetSal

    Properties

    Methods

  • 8/2/2019 Lecture 09 Oop 2010

    17/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    17

    Using Objects

    int main() {House myHouse;

    myHouse.drawing(plan5.dwg);

    myHouse.show();

    return 0;

    }

  • 8/2/2019 Lecture 09 Oop 2010

    18/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    18

    Using Dynamic Objects

    int main() {House *myHouse;

    myHouse = new House();

    myHouse->drawing(plan5.dwg);

    myHouse->show();

    delete myHouse;

    return 0;

    }

  • 8/2/2019 Lecture 09 Oop 2010

    19/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    19

    Defining the class Rectangle

    //class definitionclass Rectangle{private: //definition of data members

    float length;

    float width;public: //definition of operations

    Rectangle(); //default constructorRectangle(int l, int w); //constructorfloat area();

    float perimeter();void changeLength(int l);void changeWidth(int w);

    }; //dont forget the semicolon

  • 8/2/2019 Lecture 09 Oop 2010

    20/43INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    20

    Pictorial Representation of Class Rectangle

    Rectangle

    perimeter

    area

    changeLength

    changeWidth

    length

    width

  • 8/2/2019 Lecture 09 Oop 2010

    21/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    21

    Private and Public sections

    The private part of the definition specifies thedata members of the class.

    These are hidden from outside of the class and

    can only be accessed through the operationsdefined for the class.

    The public part of the definition specifies the

    operations as function prototypes. These operations, or methods as they are

    called, can be accessed by the main program.

  • 8/2/2019 Lecture 09 Oop 2010

    22/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    22

    Defining the class Rectangle#include Rectangle.h

    //class implementationRectangle::Rectangle() //constructor{

    length = 0;width = 0;

    }

    Rectangle::Rectangle(int l, int w) //constructor{

    length = l;width = w;

    }

    float Rectangle::area(){

    return (length*width);}

  • 8/2/2019 Lecture 09 Oop 2010

    23/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    23

    Defining the class Rectangle

    float Rectangle::perimeter(){return (2*(length+width));

    }

    void Rectangle::changeLength(int l){

    length = l;}

    void Rectangle::changeWidth(int w){width = w;

    }

  • 8/2/2019 Lecture 09 Oop 2010

    24/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    24

    Some more information about constructors

    Every class should have a constructor.

    The constructor is used to initialize the object when itis declared.

    The constructor does not return a value, and has noreturn type (not even void).

    The constructor has the same name as the class.

    Polymorphic1 constructors are allowed.

    When an object is declared the appropriateconstructor is executed.

    The default constructor has no parameters, and theobject is not followed by an argument list.

    1. Polymorphism will be discussed formally in the next lecture

  • 8/2/2019 Lecture 09 Oop 2010

    25/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    25

    Declaring And Using Objects

    Objects are declared in a similar manner to thevariable of the built in types.

    class_name object_name; or

    class_name object_name(argument_list);

    Eg: Rectangle House1;Rectangle House2 (28.5, 13.5);

    To invoke a method on a particular object,

    object_name.method_name(argument_list);Eg:

    float floorSpace;

    floorSpace = House.area();

  • 8/2/2019 Lecture 09 Oop 2010

    26/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    26

    Clients

    Programs that use classes and define objects

    are called clients.

    The only contact the client has with the private

    data members of the object is through the

    methods.

    Client DataMembersmethods

    class

  • 8/2/2019 Lecture 09 Oop 2010

    27/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    27

    A Sample Client

    Write a program which will determine the square

    yardage to be mowed for a rectangular yard, given

    the dimensions of the yard and the dimensions of the

    house on that yard. It will also determine the cost ofmowing the lawn which is so much per square yard.

    House

    Yard

  • 8/2/2019 Lecture 09 Oop 2010

    28/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    28

    Design Of Solution

    Objects : Property //the entire yardHouse //the house

    Strategy:

    Input dimensions of property and house. Calculate area of property.

    Calculate area of house.

    Subtract area of house from area of property.

    Input cost for mowing a square yard.

    Determine the cost to mow the lawn.

  • 8/2/2019 Lecture 09 Oop 2010

    29/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    29

    The Client Program

    #include #include Rectangle.husing namespace std;int main(){

    //length and width of rectanglefloat rlength, rwidth;

    //Enter data for yardcout rlength >> rwidth;

    //Define Yard objectRectangle Yard(rlength,rwidth);

  • 8/2/2019 Lecture 09 Oop 2010

    30/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    30

    Client Program continued

    //Enter data for house

    cout rlength >> rwidth;

    //Define House object

    Rectangle House(rlength,rwidth);

    //Declare variables for Area of House, Yard and Lawn.

    float houseArea, yardArea, lawn;

    //Compute Area of House and Yard using Area method.

    houseArea = House.area();

    yardArea = Yard.area();

  • 8/2/2019 Lecture 09 Oop 2010

    31/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    31

    Client Program continued

    //Compute Area of Lawn.lawn = yardArea houseArea;

    //Input cost of mowing 1 square yard.

    float mowCost;

    cout > mowCost;

    //Output cost of mowing lawn.

    cout

  • 8/2/2019 Lecture 09 Oop 2010

    32/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    32

    Separate Compilation

    Generally the definition of a class is placed in afile, which is named by the class name and file

    extension h.

    Rectangle.h

    The implementation is then placed in a file which

    is named by the class name and file extension

    cpp. This file must include the definition (.h) file.

    Rectangle.cpp

    The .cpp file can then be compiled by itself.

  • 8/2/2019 Lecture 09 Oop 2010

    33/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    33

    Using Separately Compiled Files

    Linux To compile Rectangle.cpp separately, type

    - g++ Rectangle.cpp (this will generate an object file) andtype

    - g++ -o prog prog.cpp Rectangle.o

    - OR type g++ -o prog prog.cpp Rectangle.cpp

    Visual C++

    The main program is just one file in a project.

    The main program should #include the .h files for anyclasses it is going to use.

    The project should include the .h and .cpp files for theclasses that are going to be used.

    Compile the main program, and then build the executable

    file.

  • 8/2/2019 Lecture 09 Oop 2010

    34/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    34

    Principles of Object-Oriented Design

    Encapsulation

    C++ uses classes for encapsulating a data type

    Information Hiding

    The use of private provide the hiding of the implementation of the data,

    and protects it from use expect through the methods.

    Polymorphism

    The use of the same function name or operator in different ways based

    on the arguments.

    Inheritance

    The ability to build a hierarchy of classes.

    Dynamic Binding

    The ability to determine which function to use at run-time.

    h d f l d h

  • 8/2/2019 Lecture 09 Oop 2010

    35/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    35

    When defining a class consider the

    following types of methods

    Constructors

    Initialize objects when declared

    Access methods

    Return or output the value of a data member

    Transformer methods

    Modify a data member

    Manipulator methods

    Produce summary information about the object

    Iterators

    Allow you to move from one object to the next in a collection ofobjects

    Destructors

    Clean up when object goes out of scope.

  • 8/2/2019 Lecture 09 Oop 2010

    36/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    36

    Polymorphism

    Polymorphism occurs when a single function

    name represents different tasks.

    Operator overloading is, multiple

    implementations of the actions of an operator,

    where the particular implementation depends

    on the types of the operators.

    Also called, One Interface Multiple Methods

  • 8/2/2019 Lecture 09 Oop 2010

    37/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    37

    Polymorphism

    You can have multiple functions in a programwith the same function name provided theydiffer in either the number of parameters, or

    the data type of the parameters. Two functions with the same name and the

    same parameters (same number and type)cannot have same return types.

    The compiler selects the appropriate functionby matching the arguments to the parameters.

  • 8/2/2019 Lecture 09 Oop 2010

    38/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    38

    Example of Polymorphic Functions

    #include

    using namespace std;

    int area(int side);int area(int length, int width);int area(double radius);

    int main(){

    cout

  • 8/2/2019 Lecture 09 Oop 2010

    39/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    39

    Inheritance

    Is a process by which one object can acquire

    the properties of another object.

    Supports the concept of classification.

    Buildingroomsfloorsarea

    Housebedroomsbaths

    Schoolclassroomsoffices

  • 8/2/2019 Lecture 09 Oop 2010

    40/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    40

    Base and Derived classes

    The class that is inherited is referred to as a

    base class. ( Building )

    The class that does the inheriting is called the

    derived class. ( House , School )

    The derived class can also be used as a base

    class by another derived class.

  • 8/2/2019 Lecture 09 Oop 2010

    41/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    41

    How to inherit a class

    Class derived_class_name : access base_class_name

    {

    //body of class

    };

    When one class inherits another, the membersof the base class become members of the

    derived class.

    The access status of the base class membersinside the derived class is determined by

    access.

  • 8/2/2019 Lecture 09 Oop 2010

    42/43

    INTRODUCTION TO PROGRAMMING ENVIROMENTS (C++/UNIX)

    42

    Access

    Class derived_class_name : access base_class_name{

    //body of class

    };

    The access specifiers are:public,private andprotected. public:

    - All public members of the base class become publicmembers of the derived class.

    - All protected members of the base class become protectedmembers of the derived class.

    - Bases private members remain private to the base

  • 8/2/2019 Lecture 09 Oop 2010

    43/43

    43

    Access (cont..)

    Class derived_class_name : access base_class_name{

    //body of class

    };

    The access specifiers are:public,private andprotected. private:

    - All public and protected members of the base classbecome private members of the derived class.

    protected:

    - All public and protected members of the base classbecome protected members of the derived class.