Download presentation
Presentation is loading. Please wait.
Published byCandace Dickerson Modified over 9 years ago
1
Structures and Classes Version 1.0
2
Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams
3
Objectives At the completion of this topic, students should be able to: Correctly use structures in a C++ program Correctly use classes in a C++ program Design and use programmer written structures & classes in a C++ program Explain the difference between a structure, class and an object Explain what properties and behaviors are Explain the difference between the structure or class interface and the structure or class implementation Write member functions for a structure and class Create accurate class diagrams using UML
4
Structures In C++, the idea of a class has its roots in data type called structure. Structures were defined in the C language. A structure is simply a way of collecting a set of different data types together, and treating the collection as a unit. i.e. a user defined datatype.
5
Structure Definition struct CDAccount { double balance; double interestRate; int term; }; the struct keyword tells the compiler that what follows is a structure type definition. this is the name of the structure type. It is also known as the structure tag. It is any valid C++ identifier. the data members of the structure are contained in a set of curly braces. the structure type definition must end with a semicolon.
6
#include using namespace std; struct CDAccount { double balance; double interestRate; int term; }; int main ( ) { CDAccount myAccount;... } The structure type definition is written outside of any function. The structure type definition must appear before it is used in the program. Once the structure type is defined, it can be used anyplace in the program, just like any of the pre-defined data types, to create an element (object) of that data type.
7
#include using std::cin; using std::cout; struct CDAccount { double balance; double interestRate; int term; }; int main ( ) { CDAccount myAccount;... } In this case, myAccount is variable whose data type is CDAccount. The variable (object) myAccount actually consists of three pieces of data: myAccount
8
#include using std::cin; using std::cout; struct CDAccount { double balance; double interestRate; int term; }; int main ( ) { CDAccount myAccount;... } myAccount these smaller pieces of data are called member variables. We specify them as follows: * myAccount.balance * myAccount.interestRate * myAccount.term myAccount.balance myAccount.interestRate myAccount.term myAccount.balance this is called the dot operator
9
#include using std::cin; using std::cout; struct CDAccount { double balance; double interestRate; int term; }; int main ( ) { CDAccount myAccount;... } myAccount member variables are used just like any other variable … myAccount.balance = 35.50; cout << myAccount.balance;... myAccount.balance myAccount.interestRate myAccount.term
10
myAccount myAccount.balance myAccount.interestRate myAccount.term yourAccount yourAccount.balance yourAccount.interestRate yourAccount.term You can create many objects (variables), using the same structure type definition. CDAccount yourAccount, myAccount;
11
You can initialize a structure with an initializer list (NOTE: ALL data members MUST be public!) CDAccount billsAccount = {35.00,.08, 12};
12
struct Members Member –data/variables (properties) –functions/methods (behaviors) struct Data //class Data { private: int _idata; char _cdata; public: void DisplayData() { cout << _idata << “ “ << _cdata << endl; } };
13
Writing Classes
14
Key Concept An object often models things in the real world
15
Real world objects have properties or attributes An object’s properties describe its “state of being” color = pink height = 6” width = 10” the bank is empty! depending upon the application, some properties are more important than others Amount of money in bank
16
An object also has behaviors behaviors define how you interact with the object Put money in! Take money out! Count the money in the bank
17
An Object’s Properties and Behaviors Work Together Put money in! Take money out! Count the money in the bank Amount of Money in the Bank this is called cohesion
18
A Class is a blueprint that a program uses when it creates an object. A class reserves no space in memory When an object is created (instantiated) from the class blueprint, memory is reserved to hold the object’s properties. An object is known as an instance of the class. Each object has it’s own space for data.
19
Put money in! Take money out! Count the money in the bank moneyInBank data member member function
20
Encapsulation CoinBank object howMuchMoney( ) moneyInBank calling method we should not allow code outside of the object to reach in and change the data directly. Instead, we call functions in the object to do it for us. data is declared as private functions are declared as public
22
note how this variable is declared inside the class, but outside of any function. where a variable is declared defines its scope. In this case the variable moneyInBank is called instance data. When an object of this class is created the variable is created in that object. The variable exists as long as the object exists. Each CoinBank object has its own copy of moneyInBank. note that the data is declared as private. this is called a access modifier class CoinBank { private: double moneyInBank; public: CoinBank ( ); double howMuchMoney ( ); void addMoney (double); void takeMoney (double); };
23
class CoinBank { private: double moneyInBank; public: CoinBank ( ); double howMuchMoney ( ); void addMoney (double); void takeMoney (double); }; declaration of a data member. Data is almost always private. the class definition lies between the opening and closing curly braces the keyword class tells the compiler that this is a class definition. the class name the private keyword defines the declarations that follow to be private. Anything declared as private cannot be seen from outside the object. the public keyword defines the declarations that follow to be public. Anything declared as public can be seen from outside the object. the class definition ends in a semicolon.
24
these are the function prototypes for the member functions of this class. The member functions define the behavior of the class. Notice that member functions are usually public. class CoinBank { private: double moneyInBank; public: CoinBank ( ); double howMuchMoney ( ); void addMoney (double); void takeMoney (double); };
25
Member Functions When a member function is called, the flow of control in the program transfers to the first statement in the function. Each statement in the function is then executed, one by one. When the last statement in the function is executed, the flow of control returns to the point where the function was called.
26
double CoinBank::howMuchMoney( ) { return moneyInBank; } void CoinBank::addMoney( double dollars ) { moneyInBank = moneyInBank + dollars; } void CoinBank::takeMoney ( double dollars ) { moneyInBank = moneyInBank - dollars; } Writing Member Functions return type class name scope resolution operator function name
27
Using the CoinBank Class int main( ) { // create a CoinBank object and call it myBank CoinBank myBank; myBank.addMoney (10.00); myBank.takeMoney(2.00); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "Bank contains $" << myBank.howMuchMoney( ); system(“PAUSE”); return 0; } declare a CoinBank variable named myBank. This declaration is like any other, it creates a variable of the type named. send messages to myBank to invoke member functions of the object.
28
class CoinBank { private: double moneyInBank; public: CoinBank ( ); double howMuchMoney ( ); void addMoney (double); void takeMoney (double); }; Class definition CoinBank myBank; myBank moneyInBank this statement takes the CoinBank class definition and uses it to create the object myBank. When creating the object, storage is allocated for the member data that is defined for objects of this class. In this case, storage is allocated for the double variable moneyInBank.
29
Abstract Data Types Programmer defined classes are known as abstract data types. We call a data type an abstract data type when the details of how data is stored and how the operations are implemented are not visible to programmers who use the data type. Defining a class so that these details are not known is known as information hiding, data abstraction, or encapsulation.
30
class CoinBank { private: double moneyInBank; public: CoinBank ( ); double howMuchMoney ( ); void addMoney (double); void takeMoney (double); }; moneyInBank Encapsulation Data members are always private. Programmers who use this class have no idea how the data is actually declared. The developer of the class is free to change the way that member data is declared without affecting programs that use this class. Functions outside of the class have no access to data that is declared as private in the class, except by calling member functions of the class.
31
int main( ) { // create a CoinBank object and call it myBank CoinBank myBank; myBank.addMoney (10.00); myBank.takeMoney(2.00); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "Bank contains $" << myBank.moneyInBank; cin.get( ); return 0; } For Example, this statement is illegal. The compiler will return a message telling you that moneyInBank is not accessible. << myBank.howMuchMoney( ); Correct this by using a member function to get the amount of money in the bank.
32
class CoinBank { private: double moneyInBank; public: CoinBank ( ); double howMuchMoney ( ); void addMoney (double); void takeMoney (double); }; moneyInBank Encapsulation With few exceptions, functions are declared as public so that they can be accessed from code outside of the class. However, the details of how the function is implemented should not be of concern to anyone using the class. The function prototypes listed in the class definition give other programmers the interface to the class. Given these function prototypes, and a few comments about the functions, another programmer knows what each function does, what parameters to pass to the function, and what each returns.
33
Accessor and Mutator Functions Accessor functions provide a way of reading private member data. For example, the function howMuchMoney( ) is an accessor function. Mutator functions provide a way of changing private member data. For example, the function addMoney( ) is a mutator function.
34
Classes and I/O In general, it is a good idea to make your classes independent of the environment in which your program runs. One area where students often want to build in system dependencies is doing I/O. A good rule of thumb is to never write member functions in your classes that are not cohesive sucha as reading from stdin and writing to stdout. Such classes become dependent on the environment in which they run. It is impossible to move such a class to a different environment (for example from a DOS Console environment to a Windows environment) without making significant changes to the class.
35
Class Diagrams During the design phase of a program, designers need a precise, standard language for describing classes, and the relationships between classes. A number of different approaches to such a modeling language were developed in the late 1980’s and early 1990’s. These culminated in a standard called UML (Unified Modeling Language) in the late 1990s.
36
CoinBank In UML, a class diagram is used to describe a class in a very precise way. A class diagram is a rectangle. At the top of the rectangle is the class name. A line separates the class name from the rest of the diagram.
37
CoinBank - moneyInBank: double Following the class name we write the data members of the class. A line separates the data members from the rest of the diagram. access modifier: + public - private data member name data type
38
CoinBank - moneyInBank: double + CoinBank ( ) + addMoney (:double): void + takeMoney ( :double): void + howMuchMoney( ): double Following the data members, we write the member functions. access modifier + public - private function name function parameters return type
39
Diagram to Code Given a class diagram, it is relatively easy to write down the class definition. CoinBank - moneyInBank: double + CoinBank ( ) + addMoney (:double): void + takeMoney ( :double): void + howMuchMoney( ): double class CoinBank { private: double moneyInBank; public: CoinBank ( ); void addMoney (double); void takeMoney (double); double howMuchMoney ( ); };
40
CoinBank Code
41
Practice
42
Design a class that represents “Integer” objects. What are the data members of the class?
43
Design a class that represents “Integer” objects. Suppose we want functions to set the integer value in the object retrieve the integer value in the object retrieve the reciprocal of the value in the object
44
Create the class diagram Integer
45
Create the class definition class Integer {
46
Create the class implementation
47
Write a main( ) to test it #include “integer.h” #include using namespace std; int main ( ) {
48
Design a class that represents “Student” objects. You could use an object of this class to hold the student information you print out at the beginning of each of your programming projects. What are the data members of the class?
49
Design a class that represents “Student” objects. Suppose we want functions to set the name, course, and section values in the object retrieve the name, course and section values from the object
50
Create the class diagram Student
51
Create the class definition class Student {
52
Create the class implementation
53
Write a main( ) to test it #include “student.h” #include using namespace std; int main ( ) {
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.