Sept. 12, 1996
Bit Fields

Recall: Structure Format
struct date
{
int year;
float month
int day;
};
struct date
{
char name[20];
float miles;
int boroughs;
struct date inc_date;
struct date orig_date;
}info;
Recall:
Bit Fields are Used For
1. Increasing Efficiency
2. Communications Programming
3. Security Purposes
Format
 
Examples:
 
{
unsigned underline: 1;
unsigned boldface: 1;
unsigned italics: 1;
}printer;
 
 
{
unsigned code1: 4;
unsigned code2: 2;
unsigned code3: 8;
}p;
 
 
{
unsigned code1: 4;
unsigned: 3;
unsigned code2: 2;
unsigned code3: 8;
}p;
 
 
Remember
Sept. 19, 1996

Function Pointers

References
Function Pointers
 
Example:
int sum(int a, int b);
void main(void)
{
int result;
result = sum(10, 12)
printf(" %d ", result);
}
sum(int a, int b)
{
return(a+b);
}
Can be coded as follows:
int (*p) (int a, int b);
int sum(int a, int b);
void main(void)
{
int result;
p = sum
result = (*p)(10, 20);
printf(" %d ", result);
}
sum(int a, int b)
{
return(a+b);
}
 
 
 
Sept 19, 1996

Quick Sort

References
Format
Example:
qsort(array, 100, sizeof(int), comp);

Oct. 3, 1996

Dynamic Allocation

Reference


Purpose

Problem: In our first assignment, we coded an array of structures called homres_records[50]. The said structure is capable of holding 50 records.

Question 1: What if the user has 75 homes? (not enough memory)
Question 2: What if the user has only 10 homes? (too much memory)

The purpose of Dynamic Allocation is to allot the available RAM to a program on a need to basis

Dynamic Allocation


Example 1

int *p;
p = (int *) malloc(100);


Example 2

int *addr;
addr = (int *)calloc(200, sizeof(int));


Example 3

char *wordpointer;
char word[25];
wordpointer = (char *) malloc(strlen(word) +1);


 
Example 4

wordpointer = (char *) realloc(wordpointer, 15);


Example 5

free(wordpointer);



Oct 10, 1996

Colours and Graphics

Reference


Setting the Monitor Mode


Format:

void main(void)

{

textmode(C80); /* sets the monitor mode */
-
-
textmode(LASTMODE); /* rests the monitor to its original setting */

}

Adding a Background Colour

Format:

textbackground(COLOUR); /* page 264 - 265 */
clrscr();

Adding a Text Background

Format:

textbackground(COLOUR); /* page 264 - 265 */
cprintf(" ")

Adding Colour to Text

Format:

textcolor(COLOUR);
cprintf(" ");

 
Moving the Cursor

Format:

gotoxy(column, rows); /* page 269 */


The cprintf() Function


Example:

textcolor(RED);
cprintf("Hello /r ");


The cscanf() Function


Example:

printf("Enter List Price: ");
textcolor(BLUE);
cscanf(" %d ", num); /* page 600 */

The getche() Function


Example:

printf("Enter List Price: ");
textcolor(BLUE);
cscanf(" %d ", num);
getche();

The cgets() Function


 
Example:

char name[22], *pointer;

*/ We always need to code two extra elements. The first element (name[0] in this example) holds the value of the maximum string size. The second element (name[1] in this example) holds the size of the string that is actually keyed in by the user. The remaining elements (in this example, name[2] through name[22]) contain the actual characters keyed in by the user */

name[0] = 19; */ maximum string size is set at 19 (20 if we include the null) */

printf("Enter Owner’s Name: ");
textcolor(BLUE);
pointer = cgets(name); */ assigns the address of name to the variable pointer */

textcolor(RED);
cprintf("The Home Owner’s Name is %s", pointer);

Available Colours


 
Oct. 17, 1996

Graphics


Setting the Background Colour

Format:

setbkcolor(COLOUR); \* Reference: page 275 *\

Setting the Text Colour

Format:

setcolor(COLOUR); \* Reference: page 276 *\

Moving the Cursor

Format:

moveto(X, Y); \* Reference: page 639 *\

Displaying Text

Format:

outtext("STRING");

Setting the Text Style

Format

settextstyle(FONT, DIRECTION, SIZE);


Centering Text


Pie Charts

Format:

pieslice(X, Y, START, END, RADIUS);

 

Example:

pieslice(100, 150, 0, 60, 5)


 
 
 









Oct. 31, 1996

Classes

Definitions

Data Hiding
- protecting your data from unauthorized access

User Defined Data (UDT) - a variable type defined by the user (also known as Abstract Data Type (ADT))

Private - main() does not have access

Public - main() has access

Encapsulation - combining data and functions inside a class

Access Functions - allow main to see the value of a private data member (get functions)

 
Classes


Format:

class Payroll

{

char name[30];

float salary;

};

void main(void)

{


Payroll employee;

}

 
Example 1:

class Store

{
int s_num;
double sales;
float pr_margin;

public:

char name[25];
char address[30];
long int sq_feet;
};

Store big_store;
strcpy(big_store.name, "Save All");

/* big_store.sales = 84543.43 will not compile */


Example 2:

