Regarding assignment 1 Style standards Program organization

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

Operator Overloading Fundamentals
C++ Classes & Data Abstraction
C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Class and Objects.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
C++ fundamentals.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
1 CSC241: Object Oriented Programming Lecture No 07.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Case Study - Fractions Timothy Budd Oregon State University.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Inheritance One of the most powerful features of C++
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
11 Introduction to Object Oriented Programming (Continued) Cats.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Chapter 12 Classes and Abstraction
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Chapter Topics The Basics of a C++ Program Data Types
Classes C++ representation of an object
Chapter 1.2 Introduction to C++ Programming
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?
Abstract Data Types Programmer-created data types that specify
Andy Wang Object Oriented Programming in C++ COP 3330
Compilation and Debugging
Compilation and Debugging
Basic Elements of C++.
A First C++ Class – a Circle
Chapter 3: Using Methods, Classes, and Objects
Chapter 5 Function Basics
Basic Elements of C++ Chapter 2.
Introduction to Classes
Inheritance Basics Programming with Inheritance
Classes and Data Abstraction
Dr. Bhargavi Dept of CS CHRIST
Computer Programming with JAVA
Classes.
C++ Compilation Model C++ is a compiled language
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Chapter 9 Introduction To Classes
Class rational part2.
Chapter 1 c++ structure C++ Input / Output
Object Oriented programming
Lecture 8 Object Oriented Programming (OOP)
Chapter 11 Classes.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Regarding assignment 1 Style standards Program organization Staple all pages together computeCoin needed an assert Program organization No function prototypes No comments with function prototypes Structure chart No function call lines Errors in communication lines Should match function prototype

Choosing test cases What is the goal of testing? Test cases for homework 1?

Choosing test cases What is the goal of testing? Finding cases that make the program fail Test cases for homework 1? Boundary cases 0, 1, 99, 100 Input for which the output is known 87, several others Out of bounds -5, 209, …

Questions about homework 2???

