Classes and Constructors

Classes and Constructors - Lecture 3 Classes and...

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

View Full Document Right Arrow Icon
Lecture 3 Classes and Constructors
Background image of page 1

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

View Full DocumentRight Arrow Icon
Outline Class Design Interface, Member functions, Applications Constructors Classes defined in Standard Template library -- vector class Coverage 6.2 & Chapter 7
Background image of page 2
Defining Classes Class definition class Name { public: //constructor and member function prototypes private: //private function prototypes //private function used as auxiliary functions //only accessible in member functions //data member declarations };
Background image of page 3

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

View Full DocumentRight Arrow Icon
Example: DayOfYear Class class DayOfYear { public: DayOfYear(); //a default constructor void input(); //user input the month and day void output(); //output the month and day void set (int new_month, int new_day); //user set new date int get_month() const; //return the month int get_day() const; //return the day private: void check_date() const; //check the validation of the date int month; int day; //data members to store the data };
Background image of page 4
Member Functions void DayOfyear::input() { //note: uses <iostream> cout << “Enter the month as a number\n”; cin >> month; //store into the data member month cout << “Enter the day as a number\n”; cin >> day; //store into the data member day check_date(); //check if entered data is valid or not } void DayOfYear::set(int new_month, int new_day) { month = new_month; day=new_day; check_date(); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Using the Objects int main () { DayOfYear today, birthday; cout << “Enter today’s date\n”; today.input(); birthday.set(5,10); if(today.get_month()==birthday.get_month() && today.get_day() == birthday.get_day()) cout << “Happy Birthday to You\n”; return 0; }
Background image of page 6
May add const modifier/qualifier in member function definition Indicates this member function cannot alter any data member of the object calling this member function Example int get_day() const { day += 5; //wrong! return day;
Background image of page 7

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

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

Page1 / 29

Classes and Constructors - Lecture 3 Classes and...

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

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