CECS 130 Final EXAM. #include Using a directive to include a header file Stdio.h = standard input...

148
CECS 130 Final EXAM

Transcript of CECS 130 Final EXAM. #include Using a directive to include a header file Stdio.h = standard input...

Page 1: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

CECS 130 Final EXAM

Page 2: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h> Using a directive to include a header file

Stdio.h = standard input output header file

Page 3: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Definition: Escape sequences are specially sequenced characters used to format output

\” Ex: printf(“ \ “This is quoted text \ “ “)

\’Ex: printf(“ \n A single quote looks like \’ \

n”);

\* *\ Comment Block

Page 4: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

To declare a constant (read only) value:

const int x = 20const float PI = 3.14

Page 5: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

TYPE SIZE VALUES

bool 1 byte true (1) or false (0)

char 1 byte ‘a’ to‘z’ , ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on

int 4 bytes -2,147,483,648 to 2,147,483,647

short 2 bytes -32,768 to 32,767

long 4 bytes -2,147,483,648 to 2,147,483,647

float 4 bytes + - (1.2 x 10^-38 to 3.4 x 10^38)

double 8 bytes +- (2.3 x 10^-308 to -1.7 x 10^308)

Page 6: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Can you explain what the code is doing?

Page 7: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 8: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 9: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Syntax:

scanf(“conversion specifier”, variable);

Page 10: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 11: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Do you know the answers to these?

A. !( 1 || 0 )

B. !( 1 || 1 && 0 )

C. !( ( 1 || 0 ) && 0 )

Page 12: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

A. !( 1 || 0 ) ANSWER: 0

B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)

C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

Page 13: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Logical operators:

Syntax:

Page 14: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Else if:

Page 15: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Can you write code that will ask a user to enter a number 1 , 2 , or 3 and print out the following:

Page 16: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 17: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 18: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 19: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

while ( condition ) { Code to execute while the condition is true }

Quiz: Can you write a program that prints xwhile x increments from 0 to 10?

Page 20: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 21: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

do { } while ( condition );

What is the main difference between “Do while” and “while”?

Page 22: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

while ( condition ) { Code to execute while the condition is true }

do { } while ( condition );

Do{} while() executes code at least once!

Page 23: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Use when the number of iterations is already known

Syntax:

Page 24: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h>

int main() { int x;

for ( x = 0; x < 10; x++ ) { printf( "%d\n", x );

} getchar(); }

Page 25: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Write a program using a FOR Loop to display all of the multiples of 5 from 0 to 100.

Page 26: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h>

int main() { int x; for ( x = 0; x < =20; x++ ){

printf( "%d\n", x*5 ); }

getchar(); }

Page 27: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

x++; x--; Postfix++x; --x; Prefixmain(){ int x = 0;

int y = 0;printf(“\n The value of y is %d \n”, y++);printf(“\n The value of x is %d \n”, ++x);

}

Answer:01

Page 28: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Use to manipulate flow in loops

What does a Break statement do when executed within a loop?

What does a Continue statement do when executed within a loop?

Page 29: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h>

main() { int x; for ( x = 10; x >5; x-- )

{ if (x==7) break;

} printf( “\n %d \n ”, x );}

#include <stdio.h>

main() { int x; for ( x = 10; x >5; x-- ) { if (x==7)

continue; printf( “\n %d \n ”, x );

}}

kmnich01
"In this program, the condition (x==7) becomes true after the third iteration. Next, the break statement is executed and program control is sent out from the for loop and continues with the printf statement"(Vine)
kmnich01
7 is not included in the output because once x==7 is true, the rest of the statements are skipped and the next iteration is executed.
Page 30: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Function Prototype Syntax

return-type function_name ( arg_type arg1, ..., arg_type argN )

Function Prototypes tell you the data type returned by the function, the data type of parameters, how many parameters, and the order of parameters

Function definitions implement the function prototype

Where are function prototypes located in the program?

Where do you find function definitions?

Page 31: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Where are function prototypes located in the program? Answer: before the main(){} Function!

Function Definitions are self contained outside of the main(){} function

Page 32: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h>int mult ( int, int);

