1 CSE 2341 Object Oriented Programming with C++ Note Set #7.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 13 – Introduction to Classes
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Lesson 14 Classes, Continued CS1 Lesson More Classes1.
EC-241 Object-Oriented Programming
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Chapter 14 More About Classes. CS SJAllan Chapter 14 2 Instance and Static Members Instance variable:  A member variable in a class  Each object.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
Starting Out with C++, 3 rd Edition 1 Chapter 14: more about classes Static Members If a member variable is declared static, all objects of that class.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
More About Classes. C++: Classes & Objects -2 2 Instance and Static Members Each instance of a class has its own copies of the class’s instance (member)
Definition Class In C++, the class is the construct primarily used to create objects.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Chapter 11: More About Classes and Object-Oriented Programming
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
Starting Out with C++ 2nd Edition, by Tony Gaddis 1 Chapter 7 – Classes and Structured Data.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Some odds and ends about Classes and Objects. 6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
CS1201: Programming Language 2 Classes and objects.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Introduction to Object Oriented Programming Chapter 10.
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
1 C++ Classes and Data Structures Course link…..
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
A Second Look at Classes and Objects - 1 Static Class Members
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
#define #include<iostream> using namespace std; #define GO
Introduction to Programming
A first Look at Classes.
Classes.
Introduction to Classes
Chapter 5 Classes.
Output Stream Formatting
Introduction to Classes
Static in Classes CSCE 121 J. Michael Moore.
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Chapter 14 – More About Classes
Lecture 8 Object Oriented Programming (OOP)
Chapter 6: A First Look at classes
Presentation transcript:

1 CSE 2341 Object Oriented Programming with C++ Note Set #7

2 Quick Look Static vs. Local Variables Friendship Memberwise Assignment to copy two objects

3 Static Member If a member is declared static –it is shared by all objects of the same class –it must be defined outside of the class –may be accessed before any instances are created

4 Example – with pictures class myCls { private: int x, y; public: myCls(){x = 5; y = 6;} }; int main() { myCls a; myCls b; return 0; } a b x y x y

5 Example – with pictures class myCls { private: int x, y; static int p; public: myCls(){x = 5; y = 6;} }; int myCls::p = 10; int main() { myCls a; myCls b; return 0; } a b x y x y p 10

6 Example Class With Static Member #ifndef BUDGET_H #define BUDGET_H // Budget class declaration class Budget { private: static float CorpBudget; float DivBudget; public: Budget(void) { DivBudget = 0; } void AddBudget(float B) { DivBudget += B; CorpBudget += B; } budget.h

7 Example Class With Static Member float GetDivBudget(void) { return DivBudget; } float GetCorpBudget(void) { return CorpBudget; } }; //definition of static data member double Budget::CorpBudget = 0; #endif budget.h

8 Example Class With Static Member #include #include using namespace std; #include "budget.h" // For Budget class int main() { Budget Divisions[4]; for (int Count = 0; Count < 4; Count++) { float Bud; cout << "Enter the budget request for “ << “division "; cout << (Count + 1) << ": "; cin >> Bud; Divisions[Count].AddBudget(Bud); } budgetDriver.cpp

9 Example Class With Static Member cout.precision(2); cout.setf(ios::showpoint | ios::fixed); cout << "\nHere are the division budget “ << “requests:\n"; for (Count = 0; Count < 4; Count++) { cout<<"\tDivision "<<(Count + 1)<<"\t$ "; cout << Divisions[Count].GetDivBudget(); cout << endl; } cout << "\tTotal Budget Requests:\t$ "; cout << Divisions[0].GetCorpBudget() << endl; return 0; } budgetDriver.cpp

10 Example Class With Static Member Enter the budget request for Division 1: Enter the budget request for Division 2: Enter the budget request for Division 3: Enter the budget request for Division 4: Here are the division budget requests: Division 1$ Division 2$ Division 3$ Division 4$ Total Budget Requests:$

