Download presentation
Presentation is loading. Please wait.
1
CS 201(Introduction To Programming)
TAMOOR SAFDER BC
2
(Class and Objects) Class
Let’s have a look on the structure of a class. It is very similar to the struct keyword. Keyword class is used and the braces enclose the definition of the class i.e. class name_of_class { // definition of class } Object We have to use the name of the object, a dot operator and the data member of structure to be accessed. The data members are of normal data types like int, float, char etc. Other data types can also be used. Let’s consider an example of Date Class shown in the following statement. class Date int Day; int month; int year; };
3
constructor // constructor of class Date(); { Date(int,int); Date(int,int,int); } Destructor // defining the constructor // default constructor. setting the date to a default date Date:: Date() day = 1; month = 1; year = 1900;
4
Private public: void display(); // to display the date on the screen void setDay(int i); // setting the day void setMonth(int i); // setting the month void setYear(int i); // setting the year Public scopes private: { int day, month, year; };
5
int main() { Date date1, date2(12,12), date3(25,12,2002); // taking objects of Date class // displaying the dates on the screen date1.display(); date2.display(); date3.display(); } Following is the output of the above program. The default constructor is called The constructor with two arguments is called The constructor with three arguments is called The date is The date is The date is The object has destroyed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.