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

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Tutorial 6 February 4th/5th, 2015
Object-Oriented PHP (1)
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
ASP.NET Programming with C# and SQL Server First Edition
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Review of C++ Programming Part II Sheng-Fang Huang.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Objects and Classes Mostafa Abdallah
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Object Oriented Programming
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Abstract Data Types Programmer-created data types that specify
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Introduction to Classes
Chapter 5 Classes.
This technique is Called “Divide and Conquer”.
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
C++ Classes & Object Oriented Programming
Introduction to Classes
Corresponds with Chapter 7
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 9 Objects and Classes
Dr. Bhargavi Dept of CS CHRIST
Introduction to Classes and Objects
Classes C++ representation of an object
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

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.

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…

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 …

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

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

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.

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?

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();

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

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

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

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

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;

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

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

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.

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;

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)

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);

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.

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

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.

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

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.

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

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

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

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; }

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.

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

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;

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