Classes Object-oriented programming: Example: Bank transactions

Slides:



Advertisements
Similar presentations
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Advertisements

1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Classes Separating interface from implementation
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.
Programming Languages and Paradigms Object-Oriented Programming.
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.
Writing Classes (Chapter 4)
Object-Oriented Programming in C++
Classes CS 21a: Introduction to Computing I First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
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.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CS 106X Classes and Objects
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
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 , ]
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Programming Abstractions
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Abstract Data Types Programmer-created data types that specify
Review What is an object? What is a class?
Andy Wang Object Oriented Programming in C++ COP 3330
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Object-Oriented Programming: Classes and Objects
Lecture 4-7 Classes and Objects
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Introduction to Classes
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 6 Class Definitions and Member Functions
Chapter 9 Objects and Classes
Code Organization Classes
Structures putting data together.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
C++ Compilation Model C++ is a compiled language
JAVA CLASSES.
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Classes C++ representation of an object
Chapter 9 Introduction To Classes
Constructors and Deconstructor
Code Organization Classes
Lecture 8 Object Oriented Programming (OOP)
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Classes Object-oriented programming: Example: Bank transactions Model the problem as a collection of objects that have certain attributes and interact with one another and/or the world through specific operations. Example: Bank transactions object: bank account attribute: balance operations: initialize, deposit, withdraw. We need to design a blueprint to describe the bank account.

Classes Class = a blueprint for an object. It describes the object's attributes (member data) and any operations that are allowed on the object (member functions) Object = an instance of a class e.g. a specific bank account. The attributes will now have specific values.

The bank account class, take 1 The name of the class. We will use it to create BankAccount objects. class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; NEVER forget the semicolon

The bank account class, take 1 class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; data member private and public are member access specifiers. Data members or member functions that are declared private cannot be directly accessed outside of the class. Instead, they can only be accessed by member functions. In this example, it is necessary to make the balance private since then it can only be modified in a controlled way through the withdraw and deposit functions.

The bank account class, take 1 class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; Usually, data are private and functions are public. Data members or member functions that are declared public can be accessed directly from outside the class. Privacy allows us to separate the interface from the implementation. public members : the interface private members : part of the implementation This allows us to change the implementation without changing the interface.

The bank account class, take 1 class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; default constructor. ALWAYS define one When a class object is created, its members are initialized by a special function called a "constructor". The default constructor specifies default initial values, so that the object is always guaranteed to be initialized to a consistent state. If the programmer does not define a default constructor, the compiler creates one and calls it automatically every time an instance of the class is created, but this does not guarantee correct initialization of member data.

The bank account class, take 1 class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; various public member functions Note the naming convention: class names usually start with an uppercase letter member names usually start with a lowercase letter.

Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses the class. This means that several files of the same project may all #include the header file. However, the compiler should only process it once. Conditional compilation allows code to be included or omitted based on certain conditions. We can use the following directives for conditional compilation: #ifdef, #ifndef, #else, #endif, #define

Classes Conditional compilation example In english: if CONSTANT_NAME has not been defined, define it compile code If CONSTANT_NAME has been defined, skip everything up to #endif and compile whatever follows (if anything follows). #ifndef CONSTANT_NAME #define CONSTANT_NAME // code #endif How does this help us? Initially, CONSTANT_NAME will not be defined, so the code will be compiled. After that, whenever this file is #included in another one, the constant will have been defined so the code will not be recompiled.

The bank account class, take 1 #ifndef BANKACCOUNT_H #define BANKACCOUNT_H class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; #endif bankaccount.h

The bank account class, take 1 #include "bankaccount.h" using std::cerr; using std::endl; BankAccount::BankAccount() { balance = 0; } use " ", not < > for user-defined header files. bankaccount.cpp, continued on next slide :: is the scope resolution operator. If it's missing, your compiler won't know that this is the implementation of the constructor for the BankAccount class.

The bank account class, take 1 BankAccount::BankAccount() { balance = 0; } int BankAccount::getBalance () { return balance; bankaccount.cpp, continued on next slide

The bank account class, take 1 void BankAccount::deposit (int amount) { balance += amount; } void BankAccount::withdraw (int amount) { if (balance < amount) cerr << "Insufficient funds\n"; else balance –= amount;

Using the bank account class Example 1: #include "bankaccount.h" #include<iostream> using std::cout; using std::endl; int main () { BankAccount myaccount; myaccount.deposit(1000); myaccount.withdraw(333.33); cout << "I have " << myaccount.getBalance() << " dollars" << endl; myaccount.balance = 10000000; return 0; } Create a BankAccount object. The default constructor is executed automatically. Use the dot operator to access the member functions This line will give a compilation ERROR because balance is private.