Professional Documents
Culture Documents
PROGRAMMING
Unit – I
TOPICS FOR TODAY
Objects
Array of Objects
Pointer to Objects
Constant objects
Nested Classes
2
Local Classes
OBJECTS
An Object is an instance of a Class.
When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is
allocated.
To use the data and access functions defined in the class,
you need to create objects.
Syntax:
ClassName ObjectName;
3
DECLARATION OF AN OBJECT
5
POINTER TO OBJECTS
Pointer is a variable that stores addresses
Pointers can also have an address for an object and they
are called Pointer to objects
Pointer increment will increment the address by the size of
the object (except static data elements)
6
POINTER TO OBJECTS
7
POINTER TO MEMBERS OF A
CLASS
8
POINTER TO MEMBERS OF A
CLASS
variable
9
CONSTANT OBJECTS
10
CONSTANT OBJECTS
11
NESTED CLASS
class Outside
{ public:
/* The nested class (Inside) is declared public, which makes it accessible to objects */
class Inside
{
int InsideInt;
public:
void SetInsideInt(int TempInside)
{
InsideInt = TempInside;
}
}; // This class is now accessible to objects
int OutsideInt;
Inside InsideObject;
void UseInside()
{
InsideObject.SetInsideInt(OutsideInt);
cout << OutsideInt;
}
12
};
NESTED CLASS
int main()
{
Outside OuterClass;
OuterClass.OutsideInt = 5;
OuterClass.UseInside();
Outside::Inside InnerPublicClass;
InnerPublicClass.SetInsideInt(5);
13
LOCAL CLASS
Local Classes are defined within a function
The class is not visible outside the function.
The scope of the class is confined to the body of the
function.
The function in which the class is defined is not a member
function of the class. It cannot access the private variables
of the class.
14
LOCAL CLASS
15
LOCAL CLASS - RESTRICTIONS
They cannot use auto local variables of the container function
They can access static local variables or external variables
(globally defined) of the container function.
They cannot contain static members themselves.
They cannot define their functions outside the class boundaries.
Here, the PrintDetails() function must be defined inside the
class.
Their private variables are not available to the container function
unless declared as a friend.
16
FUNCTIONS
int GlobalVariable
int MyFunction(int FirstArgument, string SecondArgument)
{
int FunctionVariable;
// Remaining body of the function
}
int main()
{
int MainVariable, FirstValue, SecondValue;
// Other statements
int Result = MyFunction(FirstValue, SecondValue);
/* This is the function call */
cout << Result;
/* This is the next immediate statement after function call */
17
// Other statements
}
FUNCTIONS
How function call is made ?
main() calls MyFunction(). When this call is made, the object code of
the function MyFunction() is loaded in the main memory (knows as
stack).
the control from main() has been transferred to the function
MyFunction().
Global variables and function local variables are accessible
When the function execution is over, the control is transferred back to
main()
Local variables of the function will not be accessible
when a function call is made, the context of the calling function, that
is, the CPU register values and the values of the variables, are to be
stored, and when the called function is over, they have to be restored.
This process is known as context switching.
18
INLINE FUNCTIONS
functions can be defined either inside or outside the class
If they are defined inside the class, they are treated as inline
functions
The body of inline functions replaces the function call statement
and thus avoids context switching
The function body replaces the function call statement wherever
it appears in the program body
an inline function must be defined completely before its call, as
the entire body is needed to be replaced with the call.
19
INLINE FUNCTIONS
class Rectangle
{
private:
int width, length;
class name
public:
void set (int w, int l);
int area() {return width*length; } member function name
};
23