CS 403 Class Notes: Object-Oriented Programming and Data Abstraction, Study notes of Programming Languages

A series of class notes from a spring 2002 computer science 403 class. The notes cover topics related to object-oriented programming (oop), including classes, inheritance, polymorphism, data abstraction, and object initialization. The notes also discuss concepts such as constructors, destructors, and deep copy and comparison. The document also mentions java interfaces and their role in oop.

Typology: Study notes

Pre 2010

Uploaded on 02/24/2010

koofers-user-izl
koofers-user-izl 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Spring 2002 CS 403 Class Notes Page 1
Lecture 30 – Tuesday, April 23
Today:
Introduction to Object-Oriented Programming
Reading Assignments For Next Class:
Chapter 10
No class Thursday
Spring 2002 CS 403 Class Notes Page 2
Data Abstraction and Object-Oriented
Programming
Fundamental concepts:
Classes
Inheritance
Polymorphism
Class design issues:
Object initialization
Object finalization
Copying and equality testing
Spring 2002 CS 403 Class Notes Page 3
Classes
Basi c structure:
Private data elements
Public methods
class foo{
private:
int a;
int b;
public:
foo();
foo(int x);
int c();
};
Spring 2002 CS 403 Class Notes Page 4
Clients
void main(void){
foo f;
f.c();
f.a = 3; //illegal
}
f is an object with space for 2 ints on main’s
activation record
Spring 2002 CS 403 Class Notes Page 5
Inheritance
class foo2: public fo o{
public:
int d(); //this method c an reference a and b
}
foo2 inherits the data and method members of foo
Public members of fooare exported as public members of foo2.
This makes the foll owing legal:
foo2 f2;
f2.c();
f2.d();
Spring 2002 CS 403 Class Notes Page 6
Polymorphism
Base class shape:
Virtual m ethod draw (unimplemented)
This mak es shape “abstract”
Not legal to instantia te objects of this class
Derived classes:
Triangle
Rectangle
Each with its own draw, shade and rotate method
Client program:
User cl icks on either “triangle” or “rectangle” and the
appropriate shape is drawn, shaded and rotated
pf3

Partial preview of the text

Download CS 403 Class Notes: Object-Oriented Programming and Data Abstraction and more Study notes Programming Languages in PDF only on Docsity!

Spring 2002 CS 403 Class Notes Page 1

Lecture 30 – Tuesday, April 23

  • Today:
    • Introduction to Object-Oriented Programming
  • Reading Assignments For Next Class:
    • Chapter 10
  • No class Thursday

Spring 2002 CS 403 Class Notes (^) Page 2

Data Abstraction and Object-Oriented

Programming

  • Fundamental concepts:
    • Classes
    • Inheritance
    • Polymorphism
  • Class design issues:
    • Object initialization
    • Object finalization
    • Copying and equality testing

Spring 2002 CS 403 Class Notes (^) Page 3

Classes

  • Basic structure:
    • Private data elements
    • Public methods class foo{ private: int a; int b; public: foo(); foo(int x); int c(); };

Spring 2002 CS 403 Class Notes Page 4

Clients

void main(void){ foo f; f.c(); f.a = 3; //illegal }

  • f is an object with space for 2 ints on main’s activation record

Spring 2002 CS 403 Class Notes (^) Page 5

Inheritance

class foo2: public foo{ public: int d(); //this method can reference a and b }

  • foo2 inherits the data and method members of foo
  • Public members of foo are exported as public members of foo2.
  • This makes the following legal: foo2 f2; f2.c(); f2.d();

Spring 2002 CS 403 Class Notes (^) Page 6

Polymorphism

  • Base class shape:
    • Virtual method draw (unimplemented)
    • This makes shape “abstract”
      • Not legal to instantiate objects of this class
  • Derived classes:
    • Triangle
    • Rectangle
    • Each with its own draw, shade and rotate method
  • Client program:
    • User clicks on either “triangle” or “rectangle” and the appropriate shape is drawn, shaded and rotated

Spring 2002 CS 403 Class Notes Page 7

Without Polymorphism

triangle *t = new triangle; rectangle *r = new rectangle; if user_click = “rectangle”{ r->draw(); r->shade(); r->rotate();} else{ t->draw(); t->shade(); t->rotate();}

Spring 2002 CS 403 Class Notes (^) Page 8

With Polymorphism

shape *s; rectangle *r = new rectangle; triangle *t = new triangle; if user_click = “rectangle” s = r; else s = t; s->draw(); s->shade(); s->rotate();

Spring 2002 CS 403 Class Notes (^) Page 9

Java Interfaces

interface foo{

void blah(int a, int b);

void ugh(int c);

  • foo is abstract (can’t instantiate an interface)
  • Not allowed to implement any methods
  • Class can implement many interfaces
  • Unrelated classes can implement the same

interface.

Spring 2002 CS 403 Class Notes Page 10

Classes

  • Must manage initialization and finalization of

objects:

  • Initialization: Constructor (C++)
  • Finalization: Destructor (C++)
  • Constructors in C++:
  • Invoked at declaration time
  • Invoked when an object is allocated with a new operation

Spring 2002 CS 403 Class Notes (^) Page 11

Constructors

  • Can occur with or without arguments:
    • foo();
    • foo(int x);
  • In a client:
    • foo f;
    • foo f(10);
    • foo *f = new foo;
    • foo *f = new foo(10);

Spring 2002 CS 403 Class Notes (^) Page 12

Object Initialization in Java

  • Involves both initializers and constructors class foo{ private int a = 10; //initializer private int b = 20; //initializer private int *c; foo(){c = new int; *c = 3;} //constructor };
  • foo f = new foo(); //all objects are heap objects
  • This invokes initializers and constructor (in this order)