Class 2
Class 2
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;
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;
}
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;
}