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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
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)
Java Language and SW Dev’t
Introduction to Object-Oriented Programming
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 Procedural and Object-Oriented Programming 13.1.
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
C++ Classes and Data Structures Jeffrey S. Childs
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
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.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
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.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
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?
Review What is an object? What is a class?
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Classes Object-oriented programming: Example: Bank transactions
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?
Introduction to Classes
Chapter 6 Class Definitions and Member Functions
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Classes C++ representation of an object
Chapter 9 Introduction To Classes
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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 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.

2 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.

3 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(); } ; NEVER forget the semicolon The name of the class. We will use it to create BankAccount objects.

4 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(); } ; 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. data member

5 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 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. Usually, data are private and functions are public.

6 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(); } ; 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. default constructor. ALWAYS define one

7 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(); } ; Note the naming convention: class names usually start with an uppercase letter member names usually start with a lowercase letter. various public member functions

8 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

9 Classes Conditional compilation example #ifndef CONSTANT_NAME #define CONSTANT_NAME // code #endif 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). 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.

10 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

11 The bank account class, take 1 #include "bankaccount.h" using std::cerr; using std::endl; BankAccount::BankAccount() { balance = 0; } bankaccount.cpp, continued on next slide use " ", not for user-defined header files. :: 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.

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

13 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; }

14 Using the bank account class #include "bankaccount.h" #include 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 = ; return 0; } Example 1: 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.