CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Structures Spring 2013Programming and Data Structure1.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and Polymorphism.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
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.
True or false A variable of type char can hold the value 301. ( F )
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Classes and Objects Systems Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Understanding class definitions Looking inside classes.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
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.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Object-Oriented Programming in C++
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Classes and Data Structures Jeffrey S. Childs
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
1 CSC241: Object Oriented Programming Lecture No 02.
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.
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?
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration.
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.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
1 C++ Classes and Data Structures Course link…..
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Structs versus 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?
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
Structures Lesson xx In this module, we’ll introduce you to structures.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes and Data Abstraction
Classes.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel

Fall 2010CSCI 383 Lecture 6 M. van Bommel 2 Structures A struct holds data, like an array Each unit of data in a struct is called a data member (or member) they are called “elements” in arrays In a struct, each data member can have a different data type in arrays, the data type of each element is the same

Fall 2010CSCI 383 Lecture 6 M. van Bommel 3 Struct Example 1 #include 2#include 3 4 struct CarType { 5 string make; 6 int year; 7 float price; }; 8 9 void getYourCar(CarType &car) 10 { 11 cout << “Make:”; 12 cin >> car.make; 13 cout << “Year:”; 14 cin >> car.year; 15 cout << “Price:”; 16 cin >> car.price; 17 } 18 int main( ) 19 { 20 CarType myCar, yourCar; myCar.make = "Mercedes"; 23 myCar.year = 2005; 24 myCar.price = ; getYourCar(yourCar); cout << “Your car is a “ 29 << yourCar.make 30 << “\nI’ll offer you “ 31 << yourCar.price – << “ for it.\n”; 33 }

Fall 2010CSCI 383 Lecture 6 M. van Bommel 4 Object Assignment with Structs An object of a struct can be assigned to another object of the same struct type: myCar = yourCar; This assigns each data member in yourCar to the corresponding data member of myCar Also assigns any array data members

Fall 2010CSCI 383 Lecture 6 M. van Bommel 5 Classes A class is similar to a struct A class contains data members, but it also contains function members or member functions Objects are made from classes, similarly to the way that objects are made from structs The main program communicates with the objects Data is passed from main program to object and from object back to the main program

Fall 2010CSCI 383 Lecture 6 M. van Bommel 6 Main Program using Objects Object A Object B Object C Main Program

Fall 2010CSCI 383 Lecture 6 M. van Bommel 7 How Main Program uses Objects The main program does not access the data within a class object The main program only accesses the functions of a class object communication occurs by passing data as parameters into the object’s function the object passes data to the main program through its return type

Fall 2010CSCI 383 Lecture 6 M. van Bommel 8 Main Program calls Function in Object Function returns value to Main Program Main Program and Object Main Program Object public: functions private: data Function accesses private data Main Program calls another Function Function calls another Function Other Function accesses data Other Function returns to first First Function accesses data Function returns to Main Program

Fall 2010CSCI 383 Lecture 6 M. van Bommel 9 Example of a Class – checkbook.h 1 class Checkbook 2 { 3 public: 4 void setBalance( float amount ); 5 void deposit( float amount ); 6 bool writeCheck( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; This class definition is placed into its own file, the class specification file, named checkbook.h The writeCheck function returns false if the amount of the check exceeds the balance; true otherwise. Don’t forget the semicolon.

Fall 2010CSCI 383 Lecture 6 M. van Bommel 10 Example of a class – checkbook.cpp 1 #include “checkbook.h” 2 3 void Checkbook::setBalance( float amount ) 4 { 5 balance = amount; 6 } void Checkbook::deposit( float amount ) 10 { 11 balance += amount; 12 lastDeposit = amount; 13 } The function definitions are placed into a separate file called the class implementation file. This file would be called checkbook.cpp (by convention). Special notation for class function definitions Checkbook:: The balance variable is declared as private in the class definition. balance

Fall 2010CSCI 383 Lecture 6 M. van Bommel 11 More of checkbook.cpp 14 bool Checkbook::writeCheck( float amount ) 15 { 16if ( amount > balance ) return false; 17balance –= amount; 18lastCheck = amount; return true; 19 } float Checkbook::getBalance( ) { 22return balance; 23 } float Checkbook::getLastCheck( ) { 26return lastCheck; 27 } end of checkbook.cpp

Fall 2010CSCI 383 Lecture 6 M. van Bommel 12 Notes on classes The data members of a class cannot be accessed by a main program The object always retains the current values of its data members, even when object code is no longer executing Each function of a class can use the data members of the class as though they have been declared within the function

Fall 2010CSCI 383 Lecture 6 M. van Bommel 13 If direct access to data members … Class int a; int b; int c; Suppose the variables of a class were accessed by 100 places in a program And we need to change the data representation (for maintenance) We need to change 100 lines of code in the main program! Main Program

14 With private data members … Class private: int a; int b; int c; Here, the main program calls foo from 100 places and foo accesses the private data members. int foo( int x ) Main Program If the data needs to change, the body of foo may need to be rewritten. The function call and return type of foo stay the same so no need to make any changes in main program!

Fall 2010CSCI 383 Lecture 6 M. van Bommel 15 With private data members (cont’d) Class private: int a; int b; int c; Program maintenance is much easier this way … int foo( int x ) Main Program especially if there is more than one program using the class.

Fall 2010CSCI 383 Lecture 6 M. van Bommel 16 Structs vs Classes Functions can be placed in a struct, but only when necessary The public and private keywords can be left out of a class (rare) The public and private keywords can be placed into a struct (rare) So what is the difference between a struct and a class? If public and private are not used in a struct, all data members are public by default If public and private are not used in a class, all data members are private by default

Fall 2010CSCI 383 Lecture 6 M. van Bommel 17 Conventions By convention, we use structs when we want all data members to be public structs are typically defined and used within the client’s program, not in a separate file typically used for records of information By convention, we use classes when we want all data members to be private (for maintenance purposes)