CS201 IntroClassDataType F2007

CS201 IntroClassDataType F2007 -...

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

View Full Document Right Arrow Icon
bf44e225243b6630660ab9befc6e84973b2015d4.doc05/12/09 1 Introduction to class data type Problems or drawbacks with procedural programming 1) initialization of data not required 2) program can freely change data values which could lead to corruption 3) difficult to modify program 4) operations are limited C++ classes overcome these problems 1) constructor function automatically called to initialize class data 2) contain functions that provide interface to class 3) provides easy ways to implement "information hiding" calling program is not affected by changes in representation of class data. 4) can re-define operators to work with our class << >> - + Class data type - user defined type - class creator decides on contents of class Class definition Example: class Car { public: Car(); //constructor function has same name as class void fillCar(); //fill the car object with data string getModel(); //function to return model name void setModel(string pnm);//function to change model name int getMiles(); //function to return mileage void setMiles(int pmiles); //function to change mileage double getMpg (); //function to return the miles per gallon void setMpg(double pmpg); //function to assign a new value to mpg private: string model; //string for model name int miles; //miles the car has been driven double mpg; //miles per gallon }; //don't forget the semicolon
Background image of page 1

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

View Full DocumentRight Arrow Icon
bf44e225243b6630660ab9befc6e84973b2015d4.doc05/12/09 2 Things to note: - Can not initialize data members within class definition private section - Purpose of constructor function is to initialize class data - Public and private sections public: means the data and functions can be accessed by anything outside of the class, such as programs that use the class and other classes private: means the data and functions can only be accessed by the class’s member functions - The class contains functions plus data - contains public member functions which form the interface of the class - "clients" of the class call member functions to access to private data The client is any function that uses the class (often the main) - "get" functions return class data values - "set" functions change class data values Date example: (handout) - 3 parts - definition, implementation, calling program (client of class) 1) definition of class contains - public section first - our style (private could be first) - member function prototypes - data members 2) implementation of class member functions - Binary Scope Resolution Operator :: - ties function name to class name - not recognized as class function without it - other classes may have functions with the same name - Default constructor function - can be called with no arguments Date(); //prototype - intitializes data members to appropriate values - Second ( overloaded ) constructor function - requires 3 arguments Date(int, int, int); - initializes data members to the argument values 3) main program (client of the Date class)
Background image of page 2
bf44e225243b6630660ab9befc6e84973b2015d4.doc05/12/09 3 Contains: A)- declaration of objects (variables) of type
Background image of page 3

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

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

This note was uploaded on 04/18/2008 for the course CS 201 taught by Professor Conway during the Fall '07 term at Bowling Green.

Page1 / 15

CS201 IntroClassDataType F2007 -...

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

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