You are on page 1of 5

Object Oriented Programming Lab Lab 3

Object Oriented Programming


Constructor
When the parameterized constructor is defined in class and class don’t have default constructor,
then instance without arguments can’t be created. We can either overload the constructor by
defining the default constructor.

Constructor with default values


Constructor can be defined with default values so if the arguments are not passed, members can be
initialized with default values.

Example

This-> Pointer
In above program, constructor have the word this. If we see the initialization of data members, the
class data members have same name as the functions parameters. The compiler can’t differentiate
so this pointer is used to point to class data member.

Scope Resolution :: Operator


Till now, we have declared and defined function within classes, but when the functionality in class
increases, it becomes difficult to manage all functions within class. We can declare functions within
class and define it outside the class. To differentiate between functions of different classes scope
resolution operator is used with class name. Syntax is given below

ReturnType ClassName :: FunctionName(Parameters)

1|Page Furqan Nasir


Object Oriented Programming Lab Lab 3

Example

Note: For Constructors Syntax will be

ClassName :: ClassName(Parameters)

Friend Function
One of the important concepts of OOP is data hiding, i.e., a non-member cannot access an object's
private or protected data.

But, sometimes this restriction may force programmer to write long and complex codes. So, there is
mechanism built in C++ programming to access private or protected data from non-member
functions.

This is done using a friend function or/and a friend class. Friend function is a special type of function,
which is declared inside the class. Friend function can access the private, protected and public data
of the class. A keyword friend is used before return type of the function declaration/prototype.

2|Page Furqan Nasir


Object Oriented Programming Lab Lab 3

Example

A function can be made friend of two classes. See the example.

Example

When a class is made a friend class, all the member functions of that class becomes friend functions.

class B;

class A

…….. // class B is a friend class of class A

friend class B;

3|Page Furqan Nasir


Object Oriented Programming Lab Lab 3

class B

... .. ...

All member functions of class B will be friend functions of class A. Thus, any member function of
class B can access the private and protected data of class A. But, member functions of class A cannot
access the data of class B.

Remember, friend relation in C++ is only granted, not taken.

Static Data Member


Static data members are class members that are declared using the static keyword. There is only one
copy of the static data member in the class, even if there are many class objects. This is because all
the objects share the static data member.

Ref: https://www.tutorialspoint.com/static-data-members-in-cplusplus

Static Member Function


A static member function is a special member function, which is used to access only static data
members, any other normal data member cannot be accessed through static member function. Just
like static data member, static member function is also a class function; it is not associated with any
class object.

4|Page Furqan Nasir


Object Oriented Programming Lab Lab 3

We can access a static member function with class name, by using following syntax:

class_name:: function_name(perameter);

Example

Ref:: https://www.includehelp.com/cpp-tutorial/static-member-function.aspx

5|Page Furqan Nasir

You might also like