Todays Material - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Todays Material

Description:

Class is only a blueprint using which many objects of the same type can be created/instantiated. ... void setWidth(int w); // set square's width to w. int ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 12
Provided by: cengAna
Category:

less

Transcript and Presenter's Notes

Title: Todays Material


1
Todays Material
  • Class
  • The blueprint of objects
  • Implementing classes
  • C
  • Java
  • Object Creation (Instantiation)
  • Implicit instantiation
  • Explicit instantiation

2
Class Blueprint for Objects
  • In OO programming languages, class is the
    blueprint for objects of a particular type
  • class specifies the state (attributes) and
    behavior (public methods) of objects of a
    particular type, e.g., Square, Rectangle,
    Triangle, Circle,
  • class definition does not create an object by
    itself. Class is only a blueprint using which
    many objects of the same type can be
    created/instantiated.

3
Blueprint of Square Objects 2 Square object
instances
width 4
Square
  • -width int

Create Object
getWidth() int setWidth(int) getArea()
int getPerimeter() int draw(char)
square1
Create Object
width 6
Square class UML-like representation
square2
4
Blueprint of Rectangle Objects 2 Rectangle
object instances
width 12
Rectangle
height 8
getWidth
-width int -height int
getHeight
Create Object
setWidth
getWidth() int getHeight() int setWidth(int)
setHeight(int) getArea() int getPerimeter()
int draw(char)
setHeight
getArea
width 20
getPerimeter
height 5
draw
getWidth
Create Object
rect1
getHeight
setWidth
setHeight
Rectangle class UML-like representation
getArea
getPerimeter
draw
rect2
5
Implementing classes
  • In C, the typical practice is to write class
    definition (attributes and method prototypes)
    into a header file, e.g., Square.h, Rectangle.h,
    and method implementations into a source file,
    e.g., Square.cpp, Rectangle.cpp etc.
  • In Java, class definition and method
    implementations are written into a single file,
    e.g., Square.java, Rectangle.java,

6
Square class definition in CltSquare.hgt
class Square private int width //
Width of the square public // public
interface Square objects are controlled //
by the following methods int getWidth()
// get squares width void setWidth(int
w) // set squares width to w int
getArea() // get squares area int
getPerimeter() // get squares perimeter
void draw() // draw square on the
screen
7
Square class implementation in CltSquare.cppgt
// set squares width to w void
SquaresetWidth(int w) width w
//end-setWidth
// get squares width int SquaregetWidth()
return width //end-getWidth
// get squares area int SquaregetArea()
return widthwidth //end-getArea
// get squares perimeter int SquaregetPerimeter
() return 4width //end-getPerimeter
// draw the square on the screen void
Squaredraw(char ch) for (int i0 iltwidth
i) for (int j0 jltwidth j)
putchar(ch) //end-for
putchar(\n) //end-for //end-draw
8
Creating Square object instances
main() Square square1 //
implicit object creation Square square2 new
Square() // explicit object creation
square1.setWidth(4) square2-gtsetWidth(6)
printf(square1 width d\n, square1.getWidth())
printf(Area of square1 d\n,
square1.getArea()) printf(Perimeter of
square1 d\n, square1.getPerimeter())
square1.draw() printf(square2 width
d\n, square2-gtgetWidth()) printf(Area of
square2 d\n, square2-gtgetArea())
printf(Perimeter of square2 d\n,
square2-gtgetPerimeter())
square2-gtdraw(o) delete square2 //
explicit object deletion //end-main square1
is automatically deleted at the end of main
9
Square class definition implementation in Java
ltSquare.javagt
class Square private int width //
Squares width // Public interface below
public int getWidth()return width public
void setWidth(int w)width w public int
getArea()return widthwidth public int
getPerimeter()return 4width public void
draw(char ch) for (int i0 iltwidth i)
for (int j0 jltwidth j)
System.out.print(ch) //end-for
System.out.print(\n) //end-for
//end-draw
10
Main.java
class Main public static void main(String
args) Square square1 new Square() //
explicit object creation Square square2 new
Square() // no before square1 or square2
square1.setWidth(4) square2.setWidth(6)
System.out.println(square1 width
square1.getWidth()) System.out.println(Area
of square1 square1.getArea())
System.out.println(Perimeter of square1
square1.getPerimeter()) square1.draw()
System.out.println(square2 width
square2.getWidth())
System.out.println(Area of square2
square2.getArea()) System.out.println(Perime
ter of square2 square2.getPerimeter())
square2.draw(o) // end-main square1 and
square2 are automatically deleted // by the
garbage collector at the end of main
11
How to compile/run Java programs
javac Square.java --? This will create
Square.class javac Main.java --? This will
create Main.class java Main --? This runs
Main in java virtual machine (emulator) To
compile all Java code simultaneously javac
.java -? This will create Square.class and
Main.class
Write a Comment
User Comments (0)
About PowerShow.com