Professional Documents
Culture Documents
Lecture 3
Lecture 3
pointer reference
aStudent anotherStudent
.age 18 .age 19
1
struct StudentType struct type Declaration
enum HealthType { Poor, Fair, Good, Excellent } ; SYNTAX
struct StudentType // declares a struct data type
{ // does not allocate memory struct TypeName // does not allocate memory
long id ;
string firstname; {
string lastname; DataType MemberName ;
string school; struct members
string major;
DataType MemberName ; MemberList
int age;
float weight;
HealthType health;
}; };
StudentType aStudent; // declare variables of
AnimalType
StudentType anotherStudent;
5 6
operator.
whole, as opposed to an operation on
an individual component of the data
EXAMPLES structure
StudentType aStudent, anotherStudent; //
declare variables . .of AnimalType
.
.
aStudent.weight
anotherStudent.country
7 8
2
Examples of
Aggregate struct Operations aggregate struct operations
anotherStudent = aStudent; //
I/O, arithmetic, and comparisons of entire assignment
ChangeAge(aStudent); // r
variable: assignment to another struct eference pter
variable of same type, pass to a function as
aStudent = GetStudentData( ); // return
argument (by value or by reference), return as
value of function
value of a function
9 10
aStudent.age++ ;
cout << aStudent.age << “years “ << endl;
}
cout << aStudent.weight << “ lbs. “ << endl;
PrintWord ( aStudent.health ) ;
}
11 12
3
StudentType GetStudentData ( void )
// Obtains all information about a student from keyboard
Hierarchical Structures
// Postcondition:
// Function value = StudentType members entered at kbd The type of a struct member can be another
{ struct type. This is called nested or
StudentType aStudent ; hierarchical structures.
char response ;
do { // have user enter all members until they are correct
struct DateType
{ int month ; // Assume 1 . . 12
struct MachineRec int day ;
int year ;
// Assume 1 . . 31
// Assume 1900 . . 2050
};
Information about each machine in a shop
struct StatisticsType
contains: { float failRate ;
– an idNumber, DateType lastServiced ; // DateType is a struct type
int downDays ;
– a written description, };
struct MachineRec
– the purchase date,
{ int idNumber ;
– the cost, string description ;
StatisticsType history ; // StatisticsType is a struct type
– and a history (including failure rate, number of days DateType purchaseDate ;
down, and date of last service). float cost ;
};
15 MachineRec machine ; 16
4
struct type variable machine Unions in C++
DEFINITION
A union is a struct that holds only one of its
members at a time during program execution.
361 “drilling…” .02 1 25 1999 4 3 21 1995 8000.0
.month .day .year EXAMPLE
.failrate .lastServiced .downdays .month .day .year
union WeightType
.description . history .purchaseDate .cost {
long wtInOunces ;
.idNumber int wtInPounds; only one at a time
float wtInTons;
machine.history.lastServiced.year has value 1999 } ;
17 18
long wtInOunces ;
int wtInPounds; data type from its implementation
float wtInTons;
} ;
19 20
5
Abstract Data Type (ADT) ADT Specification Example
TYPE
TimeType
A data type whose domain and operations are
DOMAIN
specified (what) independently of any
particular implementation (how). Each value is a time in hours, minutes, and seconds.
OPERATIONS
Set the time
Print the time
Increment by one second
Compare 2 times for equality
Determine if one time is “less than” another
21 22
DOMAIN
Each value is an ordered pair of real numbers (a, b) for the ADT using existing data types
representing a + bi. (built-in or programmer-defined), and
OPERATIONS
Initialize the complex number
Write the complex number
6
Several Possible Representations of Some Possible Representations
TimeType of ComplexNumberType
3 int variables struct with 2 float members
10 45 27
-16.2 5.18
3 strings
real .imag
“10” “45” “27”
7
More about Class Using class
A class is a programmer-defined type whose
int hrs ;
int mins ; handle its class objects.
int secs ;
};
31 32
8
Member functions
categorized by task class type declaration
The class declaration creates a data type and
33 34
C++ Data Type class represents 2 separate files generally used for
an ADT class type
// SPECIFICATION FILE ( timetype .h )
2 kinds of class members: // Specifies the data and function members.
– data members and function members
class TimeType {
public:
class members are private by default . . .
data members are generally private private:
. . .
function members are generally declared public };
9
Implementation file for
TimeType Member selection operator .
// IMPLEMENTATION FILE ( timetype.cpp )
// Implements the TimeType member functions When a client uses a public member function, the
#include “ timetype.h” // also must appear in client code
function call requires a dot preceded by the name
#include <iostream.h> of the object. You can think of the function call as
being sent to this object.
Boolean TimeType :: Equal (TimeType otherTime ) const
// POST: FCTVAL == true, if this time equals otherTime ofstream outFile ;
// == false , otherwise
{
return ( (hrs == otherTime.hrs) && (mins == otherTime.mins) outFile.open (“a:\\my.out”) ;
&& (secs == otherTime.secs) ) ; variable,
} object,
instance
. . . outFile.close( ) ;
class
37 type 38
39 40
10
const member functions A const member function
void TimeType :: Write ( ) const
When a member function does not modify the // POST: Time has been output in form HH:MM:SS
private data members, use const in both the { if ( hrs < 10 )
function prototype (in specification file) and the cout << ‘0’ ;
heading of the function definition (in cout << hrs << ‘:’ ;
implementation file). if ( mins < 10 )
cout << ‘0’ ;
cout << mins << ‘:’ ;
if ( secs < 10 )
cout << ‘0’ ;
cout << secs ;
41 } 42
Compiler Compiler
Linker
client.exe 43 44
11
Aside from . and = operator+ implemented as
a member function
Other operators like +, -, ==, * cannot be
The name of the function defining == must be int resultUnits = numUnits + otherJar.numUnits ;
operator == JarType resultJar (resultUnits) ; // invokes
constructor
45 46
47
12