C++ OO methods
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
Constructor and param constructor
A constructor can have or not have parameters.
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the param constructor
Line();// This is the empty-param constructor
private:
double length;
};
// Member functions definitions including constructor
Line::Line( double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
Line::Line(){
cout << "Object is being created" << endl;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// Main function for the program
//int main( )
//{
// Line line(10.0);
// Line line2;
//
// // get initially set length.
// cout << "Length of line : " << line.getLength() <<endl;
// // set line length again
// line.setLength(6.0);
// cout << "Length of line : " << line.getLength() <<endl;
//
// return 0;
//}
~ destructor
A destructor is a special member function of a class that is executed whenever an object of it’s class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
// Member functions definitions including constructor
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// Main function for the program
int main( )
{
Line line;
// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
copy constructor
The copy constructor is a constructor which creates an object by initialising it with an object of the same class, which has been created previously.
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 copy constructor.
Example:
/*
Syntax for creating copy
classname (const classname &obj) {
// body of constructor
}
*/
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor
private:
int *ptr; // an int pointer
};
//Member function
Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
// allocate memory for the pointer;
ptr = new int;
*ptr = len;
}
//copy method
Line::Line(const Line &obj)
{
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}
void display(Line obj)
{
cout << "Length of line : " << obj.getLength() <<endl;
}
// Main function for the program
int main( )
{
Line line(10); // constructor is invoked here!
display(line);
//(1) when this is called, first it copy line to obj
//(formal param in display)
//then output obj.getLength()
//then display exit, obj's ~ deconstructor called.
//then program exit, line's deconstructor called.
return 0;
}
/*
which is why the output is:
Normal constructor allocating ptr
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
*/
Note if you use “=” here you are actually COPY the value. And when you do function call, you are also if fact copying a isolate new object! This behave so much different than JAVA and I still don’t know how to just pass reference… need to learn!
// use above class...
int main( )
{
Line line1(10);
Line line2 = line1; //
cout <<"Length of line1: "<<line1.getLength()<<endl;
cout <<"Length of line2: "<<line2.getLength()<<endl;
line2.setLength(20);
cout <<"Length of line1: "<<line1.getLength()<<endl;
cout <<"Length of line2: "<<line2.getLength()<<endl;
//display(line);
return 0;
}
and you still get this:
Normal constructor allocating ptr
Copy constructor allocating ptr.
Length of line1: 10
Length of line2: 10
Length of line1: 10
Length of line2: 20
Freeing memory!
Freeing memory!
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. To declare all member functions of class ClassTwo as friends of class ClassOne, place a following declaration in the definition of class ClassOne:
friend class ClassTwo;
Example of a friend function in Box:
#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 with member function
box.setWidth(10.0);
// Use friend function to print the wdith.
printWidth( box );
return 0;
inline function
Why? faster.
To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function.
A function definition in a class definition is an inline function definition, even without the use of the inline specifier.
this pointer
this->member
we also use point->member to access members of a class.
#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;
}
double Volume()
{
return length * breadth * height;
}
int compare(Box box)
{
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
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;
}
return 0;
}
static member of C++ class
static var
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).
static function
By declaring a function member as static, you make it independent of any particular object of the class.
A static member function can only access static data member, other static member functions and any other functions from outside the class.
polymorphism overload…(shenglue)
virtual keyword
static resolution/ static linkage, the function call is fixed before the program is executed. This is also sometimes called early binding. That is when you call a function, it may execute upper classes’s function instead of its current function. A difference between Java see this example:
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
int area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape{
public:
Rectangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public Shape{
public:
Triangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
// Main function for the program
int main( )
{
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// store the address of Rectangle
shape = &rec;
// call rectangle area.
shape->area();
// store the address of Triangle
shape = &tri;
// call triangle area.
shape->area();
return 0;
}
Output:
Parent class area :
Parent class area :
even though shape points to the rec or tri, it still calls the shape’s area().
By putting virtual, we can see
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
virtual int area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
This time, the compiler looks at the contents of the pointer instead of it’s type. This sort of operation is referred to dynamic linkage, or late binding.
Pure Virtual Functions:
virtual int area() = 0;