2. is a widely used programming methodology
First step in the problem-solving process is to
identify the components called objects, which
form the basis of the solution and to determine
how these objects interact with one another.
Example: write a program that automates the
video rental process for a local video store.
Identify Object: video and customer
Specify for each object the relevant data and
operations to be performed on that data.
3. Video
Data
▪ movie name
▪ starring actors
▪ producer
▪ production company
▪ number of copies in stock
Operations
▪ checking the name of the movie
▪ reducing the number of copies in stock by one after a copy is
rented
▪ incrementing the number of copies in stock by one after a
customer returns a particular video
4. An object combines data and operations into
a single unit
In OOD, the final program is a collection of
interacting objects
A programming language that implements
OOD is called an object-oriented
programming language.
5. is collection of fixed number of components
components of a class are called members of the
class
Syntax:
class classIdentifier
{
classMemberList
};
where: classMemberList consists of variable
declarations and/or functions. That is a member of a
class can be either a variable (to store data) or a
function.
6. If a member of a class is a variable, you declare it just like
any other variable. Also, in the definition of the class, you
cannot initialize a variable when you declare it.
If a member of a class is a function, you typically use the
function prototype to declare that member.
If a member of a class is a function, it can (directly) access
any member of the class – member variables and member
functions. That is, when you write the definition of a
member function, you can directly access any member
variable of the class without passing it as a parameter. The
only obvious condition is that you must declare an
identifier before you can use it.
7. Members of a class are classified into three
categories: private, public, protected
private, public and protected are reserved
words and are called member access specifiers
Facts about private and public members of a
class :
By default, all members of a class are private
If a member of a class is private, you cannot access it
outside the class
A public member is accessible outside the class
To make a member of a class public, you use the
member access specifier public with a colon, :.
8. Define a class to implement the time of day in a
program. Because a clock gives the time of day, let
use call this class ClockType
Data members to represent time: hours, minutes, and
seconds
Some operations on time
Set the time.
Print the time.
Increment the time by one second.
Increment the time by one minute.
Increment the time by one hour.
How many members are there in ClockType?
9. class ClockType
{
int hours;
int minutes;
int seconds;
public:
void setTime();
void printTime();
void incrementHours();
void incrementMinutes();
void incrementSeconds();
};
11. Syntax:
classObjectName.memberName
Example:
myClock.hours = 5; // if hours is public
myClock.printTime();
The class members that a class object can access
depend on where the object is declared.
If the object is declared in the definition of member
function of the class, then the object can access both
the public and private members.
If the object is declared elsewhere, then the object
can access only the public members of the class.
12. Not Allowed
You cannot use arithmetic operations on class objects
▪ Example: myClock + yourClock
You cannot use relational operators to compare two
class objects for equality
▪ Example: if(myClock == yourClock)
Allowed: myClock yourClock
Member access operator (.) hours 2 hours
▪ myClock.printTime(); minutes 26 minutes
Assignmner operator (=) seconds 47 seconds
▪ yourClock = myClock;
14. The following rules describe the relationship
between functions and classes:
Class objects can be passed as parameters to
functions and returned as function values
As parameters to functions, classes can be passed
either by value of by reference
If a class object is passed by value, the contents of
the member variables of the actual parameters
are copied into the corresponding member
variables of the formal parameter.
15. Note: If a variable is passed by values, the formal parameter copies the values of
the actual parameters.
void sum(int a, int b)
{
a = a + 5;
b = b * a; sum main
}
int main()
{ a x 3
int x,y;
x = 3;y=2; b y 2
cout<<x<<“ “<<y<<endl;
sum(x,y);
cout<<x<<“ “<<y<<endl;
}
16. Note: If a variable is passed by reference, the formal parameter receives only the
address of the actual parameters.
void sum(int& a, int& b)
{
a = a + 5;
b = b * a; sum main
}
int main() a x 3
{
int x,y;
x = 3;y=2; b y 2
cout<<x<<“ “<<y<<endl;
sum(x,y);
cout<<x<<“ “<<y<<endl;
}
17. How to pass object as a parameter to a function?
18. C++ has no fixed order in which you declare
public and private members; you can
declare them in any order. The only thing you
need to remember is that, by default, all
members of a class are private.
If you decide to declare private members
after the public members , you must use the
member access specifier private to begin the
declaration of the private member.
19.
20.
21. Categories of Member Functions
Mutator Function – a member function of a class that
modifies the value(s) of the member variable(s).
Accessor Function – a member function of a class
that only accesses (that is, does not modify the
value(s) of the member variable(s).
▪ As a safeguard, a reserved word const is added at the end of
the headings of the accessor function.
▪ Note: A constant member function of a class cannot modify the
member variables of that class.
▪ A member function of a class is called constant function if
its heading contains the reserved word const at the end.
▪ A constant member function of a class can only call other constant
member functions of that class.
22. Add another member function that simply
returns the values of the data members of the
class(hours, minutes and seconds).
This member function should prevent user
from modifying the values of member
variables of the class.
Be reminded that a function returns at most
one value.
23. What if printTime() is called right after
creating an object of ClockType()?
int main()
{
ClockType myClock;
myClock.printTime();
}
24. Is a special function whose main purpose is to
initialize the data members of a class
There are two types of constructors:
Constructors with parameters (parameterized
constructor)
Constructors without parameters (default
constructor)
25. The name of a constructor is the same as the
name of the class
A constructor, even though it is a function,
has no type. That is, it is neither a value-
returning function nor a void function.
A class can have more than one constructor.
However, all constructors of a class have the
same name.
26. If a class has more than one constructor, the
constructors must have different formal parameter
lists. That is, either they have a different number of
formal parameters or, if the number of formal
parameters is the same, then the data type of the
formal parameters, in the order list, must differ in at
least one position.
Constructors execute automatically when a class
object enters its scope. Because they have no types,
they cannot be called like other functions.
Which constructor executes depends on the types of
values passed to the class object when the class is
declared
31. Syntax:
className classObjectName(argument list);
Example:
ClockType myClock(7,19,11);
Note:
The number of arguments and their type( in the argument
list ) should match the formal parameters(in the order
given) of one of the constructors.
If the type of the argument does not match the formal
parameters of any constructor(in the order given), C++
used type conversion and looks for the best match. For
example, an integer value might be converted to a floating-
point value with a zero decimal part. Any ambiguity will
result in a compile-time error.
32. A constructor can also have default
parameters. In such cases, rules for declaring
formal parameters are the same as those for
declaring formal parameters in a function.
Moreover, actual parameters to a constructor
with default parameters are passed according
to the rules for functions with default
parameters.
33. Using ClockType class, we can replace both
constructors with:
ClockType(int=0, int=0, int=0);
34. class TestClass
{
public:
void print() const;
TestClass(int =0, int =0, double =0.0, char =‘*’);
private:
int x,y;
double z;
char ch;
};
37. If a class has no constructor(s), C++
automatically provides the default constructor.
However, the object declared is still
uninitialized.
If a class includes constructor(s) with
parameter(s) and does not include the default
constructor, C++ does not provide the default
constructor for the class. Therefore, when an
object of the class is declared, we must include
the appropriate arguments in the declaration.
39. 1. Design and implement a class DayType that implements the day of the week
in a program. The class should store the day such as Sun for “Sunday”. The
program should be able to perform the following operations on an object of
type DayType:
1. Set the day.
2. Print the day.
3. Return the day.
4. Return the next day.
5. Return the previous day.
6. Calculate and return the day by adding certain days to the current day. For
example, if the current day is Monday and we add 4 days, the day to be
returned is Friday. Similarly, if today is Tuesday, and we add 13 days, the day
to be returned is Monday.
7. Add the appropriate constructors.
2. Write the definitions if the functions to implement the operations for the
class DayType as defined in #1. Also, write a program to test various
operations on this class.