1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.

Slides:



Advertisements
Similar presentations
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Advertisements

1 Classes with Pointer Data Members (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 Array, Pointer and Reference ( IV ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Constructors and Destructors Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 Dynamic Memory Allocation Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
Introduction to Classes and Data Abstraction
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Object-Oriented Programming Using C++ CLASS 27.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 C++ Classes (I) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3: Classes May 24,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4: Classes Jan 27,
1 Lecture 6 Classes and Data Abstraction: Part II.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
11 Introduction to Object Oriented Programming (Continued) Cats.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
1 C++ Classes and Data Structures Course link…..
Chapter 16: Classes and Data Abstraction
Pointer to an Object Can define a pointer to an object:
Chapter 16: Classes and Data Abstraction
Classes.
Lecture 5: Classes September 14, 2004
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Object-oriented programming (OOP)
Chapter 6: Classes and Data Abstraction
Chapter 19 - Inheritance Outline 19.1 Introduction
Chapter 15 - C++ As A "Better C"
CISC181 Introduction to Computer Science Dr
Chapter 6: Classes and Data Abstraction
CS 144 Advanced C++ Programming February 21 Class Meeting
Presentation transcript:

1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series

2 Thinking in C Buy_Materials() Hire_Workers() Build_Floor() Build_Bedrooms() Build_Kitchen() Build_Roof() Assembly() $$$

3 Thinking in C++ $$$ HomeDepot Material Worker Floor Bedroom Kitchen Roof House

4 CVariable class CVariable { doublem_dValue; char*m_sName; public: // constructors and destructors CVariable(); CVariable(const char*name, const double& v = 0.0); ~CVariable(); CVariable(const CVariable& var); // copy constructor const CVariable& operator=(const CVariable& var); // overload = // getting and setting double Value() { return m_dValue; }; char* Name() const { return m_sName; }; void SetValue(const double& v) { m_dValue = v; }; bool SetName(const char* name); };

5 A good point of C++: “package” Data encapsulation –Class = Data + Functions Data Functions Class access Is it good to access all the members from outside?

6 What to learn today? Access to data members Access to member functions Header files

7 Syntax for accessing Rules of thumb –(I) we do not differentiate data members and member functions. (since both are members!) –(II) if the client is an object, use “.” –(III) if the client is a pointer of a object, use “  ”

8 void main() { CVariable a; a.SetName(“var_1”); a.SetValue(1.5); CVariable *b; b = &a; b->SetValue(3.3); (*b).SetName(“vvv”); CVariable &c; c = a; c.SetValue(0.0); cout << a.Name() << a.Value() << endl; cout Name() Value() << endl; }

9 Question? (1)Is it good? (2)Why do I need to manipulate the data directly? No, I don’t want my class to be too transparent to other users! I don’t want to give users too much rights to manipulate the data in my package! void main() { CVariable a; a.m_dValue = 1.0; char name[] = “var_1”; a.m_sName = name; }

10 Another good point of C++ Information Hiding –Clients can only access some specific members in a class Data Functions access Data Functions Class

11 Controlling Access to Members public –Presents clients with a view of the services the class provides (interface) –Data and member functions are accessible private –Default access mode –Data only accessible to member functions and friend s –private members only accessible through the public class interface using public member functions

12 Access Functions and Utility Functions Utility functions –private functions that support the operation of public functions –Not intended to be used directly by clients Access functions –public functions that read/display data or check conditions –Allow public functions to check private data –“set” functions –“get” functions –Predicate functions

13 // salesp.h // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public: SalesPerson(); // constructor void getSalesFromUser(); // get sales figures from keyboard void setSales( int, double ); // User supplies one month's // sales figures. void printAnnualSales(); private: double totalAnnualSales(); // utility function double sales[ 12 ]; // 12 monthly sales figures }; #endif

14 // salesp.cpp // Member functions for class SalesPerson #include using namespace std; #include "salesp.h" // Constructor function initializes array SalesPerson::SalesPerson() { for ( int i = 0; i < 12; i++ ) sales[ i ] = 0.0; } // Function to get 12 sales figures from the user // at the keyboard void SalesPerson::getSalesFromUser() { double salesFigure; for ( int i = 1; i <= 12; i++ ) { cout << "Enter sales amount for month " << i << ": "; cin >> salesFigure; setSales( i, salesFigure ); }

15 // Function to set one of the 12 monthly sales figures. // Note that the month value must be from 0 to 11. void SalesPerson::setSales( int month, double amount ) { if ( month >= 1 && month 0 ) sales[ month - 1 ] = amount; // adjust for subscripts 0-11 else cout << "Invalid month or sales figure" << endl; } // Print the total annual sales void SalesPerson::printAnnualSales() { cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << "\nThe total annual sales are: $" << totalAnnualSales() << endl; } // Private utility function to total annual sales double SalesPerson::totalAnnualSales() { double total = 0.0; for ( int i = 0; i < 12; i++ ) total += sales[ i ]; return total; }

16 87// Fig. 6.7: fig06_07.cpp 88// Demonstrating a utility function 89// Compile with salesp.cpp 90#include "salesp.h" 91 92int main() 93{ 94 SalesPerson s; // create SalesPerson object s s.getSalesFromUser(); // note simple sequential code 97 s.printAnnualSales(); // no control structures in main 98 return 0; 99} OUTPUT Enter sales amount for month 1: Enter sales amount for month 2: Enter sales amount for month 3: Enter sales amount for month 4: Enter sales amount for month 5: Enter sales amount for month 6: Enter sales amount for month 7: Enter sales amount for month 8: Enter sales amount for month 9: Enter sales amount for month 10: Enter sales amount for month 11: Enter sales amount for month 12: The total annual sales are: $ Create object s, an instance of class SalesPerson 1. Load header file and compile with the file that contains the function definitions 2. Create an object 2.1 Use the object’s member functions to get and print sales Program Output Use access functions to gather and print data ( getSalesFromUser and printAnnualSales ). Utility functions actually calculate the total sales, but the user is not aware of these function calls. Notice how simple main() is – there are no control structures, only function calls. This hides the implementation of the program.

17 Separating Interface from Implementation Separating interface from implementation –Makes it easier to modify programs –Header files Contains class definitions and function prototypes Using #ifndef #define … #endif –Source-code files Contains member function definitions

18 // variable.h Header file #ifndef _VARIABLE_H_ #define _VARIABLE_H_ class CVariable { doublem_dValue; char*m_sName; public: // constructors and destructors CVariable(); CVariable(const char*name, const double& v = 0.0); ~CVariable(); CVariable(const CVariable& var); // copy constructor const CVariable& operator=(const CVariable& var); // overload = // getting and setting double Value() { return m_dValue; }; char* Name() const { return m_sName; }; void SetValue(const double& v) { m_dValue = v; }; bool SetName(const char* name); }; #endif

19 // variable.cpp source file #include “variable.h” CVariable::CVariable() : m_dValue(0.0), m_sName(NULL) { // empty } CVariable::CVariable(const char* name, const double& v) { m_dValue = v; m_sName = new char[strlen(name)+1]; strcpy(m_sName, name); } CVariable::CVariable(const CVariable& var) { m_dValue = var.m_dValue; m_sName = new char[strlen(var.m_sName)+1]; strcpy(m_sName, var.m_sName); } CVariable::~CVariable() { if(m_sName!=NULL){ delete [] m_sName; }

20 const CVariable& CVariable::operator=(const CVariable& var) { if(&var != this){ // check for self-assignment m_dValue = var.m_dValue; SetName(var.m_sName); } return *this; } bool CVariable::SetName(const char* name) { boolcode = true; if(m_sName!=NULL) delete [] m_sName; m_sName = new char [strlen(name) + 1]; if(m_sName){ strcpy(m_sName, name); } else code = false; return code; }