Pointers N Classes
Pointers N Classes
Pointers
And
Classes
Understanding pointers
• Pointer is a variable that stores a memory
address.
• Every variable is located under unique location
within a computer's memory and this unique
location has its own unique address, the memory
address.
• it holds the memory address as its value and has
an ability to “point” ( hence pointer ) to certain
value within a memory, by use of its associated
memory address.
Retrieving a Variable's Memory Address
• To retrieve a variable's memory address, we need to use address-of operator ‘&’.
E.g:
• #include <iostream>
int main()
{
using namespace std;
// Declare an integer variable and initialize it with 99
int myInt = 99;
// Print out value of myInt
cout << myInt << endl;
// Use address-of operator & to print out memory address of myInt
cout << &myInt << endl;
return 0;
}
• OUTPUT:
99
0xbff26312
Declaration
• Declaring a pointer in C++ is as simple as to
declare any other variable with one single
difference.
• Asterik symbol " * " needs to be added and
located after variable type and before a variable
name.
• One rule has to be followed when assigning
memory address to a pointer: pointer type has to
match with variable type it will point to. One
exception is a pointer to void, which can handle
different types of variables it will point to.
Pointers
int *intPtr; Create a pointer
*intPtr 6837
intPtr 0x0050
*intPtr 5 otherVal
intPtr 0x0054 &otherVal
Pointers to classes
• It is perfectly valid to create pointers that point
to classes. We simply have to consider that once
declared, a class becomes a valid type, so we can
use the class name as the type for the pointer.
For example:
CRectangle * prect;
– is a pointer to an object of class CRectangle.
• To refer directly to a member of an object
pointed by a pointer we can use the arrow
operator (->) of indirection. Here is an example
with some possible combinations:
• // pointer to classes example
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
};
void CRectangle::set_values (int a, int b)
{
width = a; height = b;
}
int main () {
CRectangle a, *b, *c;
CRectangle * d = new CRectangle[2];
b= new CRectangle;
c= &a;
a.set_values (1,2);
b->set_values (3,4);
d->set_values (5,6);
d[1].set_values (7,8);
cout << "a area: " << a.area() << endl;
cout << "*b area: " << b->area() << endl;
cout << "*c area: " << c->area() << endl;
cout << "d[0] area: " << d[0].area() << endl;
cout << "d[1] area: " << d[1].area() << endl;
delete[] d; delete b;
return 0;
}
Output Results
a area: 2
*b area: 12
*c area: 2 d[0]
area: 30 d[1] area: 56
Inheritance
• is the process by which new classes called derived/sub
classes are created from existing classes called
base/super classes. The derived classes have all the
features of the base class and the programmer can
choose to add new features specific to the newly
created derived class.
• Advantages of Inheritance:
– Reusability: Inheritance helps the code to be reused in many
situations.
– Saves Time and Effort: Since the main code written can be
reused in various situations as needed.
– Increases Program Structure which results in greater
reliability.
– Polymorphism
• General Format for implementing the concept
of Inheritance:
– class derived_classname: access specifier
baseclassname
• For example, if the base class is Polygon and the
derived is Triangle, it is specified as:
– class Triangle: public Polygon
• The above makes Triangle have access to both
public and protected variables of base class
Polygon.
• Reminder about public, private and protected
access specifiers:
– If a member defined in a class is private, then they
are accessible by members of the same class only and
cannot be accessed from outside the class.
– Public members and variables are accessible from
outside the class.
– Protected access specifier is a stage between private
and public. If a member functions or variables
defined in a class are protected, then they cannot be
accessed from outside the class but can be accessed
from the derived class.
• The access specifier specified in the line:
class Triangle: public Polygon
– Public indicates that the public data members
which are inherited from the base class by the
derived class sample remains public in the derived
class.
• The base class constructor is used to initialize
the base class data members and the derived
class constructor is used to initialize the data
members defined in derived class.
Inheritance
• // derived classes
#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b) { width=a; height=b;}
};
class CRectangle: public CPolygon {
public:
int area () { return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area () { return (width * height / 2); }
};
// Main method
int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
Multiple inheritance
• This is where a class inherits members from
more than one class.
• This is done by simply separating the different
base classes with commas in the derived class
declaration. E.G:
– class CRectangle: public CPolygon, public CShape {
• In this case, CRectangle inherites from CPolygon
and CShapes.
Friendship
• It is often useful for one class to see the private
variables of another class, even though these
variables should probably not be made part of
the public interface that the class supports.
• C++ provides the friend keyword to do just this.
Inside a class, you can indicate that other classes
(or simply functions) will have direct access to
protected and private members of the class.
• When granting access to a class, you must specify
that the access is granted for a class using the
class keyword: friend class aClass;
• Note that friend declarations can go in either the
public, private, or protected section of a class--it
doesn't matter where they appear.
class Node {
private:
int data; int key;
// ...
friend class BinaryTree; // class BinaryTree can now access data directly
};
Example
• // friend class
#include <iostream>
using namespace std;
class CSquare;
class CRectangle {
int width, height;
public:
int area () {return (width * height);}
void convert (CSquare a);
};
• class CSquare {
private:
int side;
public:
void set_side (int a)
{side=a;}
friend class CRectangle;
};
void CRectangle::convert (CSquare a) {
width = a.side;
height = a.side;
}
// Main method
int main () {
CSquare sqr;
CRectangle rect;
sqr.set_side(4);
rect.convert(sqr);
cout << rect.area();
return 0;
}
In this example, we have declared CRectangle as a friend of
CSquare so that CRectangle member functions could have
access to the protected and private members of CSquare,
more concretely to CSquare::side, which describes the
side width of the square.
• You may also see something new at the beginning of the
program: an empty declaration of class CSquare. This is necessary
because within the declaration of CRectangle we refer to CSquare
(as a parameter in convert()). The definition of CSquare is
included later, so if we did not include a previous empty
declaration for CSquare this class would not be visible from within
the definition of CRectangle.
• Consider that friendships are not corresponded if we do not
explicitly specify so. In our example, CRectangle is considered as a
friend class by CSquare, but CRectangle does not consider
CSquare to be a friend, so CRectangle can access the protected
and private members of CSquare but not the reverse way. Of
course, we could have declared also CSquare as friend of
CRectangle if we wanted to.
• Another property of friendships is that they are not transitive:
The friend of a friend is not considered to be a friend unless
explicitly specified.
Friend functions
• If we want to declare an external function as
friend of a class, thus allowing this function to
have access to the private and protected
members of this class, we do it by declaring a
prototype of this external function within the
class, and preceding it with the keyword friend:
– friend return_type class_name::function(args);
class Node {
private:
int data;
int key;
// ...
friend int BinaryTree::find(); // Only BinaryTree's find
function has access
};