Chapter 2
Chapter 2
Objective
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor
Examples –
o1 o2 o3
a a a
b b b
//A PROGRAM OF STATIC MEMBER DATA
#include<iostream.h>
class TestStatic
{
int x;
static int count;
public:
void show()
{
cout<<x<<count;
}
};
int TestStatic::count=20;
void main()
{
TestStatic o1,o2;
o1.show();
o2.show();
}
Static Member Function
A function defined with static modifier is termed as static member
function.
A static function can have access to only other static members ( data or
function ) declared in the same class .
A static member function can be called using the class name instead of its
objects.
class_name :: function_name ;
TestStatic :: show();
Non static member function can access non static & static members.
//A PROGRAM OF STATIC MEMBER FUNCTION
#include<iostream.h>
class TestStatic
{
int x;
static int count;
public:
static void show() { cout<<count<<endl; }
void show2(){ cout<<x<<endl<<count; }
};
int TestStatic::count=10;
void main()
{
TestStatic o1;
TestStatic::show();
o1.show2();
}
Friend Functions in C++
Friend Functions
It is possible to grant a nonmember function access to the private members
of a class by using a friend.
A friend function has access to all private and protected members of the
class for which it is a friend.
To declare a friend function, include its prototype within the class,
preceding it with the keyword friend.
Syntax:
public:
friend ret-type fun-name(arg-list);
Example:
#include <iostream.h>
class FriendDemo {
int a, b;
public:
friend int sum(FriendDemo fd);
void setvalue(int i, int j);
};
void FriendDemo::setvalue(int i, int j)
{
a = i;
b = j;
}
int sum(FriendDemo x)
{
return x.a + x.b;
}
int main()
{
FriendDemo fd_ob;
fd_ob.setvalue(30, 40);
cout << sum(fd_ob);
return 0;
}
Operator Overloading
Operator overloading
Operator overloading means giving capability to the operator to work on
different types of operands.
The operators +,* etc, works for type int, float etc. we can overload these
operators by giving them the capability to work on derived data types.
By making the operators to work on derived data types just like they work
on built-in data types, we can have a consistent approach.
This is done with the help of special function called operator function.
Syntax for operator overloading
return-type classname::operator #(arg-list)
{
//operations
}
Often, operator functions returns an object of the class they operate on,but
ret-type can be any valid type. The # is a placeholder. When you create an
operator function, substitute the operator for the #.
Operator functions must be either member functions (nonstatic ) or friend
function.
A member function has no arguments for unary operators and only one for
binary operators.
And if there is a friend function then a friend function will have only one
argument for unary operators and two for binary operators.
Arguments may be passed either by value or by reference.
Rules for overloading operators
}
Thank You