CHAP 3 : PENGENALAN KEPADA C++ |
#ifndef DATE-H
#define DATE-H
/* Date object
Definition file */
typedef char Boolean
#define True 1
#define False 0
/* Object definition */
typedef struct _Date{
int day, month, year;
}
Date;
/*Public Functions */
Date* DateCreate (int day, int month, int year);
Date* DateCreateFromUser();
void DatePrint (Date *d);
void DateDestroy(Date *d);
#define DateGetYear(d) d->year /*inline*/
#endif //DATE-H
/* Date object & implementation file */
# include <stdio.h>
# include <mallloc.h>
# include <assert.h>
# include "date.h"
/*Private function*/
static Boolean _IsValid(int day, int month) {
static int len[] = {0, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31};
if (month < 1 || month > 12) {
printf ("_IsValid : month
invalid \n");
return False;
}
if (day < 1 || day > len [month] ) {
printf ("_IsValid : day
invalid \n");
return False;
}
return True;
}
/* Public function */
Date* DateCreate ( int day, int month, int year){
Date *newDate;
assert (_IsValid (day, month));
newDate = (Date*) malloc (sizeof (Date));
assert (newDate);
newDate->day = day;
newDate->month = month;
newDate->year = year;
return newDate;
}
Date* DateCreateFromUser(){
printf ( " Enter date ( dd mm yyyy) : ");
scanf("%d %d %d ", &day, &month, &year);
return DateCreate (Day, month, year);
}
void DatePrint (Date *d) {
assert (d);
printf ("%d %d %d", d->day, d->month, d->year);
}
void DateDestroy (Date *d) {
assert(d);
free (d);
}
#ifndef NAME_H
#define NAME_H
/* Name object & definition file */
#define MAXSTRLEN 80
typedef char Boolean;
#define True 1
#define False 0
/* Object Definition */
typedef struct _Name{
char surname[MAXSTRLEN], othername[MAXSTRLEN];
}Name;
/* Public functions */
Name * NameCreate (char *surname, char *othername);
Name * NameCreateFromUser();
void NamePrint(Name *n);
Boolean NameSame(Name *a, Name *b);
void NameDestroy(Name *n);
endif //NAME_H
Boolean NameSame(Name *a,
Name *b){
assert (a);
assert (b);
if (strcmp (a->surname, b->surname) = = 0 &&
strcmp (a->othername, b->othername) = = 0)
return True;
else
return False;
}
void NameDestroy(Name *n)
{
assert (n);
free (n);
}
Constants
Enumerations
Function Prototype
Function prototype | C++ | ANSI C |
int func(); | No parameter | Arbitrary no of parameter |
int func(void);
int func(int, int); int func(int x, int y); int func(.......); int func(char*, int, ......); |
No. parameters
2 int 2 int Arbitrary no. 1 char*, I int, followed by an arbitrary no. of parameter |
Default Function Parameters
Inline function
void main() {
int x,y, z;
x=12; y=34;
z=MIN(x++, y++); // side-effect, smaller
value
cout << "x=" << x << ", y =" << y << "\n";
x=12; y=34;
// work as expected
z= min(x++, y++);
cout << "x=" << x << ", y =" << y << "\n";
}
References
Initialisation of references
//File
B
extern int& refNum;
void byReference(InfoRec &ir) {
cout << ir.code << "," << ir.name << "\n";
}
void main() {
static InfoRec data = {10, "Hello"};
byValue (data);
byPointer (&data);
//& is needed
byReference (data);
//No & is needed
void main() {
Account *ac;
ac = new Account (123, "David");
delete ac;
//calls destructor, which deallocates name,
//then deallocates Account
}
1. Tulis semula dan lengkapkan aturcara yang terdapat dalam nota ini seperti