Savitch Chapter 10  Defining Classes  Topics: Structures Class Definitions Members ○ Data Members ○ Member Functions ○ Public/Private Sections ○ Types.

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

Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Chapter 10 Defining Classes. User-defined Types Are data types defined by programmers. Include: – typedef: simple data definition – struct: a structure.
Constructors and Other Tools Version 1.0 Topics Constructors & Destructors Composition const Parameter Modifier const objects const functions In-line.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Object-Oriented Programming Using C++ CLASS 27.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
User-Defined Classes Chapter Class Definition t int, float, char are built into C+ t Declare and use –int x = 5; t Create user defined data.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
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.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: User-Defined Classes Problem Solving, Abstraction, and Design.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Classes and Data Structures Jeffrey S. Childs
Savitch - Chapter 11CS Extending the Class Capabilities Let’s extend the definition of the BankAccount class to illustrate some additional capabilities.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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.
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?
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
11 Introduction to Object Oriented Programming (Continued) Cats.
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 , ]
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Introduction to Object Oriented Programming Chapter 10.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
1 C++ Classes and Data Structures Course link…..
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction

Chapter 1.2 Introduction to C++ Programming
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?
A First C++ Class – a Circle
Classes Object-oriented programming: Example: Bank transactions
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
COP 3330 Object-oriented Programming in C++
CPS120: Introduction to Computer Science
CS 144 Advanced C++ Programming February 21 Class Meeting
Chapter 9 Introduction To Classes
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Savitch Chapter 10  Defining Classes  Topics: Structures Class Definitions Members ○ Data Members ○ Member Functions ○ Public/Private Sections ○ Types of Member Functions Savitch - Chapter 10CS 15065

Structured Data Savitch - Chapter 1066CS 150 A major problem with modular code is the need to use a large number of parameters to deal with several objects that might be closely related to each other. A preferable approach would be to group these values together in a single structure, which could easily be passed to a function as a single parameter. void processSale (string inventoryNbr, float price, float weight, int &nbrInStock, int &nbrSoldThisYear, int storeNbr, int month, int day, int year, float time); void processSale (Merchandise &itemSold, Time timeOfSale);

Savitch - Chapter 10CS C++ provides a struct mechanism for defining such structures. struct Time { int month; int day; int year; float timeOfDay; }; struct Merchandise { string inventoryNbr; float price; float weight; int nbrInStock; int nbrSoldThisYear; int storeNbr; }; Structs in C++

Savitch - Chapter 10CS Structure Variables and Parameters Once a struct has been defined, variables of that type can be declared. void processSale (Merchandise &itemSold, Time timeOfSale); Time calculateStoreClosing(int monthNo, int dayNo, int yearNo);  void main() {  closingTime = calculateStoreClosing(12, 24, 1998); processSale(saleItem, closingTime);  } const float EmployeeDiscountPercent; int dailyQuota; Merchandise saleItem, itemSold; Time timeOfSale, openingTime, closingTime; And parameters and functions of that type can be used.

Savitch - Chapter 10CS Working with the Structure Members The data elements comprising a struct are known as its members. The members are accessed by means of the C++ “dot” notation. Time calculateStoreClosing(int monthNo, int dayNo, int yearNo) { Time t; t.month = monthNo; t.day = dayNo; t.year = yearNo; if ((monthNo == 12) && (dayNo == 24)) t.timeOfDay = 18.00F; else t.timeOfDay = 22.00F; return t; } void processSale (Merchandise &itemSold, Time timeOfSale); { if (timeOfSale.timeOfDay < 8.00) itemSold.price -= EmployeeDiscountPercent*itemSold.price; itemSold.nbrInStock--; itemSold.nbrSoldThisYear++; }

Classes in C++  Classes allow for the combining of a data set with a set of functions that work on that data set  This allows for less cumbersome parameter lists, and allows the data better protection from being set to invalid values. Savitch - Chapter 10CS 15070