11 Static Member Functions callable before any objects are instantiated static member functions can access static data members before any objects are instantiated provides ability for specialized set-up routines in an object. static ( );

12 Example Class With Static Member #ifndef BUDGET_H #define BUDGET_H // Budget class declaration class Budget { private: static float CorpBudget; float DivBudget; public: Budget() { DivBudget = 0; } void AddBudget(float B) { DivBudget += B; CorpBudget += B; } budget2.h

13 Example Class With Static Member float GetDivBudget(void) { return DivBudget; } float GetCorpBudget(void) { return CorpBudget; } static void mainOffice(float); }; #endif budget2.h

14 Example Class With Static Member #include “budget2.h" // Definition of static member of Budget class float Budget::corpBudget = 0; //Definition of static member function MainOffice. //This function adds the main office's budget // request to the CorpBudget variable. void Budget::mainOffice(float moffice) { corpBudget += moffice; } budget2.cpp

15 Example Class With Static Member //Demonstrates static member function #include #include "budget2.h"//For Budget class declaration using namespace std; int main() { int count; float mainOfficeRequest; const int numDivisions = 4; cout << "Enter the main office's budget “ << “request: "; cin >> mainOfficeRequest; Budget Divisions[numDivisions]; Budget::mainOffice(mainOfficeRequest); budgetDriver2.cpp

16 Example Class With Static Member for (int Count = 0; Count < 4; Count++) { float budgetAmount; cout << "Enter the budget request for division "; cout << (count + 1) << ": "; cin >> budgetAmount; divisions[Count].addBudget(budgetAmount); } cout << setprecision(2); cout << showpoint << fixed; cout << "\nHere are the division budget requests:\n"; for (count = 0; count < 4; count++) { cout << "\tDivision " << (count + 1) << "\t$ "; cout << divisions[count].getDivisionBudget() << endl; } budgetDriver2.cpp

17 Example Class With Static Member cout << "\tTotal Requests (including main office):” << “ $ "; cout << divisions[0].getCorpBudget() << endl; return 0; } budgetDriver2.cpp Enter the main office's budget request: Enter the budget request for Division 1: Enter the budget request for Division 2: Enter the budget request for Division 3: Enter the budget request for Division 4: Here are the division budget requests: Division 1$ Division 2$ Division 3$ Division 4$ Total Requests (including main office): $

18 Friends of Classes

19 Friend Function –Not a member function of the class –has access to the private data members of the class friend ( );

20 Friendship #ifndef AUXIL_H #define AUXIL_H class Budget;// Forward declaration of Budget class class AuxiliaryOffice { private: float auxBudget; public: AuxiliaryOffice() { auxBudget = 0; } void addBudget(float, Budget&); float getDivisionBudget() { return auxBudget; } }; #endif auxil.h

21 Friendship #include "auxil.h" #include "budget3.h" void AuxiliaryOffice::addBudget(float b, Budget& div) { auxBudget += b; div.corpBudget += b; } auxil.cpp

22 Example with Friendship #ifndef BUDGET3_H #define BUDGET3_H #include "auxil.h”//For Aux class declaration class Budget { private: static float corpBudget; float divisionBudget; public: Budget() { divisionBudget = 0; } void addBudget(float b) { divisionBudget += b; corpBudget += b; } budget3.h

23 Example with Friendship float getDivisionBudget(void) { return divisionBudget; } float getCorpBudget(void) { return corpBudget; } static void mainOffice(float); friend void AuxiliaryOffice::addBudget (float, Budget&); }; #endif budget3.h

24 Example with Friendship #include “budget3.h" // Definition of static member of Budget class float Budget::corpBudget = 0; //Definition of static member function MainOffice. //This function adds the main office's budget //request to the CorpBudget variable. void Budget::mainOffice(float moffice) { corpBudget += moffice; } budget3.cpp

25 Example with Friendship #include #include "budget3.h" using namespace std; int main(int) { int count; float mainOfficeRequest; const int numDivisions = 4; cout << "Enter the main office's ” << budget request: "; cin >> mainOfficeRequest; Budget::mainOffice(mainOfficeRequest); Budget divisions[numDivisions]; AuxiliaryOffice auxOffices[numDivisions]; budgetDriver3.cpp

