CECS 130 Final Exam Review Session

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Chapter 7: User-Defined Functions II
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ fundamentals.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Introduction to Object-Oriented Programming Lesson 2.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Classes, Interfaces and Packages
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Object-Oriented Programming (OOP) and C++
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C ++ MULTIPLE CHOICE QUESTION
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 7: User-Defined Functions II
By Muhammad Waris Zargar
Introduction to C++ Systems Programming.
Andy Wang Object Oriented Programming in C++ COP 3330
Quiz 11/15/16 – C functions, arrays and strings
Review: Two Programming Paradigms
C++, OBJECT ORIENTED PROGRAMMING
Class: Special Topics Copy Constructors Static members Friends this
Object-Orientated Programming
Arrays in C.
Lecture 6 C++ Programming
(5 - 1) Object-Oriented Programming (OOP) and C++
Object Oriented Programming COP3330 / CGS5409
Introduction to Classes
Corresponds with Chapter 7
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Pointers and Pointer-Based Strings
Dynamic Memory Allocation
Constructors and Other Tools
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
(5 - 1) Object-Oriented Programming (OOP) and C++
COP 3330 Object-oriented Programming in C++
CECS 220: OOP design with java
CIS 199 Final Review.
Programming Languages and Paradigms
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Final Exam Review Inheritance Template Functions and Classes
Pointers, Dynamic Data, and Reference Types
C++ Object Oriented 1.
Introduction to Classes and Objects
Presentation transcript:

CECS 130 Final Exam Review Session By Academic Leadership Center (ALC) JB Speed School Rm. 107

Overview of Topics Arrays Pointers Functions Dynamic memory allocation Files C++ Introduction Object Oriented Programming Concepts Inheritance Templates

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?

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

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

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

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()

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 0 1 77 78 79 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

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?

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

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)

OOP Concepts

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

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

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 

Templates Rather than overloading functions to suit the different available datatypes, templates make it possible to use one function for multiple data types

Questions C code used in this test review session can be accessed here