Before Thursday’s class do zybook exercises chapter 9 (. 11 through

Cornerstones of oop Inheritance Polymorphism Encapsulation - main focus in CS240

Some differences between java classes and c++ classes A java class is defined and implemented in a single file A c++ class is defined in one file and implemented in another file Java objects are references C++ objects are values A c++ class can have methods that are overloaded operators Ex: MyObject == yourobject

The box class An object of the box class holds a single type of item A jewelry box A book box A bait box What can be done to a box? Create a box Find out the type of item stored in a box Set the type of item stored in a box

operations operations itemType itemType

The java class box public class Box { private String itemType; public Box( ) { } public Box (String theType) { itemType = theType; public setItemType (String theType) { itemType = the Type; public String getItemType ( ) { return itemType;

Syntax of C++ class statement class MyClass { public: //protypes for constructors //prototypes for operations (methods) accessible to user private: //data members needed to store value of an instance //prototypes for operations not accessible to user }; Put a class definition in a separate file with a .h extension

Header file for the c++ class box // box.h #include <string>; class Box { public: Box (); // default constructor Box (std::string theType); // constructor void setItemType (std::string theType); // set type of item to theType std:string getItemType ( ) const; // return type of item private: std::string itemType; };

Implementation file for c++ box class // file box.cpp #include “box.h” Box::Box() { } Box::Box(std::string theType) { itemType = theType; void Box::setItemType (std::string theType) { std::string Box::getItemType ( ) const { return itemType; What is the meaning of const?

Declaring class instances (objects) Java - objects are references Box myBox = new Box(); Box myBox = new Box(“books”); C++ - objects are values Box mybox (); Box mybox (“books”); Box* myboxptr = new box(“books”); (coming later)

Using the box class First write a program whose purpose is test the box class to be sure that its methods provide the expected behavior Test program needs to #include “box.h” Declare instances of type box Invoke all methods of the box class

Code is now spread over 3 files box.h - defines the class interface box.cpp - implements the class boxtester.cpp - program to test the behavior of the box class Both box.cpp and boxtester.cpp #include box.h Attempt to define a class twice results in a compiler error

preventing redefinition // box.h #ifndef _BOX_ #define _BOX_ #include <string>; class Box { ………… }; #endif

HOW DO WE CREATE AN EXECUTABLE FILE? box.cpp boxtester.cpp #include “box.h” box.o boxtester.o executable file compile link

The UNIX make program Designed to manage multifile projects Keeps track of changes made in source files Make looks for its instructions in a file named makefile (all lower case) A Makefile contains Comment lines – start with # Dependency lines followed by action lines An example: box.o: box.cpp box.h <- a dependency line g++ -c std=c++11 box.cpp <- an action line Action line must start with a tab!

A makefile boxProgram.exe: box.o boxtester.o g++ box.o boxtester.o -o boxProgram.exe box.o: box.cpp box.h g++ -c -std=c++11 box.cpp boxtester.o: boxtester.cpp box.h g++ -c -cstd=c++11 boxtester.cpp

Benefits of make Don’t have to type individual commands Will only recompile the files that have been changed since the last time make was executed Done by checking time stamps Has many other uses

A useful addition boxProgram.exe: box.o boxtester.o g++ box.o boxtester.o -o boxProgram.exe box.o: box.cpp box.h g++ -c -std=c++11 box.cpp boxtester.o: boxtester.cpp box.h g++ -c -cstd=c++11 boxtester.cpp clean: rm *.o *.exe

Future homework submission must include All .h and .cpp files needed to build the executable program A makefile with commands needed to build the executable program Any input files needed to run the program Nothing else No .o files No executable files

What is a fraction? Abstract view of a fraction object What operations are needed? What data members are needed? 2 / 5

A partial header file #ifndef _FRACTION_ #define _FRACTION_ #include <iostream> class Fraction{ public: Fraction(): // creates a Fraction with the value 1/1 Fraction(int initNumerator, int initDenominator) // creates a Fraction with the value initNumerator / initDenominator // precondition: initDenominator != 0 int getDenominator()const; // returns the denominator int getNumerator()const; // returns the numerator double getFloatValue()const; // returns fraction’s float value private: int num; int denom; }; #endif

a method to add 2 fractions Fraction operator+ (const Fraction & rhsOperand) const; // returns the result of adding this Fraction to rhsOperand in fraction.h Fraction Fraction::operator+ (const Fraction & rhsOperand) const ( int sumNum = (num * rhsOperand.denom) + (denom * rhsOperand.num); int sumDenom = denom * rhsOperand.denom; return Fraction(sumNum, sumDenom); } in fraction.cpp

Fractions should be kept in simplest form How is a fraction simplified? Whose job is this?

Fractions should be kept in simplest form How is a fraction simplified? Divide numerator and denominator by their greatest common factor Whose job is this? Best done when a fraction is being created

What is greatestCommonFactor? Fraction::Fraction (int initNumerator, int initDenominator) { assert (initDenominaotr != 0); int gcf = greatestCommonFactor (initNumerator, initDenominator); num = initNumerator / gcf; denom = initDenominaotr / gcf; } What is greatestCommonFactor?

Overloading << Cannot be defined as a method – why? Fraction object would have to be the left hand operand myFraction << cout instead of cout << myFraction Define it as a friend function a Friend function has access to private data members Prototype must be defined in the class definition (.h file)

friend std::ostream& operator<< (std::ostream & out, const Fraction & f); // sends the value of f to out in fraction.h std::ostream & operator<< (std::ostream & out, const Fraction & f) { out << f.num << “/” << f.denom; return out; } in fraction.cpp cout << myFraction << endl; and myOutFile << myFraction << endl; both work in program that uses Fraction class

Before Tuesday’s class do zybook exercises chapter 10 (.1 through .6)