Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16 Oct 30, 02.

Similar presentations


Presentation on theme: "Lecture 16 Oct 30, 02."— Presentation transcript:

1 Lecture 16 Oct 30, 02

2 Recap Lec 15 We have a class. Class contains data and functions
Access specifiers - private and public Objects are created from a class. Class forms the blueprint for objects to be created with data members and functions

3 3 sections of C++ program
Class declaration Class implementation main() function (We also discussed about the constructor function)

4 Class implementation Date::Date(int mm, int dd, int yyyy) // constructor function date { month = mm; day = dd; year = yyyy; } void Date::setdate(int mm, int dd, int yyyy) // function setdate return; void Date::showdate(void) // function showdate cout<<month<<day<<year<<endl; return

5 Class implementation explanation
The member functions declared in declaration section are written. :: (scope resolution operator identifies the function as a member of a particular class). The class has 3 functions Date(), setdate(), showdate(). First function has same name as class, called constructor function. It has no return type. It assigns the data members month, day and year with the values of the parameters mm, dd & yyyy respectively.

6 Class implementation explanation contd...
The 2nd function void Date::setdate(int mm, int dd, int yyyy) defines this as the setdate() function belonging to the Date class (Date::). Return type void. It assigns the data members month, day & year with the value of its parameters. The 3rd function void Date::showdate(void) has no parameters, returns no value, and is a member of Date class. Used to display date.

7 main() function Int main() {
Date a, b, c(4, 1, 1998) /* (declare 3objects – initializes one of them) */ b.setdate(12,25,2002); // assign values to b’s data a.showdate(); // display object a’s values b.showdate(); // display object b’s values c.showdate(); // display object c’s values return 0; }

8 main() function explanation
a,b,c are objects of class Date Whenever a new object is defined, memory is allocated for the object and its data members are automatically initialized. This is done by an automatic call to the class constructor function. When object ‘a’ is defined, constructor function Date() and its default values are initialized But in object c, (4, 1, 1998) are initialized because it has parameters assigned.

9 main() function explanation contd...
Therefore, a.month = 7; a.day = 4; a.year = 2001; c.month = 4; c.day = 1; c.year = 1998; b.setdate(12, 25, 2002) calls b’s setdate function, which assigns the argument values 12, 25, 2002 Since all class functions are public, it is valid inside the main function


Download ppt "Lecture 16 Oct 30, 02."

Similar presentations


Ads by Google