Savitch - Chapter 10CS C++ Classes To enhance modularity and information hiding even further, it would be nice if we could create types for which the internal workings are “private” to the rest of our program, and which can only be accessed or altered by means of specified “public” functions. BankAccount acct; cout >> “Enter interest rate: ”; cin << acct.intRate; BankAccount acct; float rate; cout >> “Enter interest rate: ”; cin << rate; acct.setIntRate(rate);  void BankAccount::setIntRate(float r) { if ((r =0.1F)) intRate = 0.01F else intRate = r; return; } Doesn’t prevent rates below 0% or above 10%! Does prevent rates below 0% or above 10%!

Savitch - Chapter 10CS The C++ class C++ provides a class mechanism for defining such types. class BankAccount { public: void setIntRate(float newIntRate); void setBalance(float newBalance); bool checkAcctNbr(string testAcctNbr); bool checkATMCode(string testATMCode); float getIntRate(); float getBalance(); private: void setAcctNbr(string newAcctNbr); void setATMCode(string newATMCode); string getAcctNbr(); string getATMCode(); float intRate; float balance; string acctNbr; string atmCode; }; Prototypes for “public member functions”, which can be used by any function that declares a variable of this type. Prototypes for “private member functions”, which can only be used by the other member functions of this type. “Private data members”, which can only be used by the member functions of this type.

Savitch - Chapter 10CS The Scope Resolution Operator When implementing the member functions, the compiler needs to know that they are members of the class, and are only defined within the context of the object that’s using them. The scope resolution operator, ::, is used to accomplish this. void BankAccount::setIntRate(float newIntRate) { if ((newIntRate 0.1F)) intRate = 0.01F; else intRate = newIntRate; } bool BankAccount::checkATMCode(string testATMCode) { return (atmCode == testATMCode); } string BankAccount::getATMCode() { return atmCode; }

Savitch - Chapter 10CS Working with the Class Members Just like with structures, the dot operator is used to access class members, both data members and member functions. void validateAcctNbr(BankAccount acct, string nbr) { if (accountNumber == nbr) cout << “Account Number Validated”; else cout << “Account Number Invalid! Call the cops!”; } void validateAcctNbr(BankAccount acct, string nbr) { if (acct.accountNumber == nbr) cout << “Account Number Validated”; else cout << “Account Number Invalid! Call the cops!”; } void validateAcctNbr(BankAccount acct, string nbr) { if (acct.getAcctNbr() == nbr) cout << “Account Number Validated”; else cout << “Account Number Invalid! Call the cops!”; } void validateAcctNbr(BankAccount acct, string nbr) { if (acct.checkAcctNbr(nbr)) cout << “Account Number Validated”; else cout << “Account Number Invalid! Call the cops!”; } Compilation Error! accountNumber is not a variable in this function! Compilation Error! accountNumber is a private data member of the BankAccount class! Compilation Error! getAcctNbr is a private member function of the BankAccount class! No problemo! checkAcctNbr is a public member function of the BankAccount class!

Savitch - Chapter 10CS // circleDrawingDriver.cpp // // This program generates circle data and draws them using ASCII characters. // #include #include using namespace std; const float PI = F; const int PADDING = 3; const int ASCII_ZERO_VALUE = 48; class Circle { public: Circle();// Constructor bool setCoordinates(int x, int y);// Mutator functions bool setRadius(int r); bool setASCIICharacter(char ch); float computeArea(); // Member functions float computePerimeter(); void displayCircleAttributes(); void drawCircle(); int getXCoord() const;// Accessor functions int getYCoord() const; int getRadius() const; char getASCIICharacter() const; private: int xCoord;// Data members int yCoord; int radius; char asciiCharacter; };// Notice the REQUIRED semicolon! A Complete Example The declaration of the Circle class. “Constructors” create new objects of this class; with no parameters, this one’s known as the “default” constructor, and is called whenever a variable of type Circle is declared. Regular public member functions; they can be called by any function with a Circle variable. Private data members; they can only be used by Circle member functions. Accessor public member functions; they can access but not alter a Circle object. Mutator functions allow programmers to change data member values.

