L15 - OO Design - Object Oriented Design Where do we stand...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Object Oriented Design Where do we stand now? • Class is a “blueprint” for objects – an ADT • Class Definition – placed in header file – Indicates its name along with: • Its data members or instance variables (declarations of these) • Its methods (prototypes for these) • Indication as to the accessibility of each member element (public, private, protected) – This defines the interfaces – Usually does not contain any code for implementation of the methods • Definition of methods – Placed in a source file (that is, a .cpp file) – Syntax is <classname>::<methodname> ( <args>){…..}; // header file for the weather class in weather.h #pragma once // tells the compiler to process this header only once #include <iostream> using namespace System; using namespace std; class weather { public: weather(); // constructor void setTemps (int, int); // method to set the temperatures double avgTemp (); double degreeDays (); void printData (); // prints out the data members private: int maxtemp; int mintemp; // must be <= maxtemp }; Observation: • Notice that the header for the class weather has in it: #pragma once • this is to make sure that the definitions in the header only get processed once • Sometimes you may have seen the #ifndef construct used for this purpose • What’s the relationship?? – the C++ compiler in general complains if it finds the same name(s) defined more than once in the same scope – sometimes we have to include the same headers or definitions in multiple files, so we have to tell the compiler to ignore any definitions after the first – one way to do this is to use # pragma once to tell the compiler to only process a file one time – another way to do this is to use the following type structure: #ifndef WEATHER_H #define WEATHER_H <all the class definition constructs> #endif // method definitions for the weather class in weather.cpp #include "stdafx.h" #include <iostream> #include "weather.h" using namespace System; using namespace std; weather::weather() { setTemps (0, 0); } void weather::setTemps (int max, int min) { maxtemp = (max >= min ? max : min); mintemp = (min <= max ? min : max); } double weather::avgTemp () { return ((maxtemp - static_cast <double> (mintemp))/2.0 + mintemp); } double weather::degreeDays () { return (65 - ((maxtemp - static_cast <double>(mintemp))/2.0 + mintemp)); } void weather::printData() { cout << "\n the max temp is " << maxtemp << " and the min temp is " << mintemp << endl; } Notice the integrity checks in the methods here -- • the code for the constructor has checks inside it that forces max to be no less than min • there is another way to do checks at runtime – use the assert function – the function has the syntax: assert (<condition>) – what happens is that the condition is tested and if it is false, the program is terminated and you get an error message telling you where it terminated and something about why – you’ll need to #include <cassert> to use this function // This is a driver for a use of the class weather...
View Full Document

Page1 / 26

L15 - OO Design - Object Oriented Design Where do we stand...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online