int main() { int x; int y; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n", mult( x,y ) ); getchar(); }

int mult (int a, int b) { return a * b;}

Page 33: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h> printNumber();

main(){

int x;printNumber(x);

}

void printNumber(){

printf(“\n The number is %d \n”, x)}

Page 34: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h> void printNumber( int x);

main(){

int x;printNumber(x);

}

void printNumber(int y){

printf(“\n The number is %d \n”, y);}

Page 35: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Functions - ExampleFunctions - Example

Page 36: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h>

void printNumbers();int iNumber;

main() { int x;

for(x=0, x<10,x++){printf(“\n Enter a number:”);scanf(“%d”, &iNumber);PrintNumbers();

}}

void printNumbers(){

printf(“\n Your number is: %d \n”, iNumber);}

Page 37: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Variable scope defines the life time of a variable

Local Scope: defined within functions and loses scope after function is finished.

Global Scope: defined outside of functions and can be accessed by multiple functions

Page 38: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 39: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Can you write code that asks a user to:

1. Input 4 integers. 1. Adds the numbers together in one function.

1. Multiplies them in another.

1. Prints out the absolute difference between the sum and product?

1. The user should have to do this 5 times.

Page 40: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 41: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Syntax (One-dimensional Array):

array_type array_name [number-of-elements];

Two Dimensional Array array_type array_name [#ROWS] [#COLUMNS];

Page 42: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Can you declare a one-dimensional array made up of 10 integers? Answer: int iArray[10]

How to declare an Array int iArray[10]; float fAverages[30]; double dResults[3]; short sSalaries [9]; char cName[19]; //18 characters and 1 null

character

Page 43: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Array index

Page 44: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h>

main(){

int x;int iArray[5];

for( x=0; x < 5 ; x++) { iArray[x] = 0;}

}

Page 45: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Can you add code to print out the values of the program below?

#include <stdio.h> main(){

int x;int iArray[5];

for( x=0; x < 5 ; x++) { iArray[x] = 0;}

}

Page 46: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h> main(){ int x; int iArray[5];

for( x=0; x < 5 ; x++) { iArray[x] = 0; }

for(x=0 ; x<5; x++) { printf(“\n The value of iArray index %d is %d \n”, x, iArray[x]); }}

Page 47: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Arrays - Example

Page 48: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Multidimensional Arrays Multidimensional Arrays

Page 49: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Multidimensional Arrays Multidimensional Arrays

Page 50: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Multidimensional Arrays - Example Multidimensional Arrays - Example

Page 51: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

How do you search through an array?

Page 52: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Array Search - Answer Array Search - Answer

Page 53: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Can you make a program to sort an array of 10 integers either ascending or descending order?

Consider you have the array[10]={7,8,5,3,0,3,2,1,4,10}Write a code to do the sorting.

Page 54: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Array Sort - Answer Array Sort - Answer

Page 55: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

When determining the maximum length your string variable needs to be it is important to consider a NULL Character: “\0”

char example[10]=“REACH”;

example[0] -> Rexample[1] -> Eexample[2] -> Aexample[3] -> Cexample[4] -> Hexample[5] -> \0

Page 56: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 57: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 58: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 59: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Data StructuresData Structures

A group of data elements goruped together under one name.

Syntax:

To create a single structure use this syntax:

To Access a struct member use the dot (.) operator.

struct type_name {member_type1 member_name1;member_type2 member_name2;member_type3 member_name3;

};

struct_name name_of_single_strcuture;

name_of_single_structure.name_of_variable;

Page 60: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Data StructuresData Structures

typedef can also be used to define struct as follows:

Example:

Typedef struct {member_type1 member_name1;member_type2 member_name2;member_type3 member_name3;

} struct_name;

typedef struct { char name[64]; char course[128]; int age; int year; } student;

Page 61: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Data StructuresData Structures

Example:

Page 62: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Data StructuresData Structures

If you wish to have a pointer to structure to access its information, use -> operator:

Page 63: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Structures as Functions ArgumentsStructures as Functions Arguments

OUTPUT:

Page 64: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 65: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer indirectly references a value. Referencing a value through a pointer is called indirection. A pointer variable must be declared before it can be used. ALL Arrays are Pointers!

Page 66: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Examples of pointer declarations:FILE *fptr; // fptr is a pointer to a fileint *a; // a is a pointer to an integerfloat *b; //b is a pointer to a floatchar *c; //c is a pointer to a character

The asterisk (*), when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data that the pointer points to, but NOT the name of the variable pointed to.

Page 67: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

When is & used?

When is * used?

& -- "address operator" which gives or produces the memory address of a data variable

* -- "dereferencing operator" which provides the contents in the memory location specified by a pointer

Page 68: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Reference operator (&)

Dereference operator (*)

Page 69: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Example:

Page 70: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 71: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 72: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

8

Page 73: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

• Dynamic memory allocation in C:

The process of allocating memory during program execution is called dynamic memory allocation.

• Dynamic memory allocation functions in C:

C language offers 4 dynamic memory allocation functions. They are,

1. malloc()

1. calloc()

1. realloc()

1. free()

Page 74: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 75: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

malloc():

• Used to allocate space in memory during the execution of the program.

• Does not initialize the memory allocated during execution.  It carries garbage value.

• Returns null pointer if it couldn’t able to allocate requested amount of memory.

Page 76: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

realloc():

• Modifies the allocated memory size by malloc() and calloc() functions to new size.

• If enough space doesn’t exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the existing data to new block and then frees the old block.

calloc():

• Similar to malloc () except that calloc() initializes the allocated memory to zero but malloc() doesn’t.

Page 77: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

free():

• Frees the allocated memory by malloc(), calloc(), realloc () functions and returns the memory to the system.

sizeof():

• Gives the amount of storage, in bytes, required to store an object of the type of the operand.

Page 78: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

sizeof (char) = 1 sizeof (double) = 8 sizeof (float) = 4 sizeof (int) = 4 sizeof (long) = 4 sizeof (long long) = 8 sizeof (short) = 2 sizeof (void *) = 4

sizeof (char) = 1 sizeof (double) = 8 sizeof (float) = 4 sizeof (int) = 4 sizeof (long) = 4 sizeof (long long) = 8 sizeof (short) = 2 sizeof (void *) = 4

Data Types LengthData Types Length

Page 79: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Please enter how long your name is: 21Please enter your name: David

OUTPUT:Hello David

Please enter how long your name is: -7

OTPUT:Failed allocation memory

Page 80: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 81: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 82: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

int *n;int * n1;n=( int * ) calloc(5, sizeof(int)); // Reserves a block of memory for 5 integers

//Decide you need to reallocate more memory later in the program

n1= (int *) realloc(n, 10 * sizeof(int)); //allocate 10 integers instead of 5if (n1!=NULL){

n=n1; }else printf("Out of memory!");

