Download presentation
Presentation is loading. Please wait.
1
Introduction to Object Oriented Programming CIS 230 01-03-06
2
Introduction What Makes an object oriented programming language? 1.Objects include both data and instruction 2.Objects inherit behavior from existing classes 3.Objects determine at runtime how to resond to messages
3
General Types of OOP languages 1.Hybrids C++, Objective C, Object Pascal Provide compatibility with older languages 2.Pure OOP languages Smalltalk, Actor, Java Programmer is forced to create object- oriented code.
4
Why OOP? User Interface –2/3 of an applications code –(windowing, pull down menus, graphics, etc.) –need to be able to write this easily
5
Misconceptions 1.Makes everything easy 2.You can reuse everything
6
Learning curves 1.Must learn the tools provided to create object-oriented programs 2.Must learn to work in an object-oriented programming style
7
Encapsulation The process of combining both properties (data) and behaviors (functions) into one entity. Examples: Integers – Circle – Check – digits +, -, *, / radius, circumference, area how to calc circumference, how to calc area, … amount, check number, date, comment write, sign, cash, record
8
Class A definition of an object (or for a group of similar objects) A template for creating objects Note: Each object belongs to only one class
9
C++ partial examples name might contain: circle might have: what it is how to get to it data – radius how to get the radius calculate circumference calculate area
10
C++ form class ClassName { private: data public: functions }; This defines a class, nothing exists yet prototypes: to keep the definition compact
11
Circle class Circle { private: float radius; public: void store_radius(float); float calc_circum(void); float calc_area(void); float return_radius(void); };
12
Class Functions void Circle::store_radius(float value) { radius = value; } Class Nametwo colons
13
Class Functions float Circle::calc_circum(void) { float circum; circum = 3.14 * 2 * radius; return circum; } float Circle::calc_area(void) { return (3.14 * radius * radius); } Local variable Local variable wasn’t really needed
14
Instance –an actual variable of the class void main() { Circle circle1, circle2; float x, y, z, w; … } Each has its own data (i.e. radius) but they share the functions (methods)
15
Invoking methods instance.method( ); circle1.store_radius(6); cout << “Please enter a circle’s radius ”; cin >> x; circle2.store_radius(x); y = circle1.calc_circum(); z = circle1.calc_area(); cout << “ A 6 inch circle has a circumference \n”; cout << “ of “ << y << “ and an area of “ << z << “\n”;
16
Invoking methods cout << “A “ << x << “ inch circle has a circumference \n”; cout << “of “ << circle2.calc_circum(); cout << “and an area of “ << circle2.calc_area() << “\n”; cout << “The sum of the two areas is “; w = circle1.calc_area() + circle2.calc_area(); cout << w << ‘\n’;
17
Message A request to an object Must have at least two parts: 1.an instance name 2.the name of a method Example: first_circle.assign_radius(7); first_circle.calc_area(); they may require more instancemethod
18
OOP Review Class – A definition (template) of an object –Contains: data & methods Encapsulation –Data – private –Methods – public Instance (object) – An actual variable of the class –Each instance has its own data but jointly use the methods
19
OOP Review Data Abstraction – The ability to manipulate the data without knowledge of the data’s internal format –Use methods to assign/retrieve values –Example: circle1.store_radius(6); y = circle1.calc_circum(); z = circle1.calc_area(); Assign Retrieve
20
Protecting the data Circle circle1, circle2; … cin >> x; circle.store_radius(x); y = circle2.calc_circum(); what if x was -3.4?
21
Protecting the data A new store_radius() method: Circle::store_radius(float value) { if (value >= 0) radius = value; else radius = -1 * value; }
22
Constructor Automatically invoked when a new object is created: –when: –or:
23
Constructor Default Constructor Help establish the link between the specific object and its class’ methods Can initialize data
24
Constructors Same name as the class Can NOT return anything (not even void) class Circle { private: float radius; public Circle(); … }; Circle::Circle() { radius = 1; //Default - unit circle }
25
Destructor Automatically invoked when an object goes out of existence –when: –or:
26
Destructor Default Destructor May cleanup any possible side effects ~ClassName: ~Circle()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.