0% found this document useful (0 votes)
29 views

Chapter 2

The document discusses constructors, destructors, reference variables, static members, friend functions, and operator overloading in C++. Constructors initialize objects, destructors destroy objects, reference variables provide aliases to other variables, static members are shared across all class instances, friend functions access private members, and operator overloading allows operators to work on user-defined types.

Uploaded by

Ram Bhardwaj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Chapter 2

The document discusses constructors, destructors, reference variables, static members, friend functions, and operator overloading in C++. Constructors initialize objects, destructors destroy objects, reference variables provide aliases to other variables, static members are shared across all class instances, friend functions access private members, and operator overloading allows operators to work on user-defined types.

Uploaded by

Ram Bhardwaj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Constructor,Destructor

Objective

In this chapter learner able to understand:


• Constructor and Destructor in c++
Constructor
Constructor
 C++ provides a special member function called
Constructor.
 It enables an object to initialize itself when it is
created.
 It is called constructor because it constructs the values
of data members of the class.
 It is special, because its name is the same as the class
name.
 They do not have return types.
Types of Constructor in C++

1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor

 A constructor that accepts no arguments is called the default constructor.


 Default constructors are used to assign some default values to data members at the
time of object creation.
 Example:
Parameterized Constructor

 The constructor that can take parameters are called parameterized


constructors.
 Sometimes it may be necessary to initialize the various data elements of
different objects with different values when they are created.
 C++ permits us to achieve this objective by passing arguments to the
constructor function when the objects are created.
Reference Variable
Reference Variable
 C++ Introduces a new kind of variable known as the reference
variable.
 A reference variable provides an alias (nick name ) for a
previously defined variable.
 Both the variable refer to the same data object in the memory .
Hence , change in the value of one will also be reflected in the
value of the other variable.
 A reference variable must be initialized at the time of
declaration.
 This establishes the correspondence between the reference and
the data object which it names.
 A reference , internally works as a const pointer hence once
initialized a reference can not be made to refer to another
variable . Unlike a pointer , reference gets automatically de-
referenced.
Syntax –
data_type &ref_name = var_name ;

Examples –

float total = 100; int a = 5;


float &sum = total; int &b = a;
Copy Constructor
 A copy constructor takes a reference to an object of the same class as itself as
an argument .
 The process of initializing through a copy constructor is known as copy
initialization.
class testclass
{
int a;
public:
testclass(testclass &)
{ ------
------
}
};

Is Valid & called the Copy Constructor.

 So a Copy constructor is used to declare and initialize an object from another


object .
testclass o2(o1); OR testclass o2 = o1;
Note :
testclass o2;
o2 = o1;
Legal but not invoke the Copy Constructor.
Calling of Constructor

Calling of constructor can be of two types –


1. Explicit Call
2. Implicit Call

Explicit Call – Implicit Call –


Test o1 = Test(); Test o1;
Test o1 = Test(5); Test o1(5);
Test o1 = Test(5,10); Test o1(5,10);
Destructor

 C++ provides a special member function called the destructor .


 The destructor is a member function whose name is same as class name
preceded by a tilde(~).
 It is used to destroy the objects that have been created by a constructor .
 A destructor does not have return type & never takes any arguments.
 A destructor is invoked automatically when the scope of object is going to
be finished.
 A destructor can not be overloaded.
Static Data Member and Member Function in C++
Static Data Member
 A data member of a class can be qualified as static .
 Only one copy of that member is created for the entire class
and is shared by all the objects of that class, no matter how
many objects are created.
 It is visible only within the class, but its lifetime is for the
entire program.
 Static variables are normally used to maintain values common
to the entire class.
 Static data member can be called using name of the class with
SRO and also by object with member selection operator.
Common for all objects
static data members

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

 Only existing operators can be overloaded. New operators can not be


created.
 The overloaded operator must have at least one operand that is of derived
data type.
 We can not change the basic meaning of an operator. This is to say, we can
not redefine the plus (+)operator to subtract one value from the other.
 Overloaded operators follow the syntax rules of the original operators.
They cannot be overridden.
 There are some operators that can not be overloaded.
 We cannot use friend functions to overload certain operators . However
member functions can be used to overload them.
Operators that cannot be overloaded

The size of operator (Sizeof)


The membership operator (.)
The pointer to member operator ( .* )
The scope Resolution operator ( :: )
The conditional operator (?:)
Where a friend cannot be used

Assignment operator (=)


Function call operator ( () )
Subscripting operator ( [ ] )
Class member access operator (->)
//A PROGRAM OF OPERATOR(+) OVERLOADING
#include<iostream.h>
class Test
{
int x,y;
public:
void set()
{
cout<<"Enter any Two Number:";
cin>>x>>y;
}
void show()
{
cout<<x<<endl<<y;
}
Test operator +(Test p)
{
Test temp;
temp.x=x+p.x;
temp.y= y+p.y;
return(temp);
}
};
int main()
{
Test o1, o2, o3;
o1.set();
o2.set();
o3=o1+o2;
o3.show();
system(“pause”);
return 0;
}
//operator overloading of > Operator
#include<iostream.h>
class Test
{
int x,y;
public:
void get()
{
cout<<"Enter any Two Number :";
cin>>x>>y;
}
void show()
{
cout<<x<<y;
}
int operator >(Test p)
{
if( (x > p.x) && ( y > p.y) )
{
return(1);
}
else
{
return(0);
}
}
};
int main()
{
Test o1, o2;
o1.get();
o2.get();
if(o1>o2)
cout<<“o1 greater”<<endl
else
cout<<“o2 greater”<<endl

}
Thank You

You might also like