realloc() returns null if unable to complete or a pointer to the newly reallocated memory.

Page 83: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Do you know the syntax for each of these, used to read and write to data files?

Pointers: think of it as the memory address of the file

fopen()

fclose()

fscanf()

fprintf()

Page 84: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

fopen() returns a FILE pointer back to the pRead variable

Page 85: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 86: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 87: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Pretty basic.

Always close files when you use fopen.

Page 88: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Reads a single field from a data file

“%s” will read a series of characters until a white space is

found

can do

fscanf(pRead, “%s%s”, name, hobby);

Page 89: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Using fscanf()Using fscanf()

Page 90: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Kelly 11/12/86 6 LouisvilleAllen 04/05/77 49 AtlantaChelsea 03/30/90 12Charleston

Can you write a program that prints out the contents of this information.dat file?

Page 91: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

AnswerAnswer

Page 92: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like printf() as far as the format goes.

Page 93: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <stdio.h>

main(){

FILE *pWrite;

char fName[20];char lName [20];float gpa;

pWrite = fopen(“students.dat”,”w”);

if( pWrite == NULL )printf(“\nFile not opened\n”);

elseprintf(“\nEnter first name, last name, and GPA ”);printf(“separated by spaces:”);

scanf(“%s%s%f”, fName, lName, &gpa);fprintf(pWrite, “%s \t %s \t % .2f \n”, fName, lName, gpa);fclose(pWrite);

}

Page 94: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Can you write a program that asks the user for their Name Phone Number Bank account balance

and then prints this information to a data file called accounts.dat ?

Page 95: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

AnswerAnswer