Savitch - Chapter 10CS ////////////////////////////////////////////////////////////////// // The main function serves as a driver to display a variety of // // circles with a variety of sizes, positions, and characters. // ////////////////////////////////////////////////////////////////// void main() { Circle circ; char YorN; int count = 1; int positionX = 4; int positionY = 2; cout << "Ready for the first circle? (Enter Y or N) "; cin >> YorN; while ((YorN == 'y') || (YorN == 'Y')) { circ.setASCIICharacter(char(ASCII_ZERO_VALUE+count)); circ.setRadius(count+3); circ.setCoordinates(positionX,positionY); circ.displayCircleAttributes(); circ.drawCircle(); cout << "Ready for another circle? (Enter Y or N) "; cin >> YorN; count++; positionX = (positionX + 11) % 13; positionY = (positionY + 13) % 11; } return; } The Circle Example (Continued) The declaration of a variable of type Circle. Calling some of Circle’s public member functions; note the use of the dot notation.

Savitch - Chapter 10CS // This default constructor sets up the data members with default values. // Circle::Circle() { xCoord = yCoord = radius = 0; asciiCharacter = ' '; } // Assign the x- and y- coords of the circle's center with parameterized values. // bool Circle::setCoordinates(int x, int y) { xCoord = x; yCoord = y; return true; } // Assign the radius of the circle the parameterized value. // bool Circle::setRadius(int r) { if(r <= 0) return false; radius = r; return true; } // Assign the fill character of the circle the parameterized value. // bool Circle::setASCIICharacter(char ch) { asciiCharacter = ch; return true; } The Circle Example (Continued) The Circle’s default constructor initializes the data members; default constructors do not have to do initialization. Notice that the dot operator is not needed to access members inside member functions themselves.

Savitch - Chapter 10CS // Compute and return the area of the circle. // float Circle::computeArea() { return PI * radius * radius; } // Compute and return the perimeter of the circle. // float Circle::computePerimeter() { return 2 * PI * radius; } // Output the circle's data member values, as // // well as the area and perimeter of the Circle. // void Circle::displayCircleAttributes() { cout.setf(ios::fixed); cout << setprecision(4); cout << "Center's x-coordinate: " << xCoord << endl; cout << "Center's y-coordinate: " << yCoord << endl; cout << "Circle's radius: " << radius << endl; cout << "Circle's area: " << computeArea() << endl; cout << "Circle's perimeter: " << computePerimeter() << endl; cout << "Circle's fill character: " << asciiCharacter << endl; } The Circle Example (Continued) Also notice that the dot operator is not needed to access member functions in member functions themselves; the compiler still recognizes that the Circle being operated upon is the one being referred to.

Savitch - Chapter 10CS // Output the Circle, using its ASCII character to draw it, // // as well as vertical and horizontal symbols to draw the // // coordinate axes, and an 'X' at the center of the circle. // void Circle::drawCircle() { const int PADDING = 4; const float HEIGHT_WIDTH_RATIO = 1.5F; int lowerX = (xCoord-radius < -PADDING) ? (xCoord-radius-PADDING) : -PADDING; int upperX = (xCoord+radius > PADDING) ? (xCoord+radius+PADDING) : PADDING; int lowerY = (yCoord-radius < -PADDING) ? (yCoord-radius-PADDING) : -PADDING; int upperY = (yCoord+radius > PADDING) ? (yCoord+radius+PADDING) : PADDING; for (int y = upperY; y >= lowerY; y--) { for (int x = int(HEIGHT_WIDTH_RATIO*lowerX); x <= int(HEIGHT_WIDTH_RATIO*upperX); x++) { if ((x == xCoord) && (y == yCoord)) cout << 'X'; else if (pow((x-xCoord)/HEIGHT_WIDTH_RATIO,2) + pow(y-yCoord,2) <= pow(radius,2)) cout << asciiCharacter; else if ((x == 0) && (y == 0)) cout << '+'; else if (x == 0) cout << '|'; else if (y == 0) cout << '-'; else cout << ' '; } cout << endl; } The Circle Example (Continued)

Savitch - Chapter 10CS // Access and return the Circle's x-coordinate value. // int Circle::getXCoord() const { return xCoord; } // Access and return the Circle's y-coordinate value. // int Circle::getYCoord() const { return yCoord; } // Access and return the value of the Circle's radius. // int Circle::getRadius() const { return radius; } // Access and return the value of the Circle's ASCII fill character. // char Circle::getASCIICharacter() const { return asciiCharacter; } The Circle Example (Continued) The accessor functions have been set up as constant member functions; this guarantees that calling them will not alter the values of any of the Circle’s data members..

