Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Linked Structures, Project 1: Linked List Bryce Boe 2013/07/11 CS24, Summer 2013 C.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Complexity Bryce Boe 2013/10/21 CS24, Fall Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
More C++ Bryce Boe 2013/10/23 CS24, Fall Outline Project 1 Review Alignment and Padding Finish C++ Introduction.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Structures and Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Review: Two Programming Paradigms
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Lecture 9 Concepts of Programming Languages
Introduction to Classes
Learning Objectives Classes Constructors Principles of OOP
Constructors and Other Tools
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C

Outline IRB Consent Forms Project 1 Questions Individually Testing ADTs Big-O Examples C++ Introduction

PROJECT 1 QUESTIONS?

INDIVIDUAL TESTING

I expect that you are able write your own C program from scratch and compile the program are able to include a library and use its functions and compile the library can think of and write test cases for those functions

Review: The simplest C Program int main() { return 0; } Save as: simple.c (or something else) Compile via: clang simple.c

Review: A simple program to utilize the List ADT #include “array_list.h” int main() { struct List *tmp = list_construct(); // Insert testing code here (adding/removing items) list_destruct(list); return 0; } Save as: my_test_list.c Compile as: clang my_test_list.c array_list.c

Testing the list ADT

BIG-O REVIEW AND EXAMPLES

Recall: Common Ordered Complexities O(1) – constant time O(log(n)) – logarithmic time O(n) – linear time O(nlog(n)) – linearithmic time O(n 2 ) – quadratic time O(2 n ) – exponential time O(n!) – factorial time

O(?) int a[1024]; // assume allocated if (a[0] == a[1]) return a[2]; else return a[0]; O(1)

O(?) int a[4]; // assume allocated int n = sizeof(a) / sizeof(int); int sum = 0; for (int i = 0; i < n; ++i) sum += a[i]; return sum; O(n)

O(?) int a[4]; // assume allocated return a[0] + a[1] + a[2] + a[3]; O(1)

O(?) int a[1024]; // assume allocated int n = sizeof(a) / sizeof(int); int dups = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (a[i] == a[j]) ++dups; O(n 2 )

O(?) int a[1024]; // assume allocated int n = sizeof(a) / sizeof(int); int dups = 0; for (int i = 0; i < n; ++i) for (int j = i; j < n; ++j) if (a[i] == a[j]) ++dups; O(n 2 )

C++ INTRODUCTION

Why C++? Problems with C – Has a single global namespace – Cannot use the same name for functions with different types (e.g., min(int, int) and min(double, double)) – called overloading – Difficult to minimize source-code repetition for similar functions with different types

Some Differences #include  #include – Or if you want fprintf, etc use #include printf(“Hello\n”);  cout << “Hello\n”; Rather than defining a struct which only contains data, define a class which contains data and methods on the data throw exceptions rather than use return values to represent error cases

Classes Provide encapsulation – Combining a number of items, such as variables and functions, into a single package, such as an object of some class (or instance of the class)

Scope Resolution Operator ClassName::method_name Used to identify the scope, class in this case, that the method belongs to as there may be more than 1 instance of method_name Scope resolution isn’t necessary if you are also a member of that class

Data Hiding Declaring member (instance) variables as private, why? – Assists in separation of implementation and interface – Allows for input validation and state consistency

Declaring Private attributes class Date { int day; // this section is private by default int month; // though you should be explicit public: void output_date(); private: int year; };

Accessor methods Sometimes called getters Instance methods that return some data to indicate the state of the instance Typically prefixed with get_ int Date::get_day() { return day; }

Mutator methods Sometimes called setters Instance methods that update or modify the state of the instance Typically prefixed with set_ void Date::set_day(int d) { day = d; }

Overloading Instance Methods Defining methods of a class with the same name, but different parameters void Date::update_date(int d, int m, int y) {…} void Date::update_date(Date &other) {…}

Class Constructors A constructor is used to initialize an object It must: – Have the same name as the class – Not return a value Constructors should be declared public – To ponder: what does it mean to have a non- public constructor? Always define a default constructor

Example class Date { public: Date(int d, int m, int y); Date(); // default constructor private: int day, month, year; };

Two ways to initialize variables From the constructor declaration (implementation) Method 1: Initialize in the constructor initialization section Date::Date() : day(0), month(0), year(0) {} Method 2: In the method body Date::Date() { day = 0; month = 0; year = 0; }

Example Constructor Usage Date a (10, 10, 11); // use the 3 param constructor Date b; // correct use of default constructor Date c(); // incorrect use of default constructor // This is actually a function definition Date d = Date(); // valid, but inefficient

Anonymous Instances An instance that is not bound to a variable Date d = Date(); In the above example there are actually two instances of class Date – The first is represented by d – The second is the anonymous instance represented by Date() The assignment operator is used to transfer information from the anonymous instance to d

Abstract Data Types A formal specification of the separation of implementation and interface Developer can use ADTs without concern for their implementation Using classes, you can define your own ADTs – This allows for reusable code

Tips for writing ADTs Make all the member variables private attributes of the class Provide a well defined public interface to the class and don’t change it Make all helper functions private