Download presentation
Presentation is loading. Please wait.
1
CECS 130 Final Exam Review Session
By Academic Leadership Center (ALC) JB Speed School Rm. 107
2
Overview of Topics Arrays Pointers Functions Dynamic memory allocation
Files C++ Introduction Object Oriented Programming Concepts Inheritance Templates
3
Arrays An array is a simple way of grouping similar variables for easy access Examples of arrays include: a list of names, a collection of student grades, a list of phone numbers, a list of your last 10 known addresses An array can exist in a single dimension (1-dim) or multiple dimensions (n-dim) Remember, C-array indices start at 0 and end at n – 1 where n = number of elements in the array Hint for when to use an array: look out for words like: collection of, list of, group of An array name is also a pointer. It points to the beginning of the array When should we use a multidimensional array?
4
Pointers A pointer variable contains a memory address that points to another variable. A pointer should be initialized during declaration. & = address-of Which of the following are illegal initializations pointer variables? Int* somePtr = &20; Int* somePtr = 20; Int* somePtr = &anotherVariable; Int* somePtr = anotherVariable; %p can be used to print the memory addresses of pointers Pointers can be used to pass arguments by Reference
5
Functions A function is a block of code that performs a single task
They are especially useful for code reusability thus minimizing the probability of error from copying and pasting In C, we need to provide 2 parts in order to be able use a function: Function Prototype – describes the expected output type and anticipated input types (arguments) Function Definition – shows the implementation of the function i.e how the function carries out the task its been assigned A function could either be a void function or a value returning function A function could accept parameters by value (non-pointer variables) or by reference (pointer variables) a F(x) c b
6
Strings String is an array of data type char
To read and print strings, use the conversion specifier %s The string.h is called the string-handling library and contains some functions that can be used for string manipulation. Some of these functions include: Strlen() - returns length of a string Strcpy() - copies a string to another location Strcmp() – compares 2 strings and returns 0 if they are the same Strcat() - joins 2 strings together Tolower() – converts a string to lower case Toupper() – converts a string to upper case Strstr() – searches on string for the first occurrence of the other string
7
Dynamic Memory Allocation
Memory stacks are dynamic groupings of memory that grow and shrink as each program allocates and deallocates memory. They are used for storing contents of variables and parameters Heap memory is an area of unused memory that the OS manages Sizeof operator can be used to determine the memory requirements of arrays Malloc attempts to retrieve designated memory segments from the heap and returns a pointer that points to the beginning of the reserved memory Realloc provides a feature for expanding contiguous blocks of memory while preserving the original content Calloc initializes the memory reserved with 0s while malloc does not initialize the reserved memory Always remember to free the memory reserved by malloc by using free()
8
Dynamic Memory Allocation
Malloc Process: Step 1: create pointer of desired type int* values; values Step 2: reserve contiguous memory and point to the beginning of the reserved memory (like an array) values 01A values = (int*) malloc(80 * sizeof(int)) Step 3: Check if values == NULL. If it is, return 1 and exit the program Step n: remember to free the memory reserved by malloc using free(values) Realloc Process: Repeat steps 1, 2 and 3 from malloc process Step 4: Fill the reserved memory gotten from malloc Step 5: reserve more memory locations and call it temp int* temp = realloc(values, requested_spaces * sizeof(int)); Step 6: if (temp != NULL) { values = temp; } Step n: remember to free the memory reserved by malloc using free(values) 01A
9
Files FILE* is a pointer of type FILE STEPS:
Assign pointer to fopen(file_name, op_type) Check if pointer is pointing to a NULL address If yes, return 1 or terminate the program Else, while pointer is not pointing to the end of the file use fprintf to write to the file or fscanf to read from the file Use fclose to terminate the pointer Question What's the limitation of using fscanf? Is there an alternative?
10
C++ Introduction C++ means incremental C
It is an object-oriented language Files should be saved as .cpp Uses iostream rather than stdio.h Remember to include "using namespace std" in your code Uses cout and cin in place of printf and scanf For(int i = 0; i < 10; i ++) is accepted here Has a string data type Does not require function prototypes for functions
11
OOP Concepts A class is a blueprint from which objects can be made.
Class is a reserved keyword in C++ and can only be used for declaring a class Methods are functions and are declared using return_type classname::methodName(argument_list...). A method should be responsible for one thing and one thing only Constructors are automatically called when a new object is created. It initializes the data members and performs all other required initialization tasks. They have the same name as the class and have no return type Access specifiers: these control where the data members of your class can be accessed. They are: public and private Declaring an object: ClassName objectName = ClassName(argumentList) ObjectName.attributeName OR objectName.methodName(arguments)
12
OOP Concepts
13
OOP Concepts Static members are member variables or attributes that remains the same for all instances of a class e.g count of objects created so far Data Abstraction: process of hiding the data members and implementation of a class behind an interface so that users of the class don't corrupt the class Encapsulation: Each class should represent one specific thing or concept. Multiple classes then come together to represent combination of things or concepts Polymorphism
14
Inheritance Inheritance can be achieved in 2 main ways:
IS-A relationship: car IS-A type of vehicle (inheritance) HAS-A relationship: cat HAS-A digestive system (composition) Inheritance is the derivation of one class from another class derived_class_name : scope_modifier base_class_name e.g. class Cat : public Animal The protected scope modifier ensures an attribute or method can be accessed by its class and its derived classes A derived class can access: Its own members All global variables All public and protected members of its base class
15
Inheritance To override a function, create a function with the same signature in the derived class. Ensure to use the keyword virtual in front of functions belonging to the base class that will be overridden in the derived classes Abstract classes are used to define classes that describe abstract concepts e.g shape. All function in abstract classes are virtual and have no implementation of their own C++ supports multiple inheritance but it's strongly advised against because it gets messy quickly
16
Templates Rather than overloading functions to suit the different available datatypes, templates make it possible to use one function for multiple data types
17
Questions C code used in this test review session can be accessed here
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.