Matthew Small Introduction to Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Fields, Constructors, Methods
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
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. 
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 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.
Writing Classes (Chapter 4)
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
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.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Learners Support Publications Object Oriented Programming.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
Object Oriented Programming COP3330 / CGS5409.  Classes & Objects  DDU Design  Constructors  Member Functions & Data  Friends and member functions.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Review What is an object? What is a class?
Andy Wang Object Oriented Programming in C++ COP 3330
Introduction to Classes
This technique is Called “Divide and Conquer”.
Object Oriented Programming COP3330 / CGS5409
C++ Classes & Object Oriented Programming
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Packages and Interfaces
Classes and Objects.
CLASSES AND OBJECTS.
COP 3330 Object-oriented Programming in C++
Classes C++ representation of an object
Lecture 8 Object Oriented Programming (OOP)
C++ Object Oriented 1.
Presentation transcript:

Matthew Small Introduction to Object-Oriented Programming

Object-oriented Basics Object-oriented programming languages allow a programmer to combine data and functions that act on that data into a single entity called an object. An object consists of: Name – a way of referring to an object inside a program. Member Data – data contained within an object. Member Functions – routines that act upon the data within an object. Interface – defines the ways a programmer may directly access the member data/functions of an object

Class A class is the blueprint for an object A class outlines member data, member functions, an interface and a class name to refer to it. A programmer can create one or more objects of a given class. Similar to building multiple houses from a single blueprint.

Interface An interface is specified in a class and affects the access a programmer has to objects of that class. At least two levels of protection can be specified within a class: public – member data/functions that are accessible both inside and outside the object private – member data/functions that are only accessible from the member functions of the object The public members of a class provide an interface for objects The private members of a class provide internal functionality and data representation for objects By convention, member data is kept private

Reasons for protection levels Makes interface simpler for programmer. More secure. Less chance for misuse (accidental or malicious). Class implementation easy to change without affecting other modules that use it.

How to Incorporate Objects DDU – declare, define, use Declare class choose class name, interface (protection levels) and member variables/functions without providing an implementation for member functions (function body) outlines the class functionality Define member functions write code that implements member functions completes class specification Use class to create object(s) use the specification provided by the class to create an object use object interface to interact with object

Class Declaration Format class { public: //members accessible externally and internally private: //members only accessible internally }; //remember to put ‘ ; ’ here

Example Class Declaration class Circle { public: void SetRadius(double r); //sets member variable radius to r double AreaOf(); // returns area of circle as a double private: double radius; // radius of circle };

Defining Member Functions There are two ways to provide the member function definitions for a class inside the class declaration using {} (we will not use) after the class declaration (this is the method we choose) How to refer to a member function: className::memberFuntionName this identifier refers to the member function memberFunctionName of class className (e.g. Circle::SetRadius) The double colon :: is called the scope resolution operator After the class declaration, member functions are defined just like any other function

Example Member Function Definition //Declaration: class Circle { public: void SetRadius(double r); // sets member variable radius to r double AreaOf(); // returns area of circle as a double private: double radius; // radius of circle }; //Definition (Implementation): void Circle::SetRadius(double r) { radius = r; // radius refers to this object’s member variable 'radius' } double Circle::AreaOf() { return (3.14*radius*radius); }

Object Use After a class has been declared and defined, an object of that class can be be declared (also known as creation or instantiation) and used. A programmer can declare an object with the following format: ClassName ObjectName; This statement creates an object based on the blueprint of class ‘ClassName’ and the object can be referred to by the identifier (variable name) ‘ObjectName’

Accessing an Object’s Public Members The ‘. ’ (dot) operator can be used to access an object’s public members The format for referring to an object’s member is: ObjectName.MemberFunction() OR ObjectName.MemberVariable Remember that this only works for public members of an object and since, by convention, member data is kept private, we will not use the second format.

Putting it All Together See sample1.cpp To recap, this program: declares the class Circle and outlines its members and interface defines the implementation for the member functions of the Circle class declares two objects of the class Circle, referred to as C1 and C2 uses the interfaces of C1 and C2 to store the radius of two circles and later to calculate the area of those circles

Review What is a class? What is an object? How do we define the interface in a class? Where can public members be accessed? Private members? What is the difference between a declaration and a definition?