Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World  Defining a Class – Data and functions Our first C++

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lec 12 Writing an Instantiable Class. Agenda Review objects and classes Making our own class to create objects – Our first "Instantiable" class – with.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Programming with Objects Creating Cooperating Objects Week 6.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
CSC 212 Object-Oriented Programming and Java Part 1.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Chapter 8: User-Defined Classes and ADTs
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Coming up: Inheritance
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
1 C++ Classes & Object Oriented Programming Overview & Terminology.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
3 Introduction to Classes and Objects.
Classes and OOP.
User-Defined Classes and ADTs
Lecture 11 C Parameters Richard Gesick.
Chapter 8: User-Defined Classes and ADTs
Classes, Objects, Methods and Strings
By Rajanikanth B OOP Concepts By Rajanikanth B
Object Oriented Programming in java
CSC 111 Exam 3 Review Fall 2005.
Presentation transcript:

Intro to Classes Chapter 18 AND 19

Agenda Classes – getting the Real World onto the Virtual World  Defining a Class – Data and functions Our first C++ Class Why do we need Classes and Objects? Summary

The Real World How do you look at things in the real world ? Objects Look at a car Wheels Chassis Steering Doors Color Model

The Car as an object Define it 4 Wheels Metal Chassis Can move left, right, forward and back 2 Doors Bright Red Color BMW Z3

The Virtual World Why make any difference in the Virtual World ? With C++ Classes and Objects this can be a reality Solve problems as you visualize them

Even Simpler: How about representing a balloon? Define it: radius  we’ll just use this shape color can inflate it can pop it radius

Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and Functions  Our first C++ Class Why do we need Classes and Objects? Summary

How would you define it? Balloon initialize inflate pop display radius

Data and Functions Associated Class Name Balloon initialize inflate pop display radius

Data and Functions Associated Functions Balloon initialize inflate pop display radius

Data and Functions Associated Attributes (the data) Balloon initialize inflate pop display radius A class is a schematic for a hypothetical balloon

An Actual Balloon has attributes Attribute (the size of the balloon) Balloon initialize inflate pop display radius = 5 We can use the radius to indicate whether balloon has been popped (by setting it to -1)

Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++ Class  Why do we need Classes and Objects? Summary

Modeling a Balloon in C++ class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); void display(); private: int radius; }; Member Data Member Functions

Modeling a Balloon in C++ class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); void display(); private: int radius; }; Users of Balloon can’t access Users of Balloon can access

The C++ Class class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); void display(); private: int radius; }; A class defines the data and the functions that operate on the data A class is like defining your own data type, with associated functions which can act on objects of your type.

Using a class -- objects When you declare a variable of your new type, it’s called an object Balloon hotAir; Balloon bal, weather; hotAir is an object Balloon is a class More objects

You can use the public functions defined for the class Balloon bal; bal.initialize(5); bal.inflate(15); bal.pop();

Classes have Access Control Unlike struct, you can’t access the data directly (it’s private) You have to use the functions already defined in the class Balloon hotAir; hotAir.radius=10; ILLEGAL Balloon hotAir; hotAir.initialize(10); LEGAL

Why the extra restrictions? For many objects it’s too dangerous to allow ignorant (or malicious) users the ability to modify the data in an un-authorized manner Like encasing a complicated device (your iPod) in a protective package—opening package voids the warranty You can only play, download, select song (functions) We put “walls” around the object so it acts more thing-like…that’s why the keyword private

Implement the initialize and inflate functions void Balloon::initialize(int initRad) { radius = initRad; } void Balloon::inflate(int howMuch) { radius = radius + howMuch; } This says it is a member function of Class Balloon Notice how the parameter modifies the member data

Implement the pop and display functions void Balloon::pop() { cout<<"Pop!“<<endl; radius = -1; } void Balloon::display() { cout<<"("<<radius<<")"<<endl; } A “sentinel” value Meaning it’s popped

A ‘client’ program is one that uses a class int main() { Balloon myBalloon; myBalloon.initialize(3); cout<<"myBalloon currently has Radius "; myBalloon.display(); cout<<"\nInflating myBalloon by 8 \n"; myBalloon.inflate(8); cout<<"Now myBalloon has Radius "; myBalloon.display(); cout<<"\nPopping myBalloon \n"; myBalloon.pop(); cout<<"myBalloon currently has Radius "; myBalloon.display(); }

Results when executing previous program: myBalloon currently has Radius (3) Inflating myBalloon by 8 Now myBalloon has Radius (11) Popping myBalloon Pop! myBalloon currently has Radius (-1)

Improvements to Balloon functions We can model the balloon better: If the balloon is already popped, you can’t inflate it If you inflate to a radius over 25, balloon pops void Balloon::inflate(int howMuch) { if (radius >= 0) radius = radius + howMuch; if (radius > 25) pop(); } Invokes a different member function

Voila – You have your first class ! Remember – the definition is called a class An instance of a class is called an object Example: int y; Here int is the type– is analogous to a class y is the instance of the type and is analogous to an object

Classes and Objects Data type (int) xyz int x, y, z;

Classes and Objects The Class (Balloon) balhotAirweather Balloon bal, hotAir, weather; Each object can have its own attributes

Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++ Class Why do we need Classes and Objects?  Summary

Why Classes and Objects ? It may seem overwhelming or unnecessary at first As the complexity/size of your code increases, classes will help you modularize your code Helps you visualize and solve problems better Brings more order into your code

Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++ Class Why do we need Classes and Objects? Summary

The full Balloon class definition class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); void display(); void input(); //reads data like (10) from keybd float volume(); // returns volume of balloon private: int radius; };

You will also work with a (buggy) Time class class Time { public: // for public or client use void set(int h, int m, char mrd); void advance(int h, int m); void display(); private: // for internal use int hr, min; // the hour and minute char merid; // meridian: a(m) or p(m) void validate(); };

And complete the implementation of an Accounts class (don’t use on Project 2) class Account { public: // for public or client use void initialize(string newName, int newID, int newPIN, float newBalance ); void deposit(float money); void withdraw(int money); void display(); private: // for internal use string name; int userID, PIN; float balance; };

Don’t you feel a bit more ‘Class’y ? Classes are fundamental to the Java Programming language and further programming in C++