lecture2 - ClassesandC+ Somereview Importanttopics...

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

View Full Document Right Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 1 Classes and C++ Some review Important topics Inheritance Polymorphism
Background image of page 1

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

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 2 Suppose I want to describe a square What data do we need and what  types? Center (x and y), integers? Width, integer? So, what data structure  should I use to  describe a square?
Background image of page 2
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 3 A C++ Class for a Square – Contents of  CSquare.h #ifndef CSQUARE_H_ #define CSQUARE_H_ class CSquare { public:      CSquare() : mX(0), mY(0),  mWidth(100) {}      int GetX() const {return mX;}      void SetX(int x) {mX = x;}      int GetY() const {return mY;}      void SetY(int y) {mY = y;}      int GetWidth() const {return mWidth;}      void SetWidth(int w) {mWidth = w;} private:      int    mX;     // Center x      int     mY;     // Center y      int     mWidth; // Width of the square }; #endif /* CSQUARE_H_ */ Remember: For a class we have a: Header file:  CSquare.h   Implementation file:  CSquare.cpp At this point, CSquare.cpp is empty,  we don’t need anything there, yet.
Background image of page 3

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

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 4 What does this do? #ifndef CSQUARE_H_ #define CSQUARE_H_ ... #endif /* CSQUARE_H_ */
Background image of page 4
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 5 #include guard #ifndef CSQUARE_H_ #define CSQUARE_H_ ... #endif /* CSQUARE_H_ */ This is called a  #include guard You can only define the class  CSquare once. But, what if you  include more than one thing that,  in turn, includes CSquare.h?   Eclipse creates these for  you automatically. .. Another option: #pragma once The first time CSquare.h is  included, CSQUARE_H_ is  not defined, so the body of the  #ifndef is used. That defines  CSQUARE_H_. The second time CSquare.h is  included, CSQUARE_H_ is  defined, so the body of the  #ifndef is not  included and the  class is not redefined.  You need these on every header file.
Background image of page 5

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

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 6 CSquare class CSquare { public:      mWidth(100) {}      int GetX() const {return mX;}      void SetX(int x) {mX = x;}      int GetY() const {return mY;}      void SetY(int y) {mY = y;}      int GetWidth() const {return mWidth;}      void SetWidth(int w) {mWidth = w;} private:      int    mX;     // Center x      int     mY;     // Center y      int     mWidth; // Width of the square }; The name of the class Why the leading  C?
Background image of page 6
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 7 Objects vs. Classes What is the difference between an object and a  class?
Background image of page 7

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

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 8 Objects vs. Classes
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course CSE CSE 335 taught by Professor Owen during the Fall '10 term at Michigan State University.

Page1 / 37

lecture2 - ClassesandC+ Somereview Importanttopics...

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

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