Savitch - Chapter 10CS The Circle Example (Continued)

Savitch - Chapter 10CS Class Definition and Implementation Files ///////////////////////////////////////////////////////// // circle.h The class definition for the Circle class // ///////////////////////////////////////////////////////// #ifndef CIRCLE_H #include using namespace std; class Circle { public: Circle();// Constructor void setCoordinates(int x, int y);// Member functions void setRadius(int r); void setASCIICharacter(char ch); float computeArea(); float computePerimeter(); void displayCircleAttributes(); void drawCircle(); int getXCoord() const;// Accessor functions int getYCoord() const; int getRadius() const; char getASCIICharacter() const; private: int xCoord;// Data members int yCoord; int radius; char asciiCharacter; };// Notice the REQUIRED semicolon! #define CIRCLE_H #endif To relieve the programmer from the burden of rebuilding the code associated with a completed class, and to make the class reusable with other driver programs, the class definition is normally placed in a header file and the class implementation is normally placed in a separate program file. The Circle’s class definition file: circle.h

Savitch - Chapter 10CS // Access and return the Circle's x-coordinate value. // int Circle::getXCoord() const { return xCoord; } // Access and return the Circle's y-coordinate value. // int Circle::getYCoord() const { return yCoord; } // Access and return the value of the Circle's radius. // int Circle::getRadius() const { return radius; } // Access and return the value of the Circle's ASCII fill character. // char Circle::getASCIICharacter() const { return asciiCharacter; } // Output the Circle, using its ASCII character to draw it, // // as well as vertical and horizontal symbols to draw the // // coordinate axes, and an 'X' at the center of the circle. // void Circle::drawCircle() { const int PADDING = 4; const float HEIGHT_WIDTH_RATIO = 1.5F; int lowerX = (xCoord-radius < -PADDING) ? (xCoord-radius-PADDING) : -PADDING; int upperX = (xCoord+radius > PADDING) ? (xCoord+radius+PADDING) : PADDING; int lowerY = (yCoord-radius < -PADDING) ? (yCoord-radius-PADDING) : -PADDING; int upperY = (yCoord+radius > PADDING) ? (yCoord+radius+PADDING) : PADDING; for (int y = upperY; y >= lowerY; y--) { for (int x = int(HEIGHT_WIDTH_RATIO*lowerX); x <= int(HEIGHT_WIDTH_RATIO*upperX); x++) { if ((x == xCoord) && (y == yCoord)) cout << 'X'; else if (pow((x-xCoord)/HEIGHT_WIDTH_RATIO,2) + pow(y-yCoord,2) <= pow(radius,2)) cout << asciiCharacter; else if ((x == 0) && (y == 0)) cout << '+'; else if (x == 0) cout << '|'; else if (y == 0) cout << '-'; else cout << ' '; } cout << endl; } // Assign the fill character of the circle the parameterized value. // void Circle::setASCIICharacter(char ch) { asciiCharacter = ch; return; } // Compute and return the area of the circle. // float Circle::computeArea() { return PI * radius * radius; } // Compute and return the perimeter of the circle. // float Circle::computePerimeter() { return 2 * PI * radius; } // Output the circle's data member values, as // // well as the area and perimeter of the Circle. // void Circle::displayCircleAttributes() { cout.setf(ios::fixed); cout << setprecision(4); cout << "Center's x-coordinate: " << xCoord << endl; cout << "Center's y-coordinate: " << yCoord << endl; cout << "Circle's radius: " << radius << endl; cout << "Circle's area: " << computeArea() << endl; cout << "Circle's perimeter: " << computePerimeter() << endl; cout << "Circle's fill character: " << asciiCharacter << endl; } The Class Implementation File: circle.cpp ///////////////////////////////////////////////////////////////// // circle.cpp The class implementation for the Circle class. // ///////////////////////////////////////////////////////////////// #include #include "circle.h“ using namespace std; const float PI = F; const int PADDING = 3; // This default constructor sets up the data members with default values. // Circle::Circle() { xCoord = yCoord = radius = 0; asciiCharacter = ' ‘; } // Assign the x- and y- coordinates of the circle's center with parameterized values. // bool Circle::setCoordinates(int x, int y) { xCoord = x; yCoord = y; return true; } // Assign the radius of the circle the parameterized value. // bool Circle::setRadius(int r) { if(r <= 0) return false; radius = r; return true; }

