Download presentation
Presentation is loading. Please wait.
Published byLogan Smith Modified over 8 years ago
1
CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel
2
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
3
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; 21 22 myCar.make = "Mercedes"; 23 myCar.year = 2005; 24 myCar.price = 45567.75; 25 26 getYourCar(yourCar); 27 28 cout << “Your car is a “ 29 << yourCar.make 30 << “\nI’ll offer you “ 31 << yourCar.price – 100 32 << “ for it.\n”; 33 }
4
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
5
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
6
Fall 2010CSCI 383 Lecture 6 M. van Bommel 6 Main Program using Objects Object A Object B Object C Main Program
7
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
8
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
9
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.
10
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 } 7 8 9 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
11
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 } 20 21 float Checkbook::getBalance( ) { 22return balance; 23 } 24 25 float Checkbook::getLastCheck( ) { 26return lastCheck; 27 } end of checkbook.cpp
12
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
13
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
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!
15
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.
16
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
17
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.