0% found this document useful (0 votes)
85 views17 pages

Class 2

The copy constructor is a special constructor that creates an object by initializing it with an object of the same class. It allows initializing one object from another of the same type, copying an object to pass it as a function argument, and copying an object to return it from a function. If not defined by the user, the compiler provides a default copy constructor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views17 pages

Class 2

The copy constructor is a special constructor that creates an object by initializing it with an object of the same class. It allows initializing one object from another of the same type, copying an object to pass it as a function argument, and copying an object to return it from a function. If not defined by the user, the compiler provides a default copy constructor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Copy Constructor

The copy constructor is a constructor which


creates an object by initializing it with an
object of the same class, which has been
created previously.
The copy constructor is used to:1. Initialize one object from another of the
same type.
2.Copy an object to pass it as an argument to
a function.
3.Copy an object to return it from a function.
If a copy constructor is not defined in a class,
the compiler itself defines one. If the class has
pointer variables and has some dynamic
memory allocations, then it is a must to have a

class Line {
public :
int getLength( void );
Line( int len );
// simple
constructor
Line( const Line &obj);
// copy
constructor
~Line();
//
destructor
private:
int *ptr;
};
// Member functions definitions including
constructor Line::Line(int len)
{
cout << "Normal constructor allocating
ptr" << endl;
// allocate memory for the pointer
ptr = new int;
*ptr = len;

Line::Line(const Line &obj)


{
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr;
// copy the value
}
Line::~Line(void)
{
cout << "Freeing memory!" << endl;
delete ptr;
}
int Line::getLength( void )
{
return *ptr;
}
void display(Line obj)
{
cout << "Length of line : " << obj.getLength()
<<endl;
}

// Main function for the program


int main( )
{
Line line(10);
display(line);
return 0;
}
OR
int main( )
{
Line line1(10);
Line line2 = line1; // This also calls copy
constructor
display(line1);
display(line2);
return 0;
}

Friend Function
A friend function of a class is defined outside
that class' scope but it has the right to
access all private and protected members of
the class. Even though the prototypes for
friend
functions
appear
in
the
class
definition, friends are not member functions.
A friend can be a function, function template,
or member function, or a class or class
template, in which case the entire class and
all of its members are friends.

Friend Function
#include <iostream> using
namespace std;
class Box
{
double width;
public:
friend void printWidth( Box
box );
void setWidth( double wid );
};
// Member function definition
void Box::setWidth( double wid )
{
width = wid;
}

// Note: printWidth() is not a member function of


any class. void printWidth( Box box )
{
/* Because printWidth() is a friend of Box, it can
directly access any member of this class */
cout << "Width of box : " << box.width <<endl;
}
// Main function for the program
int main( )
{
Box box;
// set box width without
member function
box.setWidth(10.0);
// Use friend function
to print the width
printWidth( box );
return . 0;

Inline Functions
C++ inline function is powerful concept that is
commonly used with classes. If a function is
inline, the compiler places a copy of the code
of that function at each point where the
function is called at compile time.
Any change to an inline function could require
all clients of the function to be recompiled
because compiler would need to replace all the
code once again otherwise it will continue with
old functionality.
To inline a function, place the keyword inline
before the function name and define the
function before any calls are made to the
function. The compiler can ignore the inline
qualifier in case defined function is more than

#include <iostream>
using namespace std;
inline int Max(int x, int y) { return (x > y)? x :
y; }
// Main function for the program
int main( )
{
cout << "Max (20,10): " << Max(20,10) <<
endl;
cout << "Max (0,200): " << Max(0,200) <<
endl;
cout << "Max (100,1010): " <<
Max(100,1010) << endl;
return 0;
}

This pointer
Every object in C++ has access to its own
address through an important pointer called
this pointer. The this pointer is an implicit
parameter to all member functions. Therefore,
inside a member function, this may be used to
refer to the invoking object.
Friend functions do not have a this pointer,
because friends are not members of a class.
Only member functions have a this pointer.

This pointer
#include <iostream>
using namespace std;
class Box
{
public:
// Constructor definition
Box(double l=2.0, double b=2.0, double
h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}

This pointer
double Volume()
{
return length * breadth * height;
}
int compare(Box box)
{
return this->Volume() > box.Volume();
}
private:
double length;
double breadth;
double height; };

int main(void)
{
Box Box1(3.3, 1.2, 1.5);
// Declare box1
Box
Box2(8.5,
6.0,
2.0);
// Declare box2
if(Box1.compare(Box2))
{
cout << "Box2 is smaller than Box1"
<<endl;
}
else
{
cout << "Box2 is equal to or larger than
Box1" <<endl;
}

Static Member
We can define class members static using static
keyword. When we declare a member of a class
as static it means no matter how many objects
of the class are created, there is only one copy
of the static member.
A static member is shared by all objects of the
class. All static data is initialized to zero when
the first object is created, if no other
initialization is present. We can't put it in the
class definition but it can be initialized outside
the class as done in the following example by
redeclaring the static variable, using the scope
resolution operator :: to identify which class it
belongs to.

Static Member
#include <iostream>
using namespace std;
class Box
{
public:
static int objectCount;
// Constructor definition
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++; }

double Volume()
{
return length * breadth * height;
}
private:
double length;
double breadth;
double height;
};
// Initialize static member of class Box
int Box::objectCount = 0;

int main(void)
{
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
// Print total number of objects.
cout << "Total objects: " << Box::objectCount
<< endl;
return 0;
}

You might also like