Savitch - Chapter 10CS The Driver File: circleDrawingDriver2.cpp // circleDrawingDriver2.cpp // // This program generates the data for circles and then draws them using ASCII characters. // #include #include " circle.h" using namespace std; const int ASCII_ZERO_VALUE = 48; // The main function displays a variety of circles. // void main() { Circle circ; char YorN; int count = 1; int positionX = 4; int positionY = 2; cout << "Ready for the first circle? (Enter Y or N) "; cin >> YorN; while ((YorN == 'y') || (YorN == 'Y')) { circ.setASCIICharacter(char(ASCII_ZERO_VALUE+count)); circ.setRadius(count+3); circ.setCoordinates(positionX,positionY); circ.displayCircleAttributes(); circ.drawCircle(); cout << "Ready for another circle? (Enter Y or N) "; cin >> YorN; count++; positionX = (positionX + 11) % 13; positionY = (positionY + 13) % 11; } return; }

Savitch - Chapter 10CS An Alternative Driver //////////////////////////////////////////////////// // diagonalDotDriver.cpp // // This program generates same-sized circles that // // are displayed at a diagonal to each other. // //////////////////////////////////////////////////// #include #include "circle.h" using namespace std; //////////////////////////////////////////////////////// // The main function drives the diagonal dot display. // //////////////////////////////////////////////////////// void main() { Circle circ; circ.setASCIICharacter('O'); circ.setRadius(4); for (int i = 0; i < 40; i += 8) { circ.setCoordinates(i,0); circ.drawCircle(); } return; } Note that this driver requires no changes to the definition of the Circle class!

Savitch - Chapter 10CS Constructors Circle::Circle() { xCoord = yCoord = radius = 0; asciiCharacter = ' '; } There are three basic types of constructors in C++ programming. Circle::Circle() { } Circle::Circle(int x, int y, int r, char ch) { xCoord = x; yCoord = y; radius = r; asciiCharacter = ch; } Circle::Circle(char ch) { asciiCharacter = ch; } Circle::Circle(const Circle &c) { xCoord = c.xCoord; yCoord = c.yCoord; radius = c.radius; asciiCharacter = c.asciiCharacter; } Default Constructor Called when a new variable of this type is declared. (Only one allowed.) Initializing Constructor Called when variable of this type is having some or all of its data member values initialized. (More than one allowed.) Copy Constructor Called when a variable of this type is passed by value to a function. (Only one allowed.) Note that constructors always have the same name as the class.

Savitch - Chapter 10CS Calling the Constructors void main() { Circle roundo;  } void main() { Circle C1(3,-2,7,’$’); C2 = Circle(’!’);  } void main() { Circle bigArc;  smileyFace(bigArc);  }  void smileyFace(Circle button) {  } Default Constructor Don’t call it with the empty parentheses; the compiler might conclude that this is a prototype! Initializing Constructor As long as the parameter lists differ in type, the compiler won’t get confused. Copy Constructor You might be able to pass by value without one, but the results would be unpredictable.

Savitch - Chapter 10CS Abstract Data Types To what extent does a programmer need to know the implementation of the Circle class in order to use it in a driver? If the programmers who use a data type don’t have to know how its members are implemented, then it’s considered an abstract data type. Does the programmer need to know the types of the data members in order to use the constructors? Perhaps with that initializing constructor... Does the programmer need to know anything about the data members in order to draw the circle or display its attributes? Not really... Does the programmer need to know the types of the data members in order to set them or access them? Well, yeah... Does the programmer need to know how the area and perimeter are being computed? Nope!

Savitch - Chapter 10CS Object- Oriented Emphasis on what is being done Advantages of ADTs By limiting the required understanding of the class to the public members, modifiability, readability, and reusability are all enhanced! Procedural Emphasis Upon How To Do Things

Savitch - Chapter 10CS Achieving ADT Status in C++ Show A Little Class! (Put your class definition and your class implementation in separate.h and.cpp files!) Basic Public Broadcasting! (That is, use public member functions for only basic operations, and ensure that users know how to use them!) Private Members Only! (At least, private data members only!)