Object Oriented Programming
Functions & Objects (Part 5)
1
Review
• Constructor
• Implicit & Explicit Constructor
• Default Copy Constructor
• Constructor Overloading
• Array of Objects
• Destructor
2
Objectives
• Object as Parameter
• Object as Return Type
• Static as Keyword
• Static Members
o Member Variable / Data Member
o Member functions
• Friend Functions
• Friend Classes
3
Object as Parameter
• Objects can be passed as parameters to the
member functions similarly as we can pass the other
simple variables.
4
Example to Pass Object to Function
Class Box{
private:
int length, width, height;
Public:
void setValues(int l, int w, int h);
bool equal(Box b){ //function receiving an object
//some line of code
}
};
Int main(){
Box b1, b2;
if(b1.equal(b2)){
//do something
}
}
5
Try it now
Lets See
Returning Object From Function
• Objects can be returned from a function of a class
7
Example of Returning an Object
Class Box{
private:
double length, width, height;
Public:
void setValues(double l, double w, double h);
Box getObject(){//function receiving an object
//some line of code
}
};
Int main(){
Box b1, b2;
b1.setValues(10.0, 20.0, 5.0);
b2 = b1.getObject();
}
8
Try it now
Lets See
Static as keyword
• Unluckily, the keyword static, though the word
means "unchanging“ has multiple and apparently
unrelated uses
• static can be used in three major contexts:
o inside a function or a block
o inside a class
o in front of a global variable
10
Static keyword inside a function
• The use of static inside a function is the simplest that
once the variable has been initialized, it remains in
memory until the end of the program.
• Means the variable sticks around, maintaining its
value, until the program completely ends.
void func(){
static int a;
}
11
Guess the output?
void func(){
static int a;
cout<<a++<<endl;
}
Int main(){
func();
func();
func();
}
12
Static inside a loop
• Static keyword can be used inside a loop to
prevent a variable from being reinitialized.
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
static int number_of_times = 0;
number_of_times++;
}
}
Guess What will be the output
13
Static inside a loop (Cont.)
• The trick is that the keyword static prevents re-
initialization of the variable.
• One feature of using a static keyword is that it
automatically initialized a variable to zero
o but don't rely on this behavior (it makes your
intention unclear).
14
Static inside a Class
Both member (variables and methods)
can be static inside a class
15
Static variables inside a Class
• All other variables declared inside a class are
basically the property of the object of that class
o That means for each instance of a class, the variable can
have a different value
• While a static member variable has the same value in
any instance (object) of the class
• And doesn't even require an instance (object) of the
class to exist
16
Static variables inside a Class
(Cont.)
• Important detail to keep in mind:
o when debugging or implementing a program using a static
class member is that you cannot initialize the static class
member inside of the class.
o Means declare inside the class and initialize outside the class
similar to the way we define function outside of class.
o Syntax: Scope Resolution Operator
• type class_name::static_variable = value;
17
Example of Static member variable
In main.cpp file
class Box{
private:
static int count; //correct syntax
public:
static int count2; //correct syntax
};
int Box::count = 0; //correct syntax
main(){
cout<<Box::count; //Error because it is private
cout<<Box::count; //Ok because it is public
}
18
Try it now
Lets See
Static methods inside a Class
• You can also have static member functions of a class
o By declaring a function member as static, you make it
independent of any particular object of the class.
• Static member functions are functions that do not
require an instance of the class
• You access static member function with the class
name rather than an instance name using the scope
resolution operator
• However the static member can be accessed with
instance name.
20
Example Static methods
class Box
{
public:
static int objectCount; // static variable declaration
// Constructor definition
Box(double, double, double);
double Volume();
static int getCount(); // static method declaration
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
21
Example Static methods (cont.)
// Variable initialization
int Box::objectCount = 0; // we will not static here
// Constructor definition
Box::Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
22
Example Static methods (cont.)
double Box::Volume()
{
return length * breadth * height;
}
int Box::getCount() // we will not static here
{
return objectCount;
}
23
Example Static methods (cont.)
int main(){
Box b1, b2;
//Static method didn’t required the object to access it
cout<<"Object count"<<Box::getCount();
return 0;
}
24
Try it now
Lets See
Your Turn
Class Assignment
Friend Functions & Classes
• We Will explore it in our next lecture.
27
Reading Material
• Chapter 9
o C++ How to Program, 10th Edition, by Paul Deitel and
Harvey Deitel
• Chapter 6
o Object Oriented Programming in C++, 4th Edition, by Robert
Lafore
28