Object oriented programming concepts-Session 3
Object oriented programming concepts-Session 3
nl
O
se
U
tre
en
Session: 3 C
h
ec
O
Describe Visibility Modifiers
se
Explain Methods
U
tre
Explain Static data fields and Constant data fields
en
Describe Accessor, Mutator, and Forward declaration
C
h
ec
pt
rA
Fo
se
U
tre
Characteristics of object are enclosed within the
en
class as data members
C
h
ec
O
Code
dataSnippet
members
<access-modifier> class <class-name>
se
{ functions or behavior of an entity
U
public class Horse
<data-members>
{
tre
<methods>
string color; // data member
en
}The figure shows the structure of a class diagram.
public void display() // method
{
C
h
Console.WriteLine(color);
ec
}
pt
}
rA
Fo
O
characteristics.
se
U
tre
en
C
h
ec
pt
rA
Fo
O
using visibility modifiers.
se
U
tre
en
C
h
ec
pt
rA
Fo
O
se
Consists of the actions that an object or instance of a
U
class can perform on the data members
tre
Syntax
en
C
<access-modifier> <return-type> <method-
name> (<data-type> <parameter-name>,…)
h
ec
{
// statement1
pt
// statement2
rA
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 7
y
Code Snippet
nl
To access any member of a class, a copy of that class
O
public class
called the Horse
object of the class, has to be created
se
{
private string color; // attribute
U
public
The void setColor(string
figure shows col) // method
examples of two objects
tre
{
en
color = col;
}
static void Main() C
h
{
ec
h1.setColor(“Black”);
}
Fo
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 8
y
nl
Syntax
Used to construct an object of a class and initialize its
O
data members
se
Default or no-argument constructor
U
Specialized method that()has the same name as the
<constructor-name>
tre
{class name
// initialization statements
en
}Constructor arguments are used to initialize the fields
of an object at runtime C
h
ec
pt
rA
Fo
O
by all objects of the class
se
U
tre
Class that is declared as static, cannot be instantiated and
en
its members can be accessed directly using the class name
C
h
ec
O
and does not change later
se
Values of such fields can be fixed before the object of
U
the class is created
tre
en
The following C# code demonstrates the creation of a
constant. C
h
public class Horse
ec
{
pt
…..
Fo
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 11
y
nl
These are special methods in OOP that provide an easy
O
yet secure way of accessing a data field of a class
se
U
Accessor is the method through which the object can
tre
retrieve the value of a data field
en
C
Mutator is a method through which the object can
h
assign the value to a data field
ec
pt
O
file to be used at an earlier stage in the
prototype in C++.
se
#include <iostream.h> code without any conflict
// function prototype
U
void display(string col); // line1
tre
static void Main ()
{ It is a prior hint to the compiler that the
en
string color;
function definition or implementation has
cout << “Enterbeen
cin >>color ;
color
done C
“; later in the code
h
ec
display(color); // line 2
} // end of main()
pt
cout<<”Color is ”+ col;
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 13
y
nl
A class is a type or a software construct that encloses the data
O
members and of an entity into an enclosed structure.
se
A visibility modifier is used with a class, data field as well as a
U
tre
en
A method defines the behavior of a class and consists of the
actions that an object of a class can perform on the data
members. C
h
ec
O
copy and it is shared by all objects of a class.
se
A constant is a variable whose value is fixed during compilation
U
tre
en
Forward declaration of a function is a statement that informs the
compiler about the signature of the function without its
implementation. C
h
ec
pt
rA
Fo