Professional Documents
Culture Documents
W6 - Objects As Parameters & Static Members
W6 - Objects As Parameters & Static Members
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
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.
13
Static inside a loop (Cont.)
14
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
16
Static variables inside a Class
(Cont.)
• Important detail to keep in mind:
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.
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