Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Object Oriented Design and Classes
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
C++ data types. Structs vs. Classes C++ Classes.
1 Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Lecture 9 Concepts of Programming Languages
Object Oriented Design and Classes. What is a class? Programming mechanism to support OOD Data and the methods that operate on that data – collectively.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Programming Languages and Paradigms Object-Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Object-Oriented Programming in C++
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Warm-Up Problem Just like with primitive data types (int, double, etc.), we can create arrays of objects. ex: bankAccount employees[100]; Problem: It’s.
Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
93 4/11/98 CSE 143 Class Constructors [Sections ]
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Chapter 3 Implementing Classes
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Lecture 3 John Woodward.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
About the Presentations
Implementing Classes Yonglei Tao.
Introduction to Classes
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Chapter Three - Implementing Classes
Introduction to Classes
Chapter 6 Class Definitions and Member Functions
JAVA CLASSES.
C++ data types.
Classes.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

Classes

Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook

OOD Goals Robustness –Gracefully handle failures Adaptability –Evolve as necessary Reusability –Many programs use same piece of code

OOD Principles Abstraction –Abstract Data Types (ADTs) –Interfaces Encapsulation –Information Hiding Modularity –Easily plug together components

What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide structure for organizing programs –Almost like an advanced struct

Syntax class classname { private: … public: … } private only accessible within the class public accessible from anywhere (like with a struct)

Member Functions Typically, data (variables) declared private Functions operate on data –accessor functions – read data, provide access to data but do not change it –update functions – change data examples from bank account, zoo??? –constructor – builds a new object

Writing Classes Typically, two files header and source Header declares member variables, provides function prototypes and bodies of short functions Source provides code for rest of functions Allows separation of interface and implementation Reduces compile time

Bank Account Interface BankAccount(double initial_balance); void withdraw(double amount); void deposit(double amount); double checkBalance() const;

Creating and Using Objects BankAccount b(500); //Type Name(constructor parameters); //how would you withdraw funds?

Creating and Using Objects BankAccount b(500); Type Name(constructor parameters); //how would you withdraw funds? b.withdraw(300); object_name.method_name(param list);

Constructor BankAccount::BankAccount(double init_balance) class_name(parameter list) Special-case function called when a new object is created Used to initialize member variables –Examples? Default constructor takes no parameters

Alternative Initialization BankAccount::BankAccount(double init_balance) { balance = init_balance; } BankAccount::BankAccount(double init_balance=0) : balance(init_balance) { }

Destructor BankAccount::~BankAccount() ~class_name Used if class allocates memory dynamically (uses new) Delete all dynamically declared objects

const const member function will not change any class variables double checkBalance() const {…}

friend Allow a class to access the private members of another class Operator overloading Closely related classes

static Static class member - a global variable with scope the same as a class member –1 per class, not per object Example - car serial number

More Examples An appointment book/calendar A tic tac toe game Airline example