26 Example with Friendship for (count = 0; count < numDivisions; count++) { float budgetAmount; cout << "Enter the budget request for “ << “division "; cout << (count + 1) << ": "; cin >> budgetAmount; divisions[count].addBudget(budgetAmount); cout << "Enter the budget request for “ << “division "; cout << (count+1) << "'s\nauxiliary office: "; cin >> budgetAmount; auxOffices[count].addBudget(budgetAmount, divisions[Count]); } budgetDriver3.cpp

27 Example with Friendship cout << setprecision(2); cout << showpoint << fixed; cout << "Here are the division budget requests:\n"; for (count = 0; count < 4; count++) { cout << "\tDivision " << (count + 1) << "\t\t\t$ "; cout << setw(7); cout << divisions[count].getDivisionBudget() << endl; cout << "\tAuxiliary Office of Division " <<(count+1) << "\t$ "; cout << auxOffices[count].getDivisionBudget() << endl; } cout <<"Total Requests (including main office): $ "; cout << divisions[0].getCorpBudget() << endl; return 0; } budgetDriver3.cpp

28 Example with Friendship Enter the main office's budget request: Enter the budget request for Division 1: Enter the budget request for Division 1's auxiliary office: Enter the budget request for Division 2: Enter the budget request for Division 2's auxiliary office: Enter the budget request for Division 3: Enter the budget request for Division 3's auxiliary office: Enter the budget request for Division 4: Enter the budget request for Division 4's auxiliary office: 65000

29 Example with Friendship Here are the division budget requests: Division 1:$ Auxiliary office of Division 1:$ Division 2:$ Auxiliary office of Division 2:$ Division 3:$ Auxiliary office of Division 3:$ Division 4:$ Auxiliary office of Division 4:$ Total Requests (including main office): $

30 Classes as Friends Possible to make entire classes friends of another class Budget could make Auxiliary a friend with: friend class AuxiliaryOffice; All member functions of AuxiliaryOffice would have access to all private data members of Budget (even those added in later development)

31 Memberwise Assignment

32 Memberwise Assignment = operator –can be used to assign one objects instance to another object instance –can be used to initialize one object with another objects data –default: each member from one instance is copied to its counterpart in the other instance

33 Memberwise Assignment #include class Rectangle { private: float width; float length; public: void setWidth(float w) { width = w; } void setLength (float l){ length = l; } float getWidth() { return width; } float getLength() { return length; } float GetArea() { return width * length; } };

34 Memberwise Assignment int main() { Rectangle Box1, Box2; Box1.setWidth(10); Box1.setLength(20); cout << "Before the assignment:\n"; cout << "Box 1's Width: " << Box1.getWidth()<< endl; cout << "Box 1's Length: " << Box1.getLength() << endl; cout << "Box 1's Area: " << Box1.getArea() << endl; Box2 = Box1 ; cout << " \n"; cout << "After the assignment:\n"; cout << "Box 2's Width: " << Box2.getWidth() << endl; cout << "Box 2's Length: " << Box2.getLength() << endl; cout << "Box 2's Area: " << Box2.getArea() << endl; } Before: ???? Box1:Box2:

35 Memberwise Assignment int main() { Rectangle Box1, Box2; Box1.setWidth(10); Box1.setLength(20); cout << "Before the assignment:\n"; cout << "Box 1's Width: " << Box1.getWidth()<< endl; cout << "Box 1's Length: " << Box1.getLength() << endl; cout << "Box 1's Area: " << Box1.getArea() << endl; Box2 = Box1 ; cout << " \n"; cout << "After the assignment:\n"; cout << "Box 2's Width: " << Box2.getWidth() << endl; cout << "Box 2's Length: " << Box2.getLength() << endl; cout << "Box 2's Area: " << Box2.getArea() << endl; } After: Box1:Box2:

36 Fini ?