Page 96: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

CPP – Basic Input/Output CPP – Basic Input/Output

Page 97: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

CPP – Basic Input/Output CPP – Basic Input/Output

Page 98: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Basic Input/Output – New lineBasic Input/Output – New line

Page 99: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Basic Input/Output – New lineBasic Input/Output – New line

Page 100: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Basic Input/Output – New lineBasic Input/Output – New line

Page 101: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Basic Input/Output – New lineBasic Input/Output – New line

Page 102: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Basic Input/Output – New lineBasic Input/Output – New line

Page 103: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Function OverloadingFunction Overloading

Page 104: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

void swap (int *a, int *b) ;

void swap (float *c, float *d) ;

void swap (char *p, char *q) ;

The other way is to have different number of input parameters for the function

Page 105: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include<iostream.h>

int area(int x); // square areaint area(int x,int y); //triangle areafloat area(int x,int y, int radius); //circle area

int main(){ int x=4, y=5, rad=3; cout<<"The Square area is :"<<area(x); cout<<"\nThe Triangle area is :"<<area(x,y); cout<<"\nThe Circle area is :"<<area(x,y,rad); getchar(); return 0;}

int area(int x) // square area{ return x*x; }

int area(int x,int y ) //triangle area{ return x*y; }float area(int x,int y, int radius) //circle area{ return radius*radius*3.14; }

Output:The Square area is: 16The Triangle area is :20The Circle area is: 28.26

Page 106: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

int boxVolume(int length = 1, int width = 1,int height = 1) {return (length * width * height);

}

If the function was called with no parameters then the default values will be used.

Page 107: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Used to go out one level.

If you have a global and local variables with same name, and need to call global from local scope then you need to use:

::VariableName

Page 108: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 109: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

All your declared variables are automatic.

Static variables keep there values as long as they exist.

Page 110: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Classes are general models from which you can create objects.

Classes have data members either data types or methods.

Classes should contain a constructor method and a destructor method.

An object is an instantiation of a class.

Objects have state and behavior.

Classes and ObjectsClasses and Objects

Page 111: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

class ClassName{

memberList};

memberList can be either data memberdeclarations or method declarations.

Page 112: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Class Bow{

//data member declarationsstring color;bool drawn;int numOfArrows;

Bow(string aColor); //constructor~Bow(); //destructor

//methodsvoid draw();int fire();

};

Page 113: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Return_type ClassName::methodName(argumentList){

methodImplementation}

Page 114: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

//draws the bow

void Bow::draw(){

drawn = true;cout<< “The “<<color<<“bow has been drawn.”<<endl;

}

Class nameMethod name

Page 115: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 116: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 117: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

A class constructor is a special member function of a class that is executed whenever we create new objects of the class.

The constructor will have exact same name as the class and it doesn't have return type even void.

They can be useful for setting initial values for certain member variables

A default constructor does not have any parameter, but if you need it can have parameters.

Constructor & DestructorConstructor & Destructor

Page 118: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Constructor & DestructorConstructor & Destructor

Page 119: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

A destructor is a special member function of a class that is executed whenever an object of its goes out of scope.

The destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.

Constructor & DestructorConstructor & Destructor

Page 120: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Constructor & DestructorConstructor & Destructor

Page 121: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

An inline function is one in which the function code replaces the function call directly.

#include <stdio.h>inline void test(void){ puts("Hello!");}

int main (){ test(); // This will be replaced with puts("Hello!") on run

time return 0;}

Page 122: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Friend declarations introduce extra coupling between classes.

A friend function is permitted full access to private an protected members of a class.

Once an object is declared as a friend, it has access to all non-public members as if they were public.

A friend function of a class is defined outside of that class's scope.

Page 123: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 124: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

24

Page 125: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Can be a set of classes, objects, and functions that are included within the namespace.

Page 126: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 127: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Operator OverloadingOperator Overloading

You can redefine or overload the function of most built-in operators in C++.

An overloaded operator is called an operator function .

You can declare an operator function with the keyword operator preceding the operator.

The format is:

type operator sign (parameters) { /* …. */ }

Page 128: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Operator OverloadingOperator Overloading

Here is a list of all operators that can be overloaded:

