Download Java Classes and Objects

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Functional programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

APL syntax and symbols wikipedia , lookup

Operational transformation wikipedia , lookup

Design Patterns wikipedia , lookup

Standard ML wikipedia , lookup

Name mangling wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Corecursion wikipedia , lookup

Class (computer programming) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Object-oriented programming wikipedia , lookup

Function object wikipedia , lookup

C++ wikipedia , lookup

Transcript
Classes and Objects
December 2, 2013
Quiz Wednesday


Over 7.1-7.3, Arrays
In class quiz.
Objectives
 Design classes and objects.
 Write a simple class with member functions
and data members.
 Create a constructor to initialize the
Rectangle class
 Design some other classes
 Create an accessor to see a private data
member
Object Oriented Programming
 So far we have been doing functional
programming.
–
 OOP
–
–
Write functions to instruct the computer to
do something
Create objects with function, then instruct
the objects to do something.
Programming becomes an interaction
between objects.
Object Oriented Programming
 In Functional Programming, creating functions
helps to make code that we can reuse.
 When programs get large it becomes difficult
to manage so many functions.
 In OOP functions are organized into objects.
 Objects
are more intuitive to use.
 For example: A quadratic equation:
•
•
Has data, a, b and c
Has functions: input( ), output( ), solve( )
Objects in Programs
 You already know about the cin object
can do the following functions:
 .fail(
), .clear( )
 There must be data associated with these
functions, but it is hidden from the programmer
to make it easier to use.
Encapsulation
 You use the functions from cin, like clear(),
without knowing the details of the
implementation.
 Easier not to have to understand how the
code looks.
 This hiding is called encapsulation.
 When you create an object from scratch you
should make it easy to use w.o. knowing the
details of the functions.
Classes


Types that objects can be made from.
Give a description of what the objects, or
instances, will be like.
Specify the Public Interface
 How the class interacts with a programmer is
called the interface.
 The
functions you can call.
 Example: I want a class to represent a
rectangle. I want the interface to allow the
programmer to find the area. I also want him
to be able to find the perimeter.
I
will also add some functions to allow the user
to input and output a Rectangle.
•
Member functions
Public Interface Design
Rectangle
public:
input( );
output( );
findArea( );
findPerimeter( );
Data Members
 In order to find the rectangle’s area and
perimeter, the rectangle will need to know its
length and width.
 Outside users don’t need to know the
information. It is private.
Instance Variables Added
 Class Design
Rectangle
public
input( )
output( )
findArea( )
findPerimeter( )
private
double length
double width
Implementing the class.
 Now that I have a design, I can implement the
class.
 The main method tests the class by creating
some Rectangles
 Ch8/Rectangle/rectangle.cpp
Class Definition Syntax
class Sample
{
public:
fnc_prototype();
fnc_prototype2();
//. . . etc.
private:
member_var1;
member_var2;
// … etc.
Member Function Definition
return_type class_name :: function_name(parmlist)
{
statements
}
 :: is scope resolution operator
Calling a function
Object_name.function_name(arg list);
e.g. rec1.input( );
Scope of data members
 Each object created has it's own data
members.
 Can use data members in any of the functions
in the class.
 There is no need to pass them to functions.
Constructor
 Has the same name as the class.
 Is executed automatically when a new object
is made.
 Can be passed some values to initialize the
instance variables.
 I'll add a constructor to the Rectangle class
Another example --Sphere class
Design and implement a class called Sphere that contains
member data that represents the sphere’s diameter.
Define a Sphere constructor to accept and initialize the
diameter. Include functions to return the volume and the
surface area of the Sphere. Write a main function to
create and test a Sphere.
A= 4πr
2
V=
4 3
πr
3
Accessor Function
 A function that allows the main() function to
see a private data member.
 Usually starts with “get”
 Return the private data.
Mutator Function
 A function that allows the main() function or
outside functions to change a private data
member.
 Usually starts with “set”
 void
 I’ll add a setDiameter()to the Sphere
class.
Questions
 What is the difference between a class and an
object?
 getDiameter( ) is a special "get" function.
What is another name for a "getter"?
 A class is called Point. What is the name of
the class' constructor?