You are on page 1of 23

OBJECT ORIENTED

PROGRAMMING

Unit – I
TOPICS FOR TODAY

 Objects

 Array of Objects

 Pointer to Objects

 Pointer to members of a class

 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

class Rectangle main()


{ {
Rectangle r1;
private: Rectangle r2;
int width;
int length; r1.set(5, 8);
cout<<r1.area()<<endl;
public:
void set(int w, int l); r2.set(8,10);
cout<<r2.area()<<endl;
int area(); }
};
4
ARRAY OF OBJECTS

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

 Section 1 – Asterik (*) and dot combination

 Section 2 – Arrow notation

 Section 3 – has two new operators

( .* ) and (-> *) : called as pointer to member operators

 They are used to refer to the data member through pointer

variable

9
CONSTANT OBJECTS

 Constant objects are those that are not modifiable


 Constant object is not allowed to perform operations that
modify it and only functions that are defined as const can
be accessed by it

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
};

void Rectangle :: set (int w, int l)


inline {
r1.set(5,8);
width = w;
rp->set(8,10); length = l;
20
scope operator
}
DEFAULT ARGUMENTS
void add(int NumberOne = 100, int NumberTwo = 200)
{
cout << "Sum is " << NumberOne + NumberTwo << endl;
}
void main()
{
int Int1 = 5, Int2 = 7;
add();
add(Int1);
add(Int1, Int2);
/* If we want to provide only the second argument, it will not work */
// The following will not work as expected
add(Int2);
getchar();
21
}
DEFAULT ARGUMENTS - STATIC
static int Next = 0;
int getNext(int NextNumber = Next)
{
Next++;
return NextNumber;
}
int main()
{
cout << getNext() << endl;
cout << getNext() << endl;
cout << getNext() << endl;
getchar();
} 22
THANK YOU

23

You might also like