class Date

{
int day;
int month;
int year;

public:

int get_month(void) { return(month); }
int get_day(void) { return(day); }
int get_year(void) { return(year); }
void set(const int d, const int m, const int y);

{
day = d;
month = m;
year = y;
}

};

Date today;

today.set(31, 10, 1996); /* Sets the date to October 31, 1996 */

printf(" %d", today.get_day()); /* Displays the Current Day */

 
Non-Inline Functions


Format:

class Date

{

public:

int test_date(const int d);
int test_month(const int m);
int test_year(const int y);

};

int Date :: test_day(const int d)

{
return (d > 31) ? 0 : 1;
}


The cin Operator


Example:

cin >> d >> m >> y ;


The cout Operator


Examples:

1. cout << "What is today’s day, month, and year?"; // Displays a message on the screen

2. cout << "The date is set to " today.get_month(); // Displays a message and other data (details)

 
 
 
void main(void)

{
int years = 4;
char mesg[] = {I want to learn C++);
cout << "I’ve known C for " <<years<< "years."<<mesg;
}

 
4. cout << "Chun" <<endl; // Displays a message and moves the cursor down one line (same as \n

Formatting Detail


setw( ); // Sets the minimum width size

setprecision( ); // Sets the decimal precision

setiosflag(ios :: fixed); // Sets the notation (fixed, scientific)

setiosflag(ios :: UPERCASE); // Sets all letters to uppercase (uppercase, lowercase)

setiosflag(ios :: SHOWPOS); // Shows the sign

setiosflag(ios :: SHOWPOINT); // Shows the decimal point and all significant digits


Nov. 14, 1996

Dynamic Allocation

Reference


 
Format

struct patient

{

char name[20];
int age;

} * patient_ptr;

patient_ptr = (patient *) malloc(sizeof(patient)); */ C method of allocating memory */
free(patient_ptr); */ C method of freeing memory */

patient_ptr = new patient; */ C++ method of allocating memory */
delete patient_ptr; */ C++ method of freeing memory */


 
Nov. 14, 1996

Throwing an Exception


Format

extern void
(*set_new_handler (void (*memory_err) () )) ();

void memory_err()

{
cout << "A Memory Allocation Error Occurred. \n";
exit(1);
}

void main()

{
set_new_handler(memory_error);
-
-
-
}

 
Nov. 14, 1996

Polymorphism

Reference


Definition


Example:

Let us assume two classes exist in a particular program. One class is called "Employee", and the other class is called "Customer". Both classes possess a public member function called "print()".

void main()

{
Employee employee; */ creates an object of the class Employee */
Customer customer */ creates an object of the class Customer */

employee.print(); */ calls print() defined in the Employee class */
customer.print(); */ calls print() defined in the Customer class */
}

 
Nov. 14, 1996

Constructors and Destructors

Reference


Constructor Rules

  1. posses the same name as the class

  2. possess no return type

  3. must be public

  4. may uses constructors of the same name (function overloading)

  5. may use arguments and defaults

  6. system calls constructors automatically (do not code a call)


Example:

class Sample

{
-
-

public:

Sample();

Sample (int init)

{

C = ‘A’;
I = init;
x = 20;
}

-
-
};

void main()

{
Sample avar;
Sample avar(7);
-
-
}

 
Destructor Rules

  1. given the same name as the class plus a ~

  2. never posses a return type

  3. cannot be overloaded

  4. must be coded public

  5. cannot contain arguments

  6. must be called implicitly


Example:

class Sample

{
-
-
-

public:
~Sample();
-
-

}

Sample :: ~Sample()

{
cout << "Destructor was Called"
}

 
 
Nov. 20, 1996

Inheritance

Reference


Notes:









 





 




 
 

 
Format

class Name

{

private:
-
-
-

protected:

-
-
-

public:

-
-
-
};

class Name2 : (DERIVED CLASS ACCESS) Name

{

private:
-
-
public:
-
-
-
};


Data Member/Member Function Classifications


Derived Class Classifications


Nov. 21, 1996

Initialization List

Reference


Notes:


Example:

class Store

{
int num;
float sales;


public:
Store(int n, float s)



{
num = n;
sales = s;
}

-
-
-
};

 
Nov. 29, 1996

THIS Pointer

Reference


Notes


Example:


#include <iostream.h>

class Test

{
int variable;
public:
void load_variable(int val) {variable = val;}
void get_variable (void) {return(variable);}
};

void main(void)
{
Test Object;
Object.load_variable(100);
cout << Object.get_variable();
}

 

#include <iostream.h>

class Test

{
int variable;

public:

void load_variable(int val) {this -> variable = val;}
void get_variable (void) {return(this -> variable);}
};

void main(void)

{
Test Object;
Object.load_variable(100);
cout << Object.get_variable();
}

 
Dec. 5, 1996

Static Data Members


Example:

class People

{

char name[30];
int age;
public:
static int total;
People();
~People();
Getit();

};

People :: People() {}
People :: ~People() {}

int People :: total = 1; \\ Function Definition

void main(void)

{
cout << People :: total << endl;
People :: total++;
cout << People :: total;
}

 
 



 


 


 
 

Dec. 5, 1996

Operator Overloading