Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.

Similar presentations


Presentation on theme: "Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output."— Presentation transcript:

1

2 Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output data. Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" data rather than logic.

3 What is an object? An entity in the real world: a student, a desk, a circle, a button, a window… How do we describe an object? (a student, for example) State of an object (properties or attributes) name, ID, year, address… Behavior of an object (actions) drop a class, add a class…

4 Some more examples of objects Thinking of a circle as an object: Properties: radius (or diameter) Actions: calculate the area of the circle, calculate the circumference of the circle …

5 Classes A class is a template that defines objects of the same type.

6 Classes and Objects A Class is like a blueprint and objects are like houses built from the blueprint

7 Why OOP? The definition of a class is reusable. It is possible to define subclasses to inherit all of the main class characteristics. Person  Student, Teacher The concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.

8

9 OOP Languages Simula (1967) was the first object-oriented programming language. Java, Python, C++, Visual Basic.NET and Ruby are the most popular OOP languages today. C vs. C++: What is that “++” part?

10 How to describe an object? A circle is an object. Properties: radius (or diameter)  member variables double radius; Actions: get the area of the circle  member functions double getArea();

11 How to describe an object? An object is a bundle of variables and related functions - encapsulation

12 Let’s try to define this class in C++!

13 Class Circle Definition class Circle { public: double radius; //member variable double getArea( ); //member function }; Remember this semicolon!

14 Defining a Member Function double Circle::getArea() { double area = 3.14 * radius * radius; return area; } ‘::’ is the scope resolution operator.

15 How to create an object? You can create many objects of a class in the main function. Declare two circle objects: Circle circle1; Circle circle2;

16 Accessing Member Variables Syntax: Object. Member_Variable Example: circle1.radius = 5; circle2.radius = 10; dot operator

17 Calling Member Functions Syntax: Object. Member_Function Example: circle1.getArea( ); circle2.getArea( );

18 Visibility Modifiers  public The member variable or the member function is visible to any function (incl. main) in the program.  private The member variable or the member function can be accessed only from within its own class definition. By default, if no modifier is specified, the class members are private.

19 Member variables should be private. This is not good: circle2.radius = 5; Why? Data is not protected. class becomes difficult to maintain. Member variables should be private. private: double radius;

20 Then how can we give a value to radius of circle1? circle1.radius = 5 does NOT work any more! The get and set member functions are used to read and modify private properties. getRadius(): to read setRadius(double r): to write Accessor(get) and Mutator(set)

21 Using const With Member Functions const appearing after the parentheses in a member function declaration specifies that the function will not change any data in the calling object. double getRadius() const; void setRadius(double r);

22 Exercise: The Rectangle Class Follow the example of the Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double member variables named width and height. A member function named getArea(). A member function named getPerimeter(). The accessor and mutator for the two private variables. In main, create two rectangle objects. Set one rectangle to have width of 10, height of 5. Retrieve another rectangle‘s width and height from the user input; Print their width, height, area and perimeter.

23 You can combine all the set functions into one. void set(double w, double h);

24 Add some more.. Assume inch is used in the Rectangle class. Add another member function getAreaInFeet() to return the area of the rectangle in square feet.

25 Constructors Constructor is a special type of member function, that is invoked to create a new object. By default: (if no constructor is provided) Circle() { } Circle circle1; //Calling the default constructor

26 Facts about Constructors Circle() { } A constructor is usually public. A constructor must have the same name as the class. Constructors do not have a return type (not even void). Constructors play the role of initializing objects.

27 Constructors It is a good idea to always include a default constructor. Circle() { radius = 1; }

28 Constructors You can overload constructors. Multiple constructors can have the same name but different signatures. Circle() { radius = 1; } Circle(double r) { radius = r; }

29 Calling A Constructor A constructor is not called like a normal member function: Circle circle1; circle1.Circle(10); circle1.getArea();

30 Calling A Constructor A constructor is called in the object declaration. Circle circle1(10); Circle circle1; // uses the default Circle constructor Circle() { radius = 1; } Circle(double r) { radius = r; }

31 Add some more.. A no-arg constructor that creates a rectangle with width of 1 and height of 1. A constructor that creates a rectangle with the specified width and height. In the main function, create two Rectangle objects by invoking the two different constructors.

32 Destructors Member function automatically called when an object is destroyed Destructor name is ~ classname, e.g., ~Rectangle Has no return type; takes no arguments Only one destructor per class, i.e., it cannot be overloaded If constructor allocates dynamic memory, destructor should release it

33 Pointer to an Object Can define a pointer to an object: Rectangle *rectPtr = new Rectangle; Can access public members via pointer: rectPtr->setWidth(10); rectPtr->setHeight(15); delete rectPtr;

34 Let’s move the whole class definition to another file! Save the project for your own reference!


Download ppt "Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output."

Similar presentations


Ads by Google