0% found this document useful (0 votes)
35 views

Introduction To Programming

The document discusses classes and objects in C++, explaining that a class defines a user-defined data type with data members and member functions, and objects are instances of a class. It provides examples of defining a Date class with private data members for day, month, and year that are accessed via public member functions, and uses a constructor to initialize objects of the Date class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Introduction To Programming

The document discusses classes and objects in C++, explaining that a class defines a user-defined data type with data members and member functions, and objects are instances of a class. It provides examples of defining a Date class with private data members for day, month, and year that are accessed via public member functions, and uses a constructor to initialize objects of the Date class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Introduction to Programming

Lecture 26
Today’s Lecture
– Classes
– Object
struct
Class
 A class has
– data
– functions
Class
A Class is a user defined
data type.
Object

The instances of the


class are called Objects.
Structure of a class
class name_of_class
{

// definition of a class

}
Example 1
struct Date
{
int day ;
int month ;
int year ;
};

Date mydate ;
mydate.month = 1 ;
mydate.day = 21 ;
mydate.year = 1979 ;
Example 2
class Date
{
int day ;
int month ;
int year ;
};
Example 2
main ( )
{
Date mydate ;
/* manipulate the data members
mydate.day ;
mydate.month ;
mydate.year ;
*/
}
Example 2
class Date
{
int day ;
int month ;
int year ;
};
Example 2

main ( )
{
Date mydate;
mydate.month = 10 ; // Error
}
Private
Default visibility of
all data and function
inside a class is
private
Public
class Date
{
private :

// private data and functions

public :

// public data and functions


};
Date Class
class Date
{
int day ;
int month ;
int year ;
};
main ( )
{
Date mydate ;
mydate.month = 10 ; // illegal
}
Date Class
class Date
{
private :
int day , month , year ;
public :
setMonth ( ) ;
print ( ) ;
};
main ( )
{
Date mydate ;
mydate.setMonth ( 10 ) ;
mydate.print ( ) ;
}
Separation of Interface
from the Implementation.
Example 3
class Date
{
public :
void display ( ) ;
Date ( int day , int month , int year ) ;
private:
int day , month , year ;
};
Example 3
void Date :: display ( )
{
cout << day << “/ " << month << “/ " << year ;
}
Scope Resolution
Operator
Example 3
main ( )
{
Date mydate ;
mydate.display ( ) ;
}
Example
class Date
3: Modified
{
public :
Date ( int month , int day , int year ) ;
void display ( ) ;
setDay ( int ) ;
setMonth ( int ) ;
setYear ( int ) ;
private :
int month , day , year ;
};
Example 3: Modified
main ( )
{
Date mydate ;
mydate.setDay ( 10 ) ;
}
Example 3: Modified
void Date :: setDay ( int i )
{
day = i ;
}
Example 3: Modified
main ( )
{
Date date1 , date2 , date3 ;
date1.setMonth ( 10 ) ;
date2.display ( ) ;
}
Constructor
Example 3: Modified

class Date
{
public :
Date ( int month , int day , int year ) ;
void display ( ) ;
private :
int month , day , year ;
};
Date :: Date ( int month , int day , int year )
{
// Body of the function

}
Example 3: Modified
main ( )
{
Date mydate ( 1 , 1 ,2002 ) ;
mydate.display ( ) ;
}
Date :: Date ( int day , int month , int year = 2002 )
Example 3: Modified
main ( )
{
Date mydate ( 1 , 1 ,2002 ) ;
Date mydate ( 1 , 1 ) ;
}

You might also like