Page 129: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Operator OverloadingOperator OverloadingThis example illustrates overloading the plus (+) operator.

4,3 4,3

Page 130: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Operator OverloadingOperator Overloading

4,3

Page 131: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

class aClass // Base class{

public:int anInt;

}

class aDerivedClass : public aClass //Derived class

{protected:

float aFloat;};

Page 132: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 133: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <iostream.h>enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };class Mammal{public: Mammal(); // constructors ~Mammal(); //destructor //accessorsint GetAge()const;void SetAge(int);int GetWeight() const;void SetWeight();//Other methodsvoid Speak();void Sleep();protected:int itsAge;int itsWeight;};class Dog : public Mammal {public: Dog(); // Constructors~Dog(); // AccessorsBREED GetBreed() const; void SetBreed(BREED); // Other methods // WagTail(); // BegForFood(); protected: BREED itsBreed;};

Animals

Mammals

Reptiles

Horse Dog

HoundTerrie

r

Yorkie Cairn

Page 134: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Private members are not available to derived classes.

You could make itsAge and itsWeight public, but that is not desirable. You don't want other classes accessing these data members directly.

What you want is a designation that says, "Make these visible to this class and to classes that derive from this class." That designation is protected. Protected data members and functions are fully visible to derived classes, but are otherwise private.

Page 135: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Page 136: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

When do we need to override functions?

If you are a programmer example in your slides. If we consider “Woof” of the dog as speak example.

When a derived class creates a function with the same return type and signature as a member function in the base class, but with a new implementation, it is said to be overriding that method.

Page 137: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <iostream.h> enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };class Mammal {public:// constructors Mammal() { cout << "Mammal constructor...\n"; } ~Mammal() { cout << "Mammal destructor...\n"; }//Other methodsvoid Speak()const { cout << "Mammal sound!\n"; }void Sleep()const { cout << "shhh. I'm sleeping.\n"; } protected: int itsAge; int itsWeight; };

class Dog : public Mammal {public: // Constructors Dog(){ cout << "Dog constructor...\n"; } ~Dog(){ cout << "Dog destructor...\n"; } // Other methodsvoid WagTail() { cout << "Tail wagging...\n"; }void BegForFood() { cout << "Begging for food...\n"; }void Speak()const { cout << "Woof!\n"; } // This function is overriding the base class Speak() function private: BREED itsBreed;};int main() { Mammal bigAnimal; Dog fido; bigAnimal.Speak(); fido.Speak(); getchar(); return 0;}

Page 138: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

When you overload a method, you create more than one method with the same name, but with a different signature. When you override a method, you create a method in a derived class with the same name as a method in the base class and the same signature.

Page 139: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

#include <iostream.h>class Mammal {public:void Move() const { cout << "Mammal move one step\n"; }void Move(int distance) const { cout << "Mammal move ";cout << distance <<" _steps.\n"; }protected:int itsAge;int itsWeight;};class Dog : public Mammal {public:// You may receive a warning that you are hiding a function!void Move() const { cout << "Dog move 5 steps.\n"; }}; int main() {Mammal bigAnimal;Dog fido;bigAnimal.Move();bigAnimal.Move(2);fido.Move(8);// can I do this?fido.Move();return 0;}

Output:Mammal move one stepMammal move 2 steps.Dog move 5 steps

Page 140: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Templates Templates

Templates are the foundation of generic programming.

Templates allow us to create function template whose functionality can be adapted to more than one type of class without repeating the entire code for each type.

Single function fo a whole family of similar functions.

The format is:

Where Type is the type of the data and declaration is either a function declaration of a class declaration.

Page 141: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Templates Templates

Page 142: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Function Template Generation Function Template Generation

Page 143: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Templates – Example Templates – Example

Page 144: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Class Templates Class Templates

Page 145: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Class Template – Example Class Template – Example

100

Page 146: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Recursion Recursion

Recursivity is the property that functions have to be called by themselves.

Solving the problem by reducing it to smaller version of itself.

Useful in tasks like sorting or calculating the factorial of numbers.

Factorial and Power are common examples.

Page 147: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Recursion Recursion

Notice here how the function calls itself.

Page 148: CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.

Good Luck from REACH in your Final Test.

Questions??Questions??