Introduction To Programming Using C++: Lecture Five: Introducing Classes
Introduction To Programming Using C++: Lecture Five: Introducing Classes
Carl Gwilliam
gwilliam@hep.ph.liv.ac.uk
http://hep.ph.liv.ac.uk/~gwilliam/cppcourse
Types Revisited
A type defines the information
that can be held by an object.
A type limits the actions that can
be performed on the object.
A type is a method of
determining the amount of
memory that needs to be
allocated for a new object.
//integer
float b;
//real
double c; //real
bool d;
//boolean
char e;
//character
Extending Types
Types are not just a method of expressing the precision
of a value. A type is a category.
The key to OO programming in C++ is the ability to
create user defined types.
// some declarations
int x, y;
Particle electron, muon;
Department physics, chemistry;
Car myFiesta;
If the new types are declared and defined in the correct
manner then all of the above statements are valid C++.
BUT:
OO programming allows the creation of new types that
more accurately reflect the scenario to be modeled.
A problem can be modeled in terms of objects and the
interaction between different objects.
This seems contrary to our obsession with reducing any situation
into a series of abstract mathematical statements
Layers of Abstraction
Object Orientated programming is just a higher level of
abstraction than you are currently used to.
objects
modules
statements
assembler
language
bitwise operations
ABSTRACTION
Layers of Abstraction
Dealing with objects and object interaction enables
programs to be designed on a more intuitive level.
Planet a,b;
a.force(b);
// information determined
// by class decleration
mass1 = getMass(1);
// functions called to
force = getForce (G,mass1,mass2,dist); //retrieve properties
ABSTRACTION
Introducing Classes
User defined types are referred to as classes.
An object is an instance of class. A class is a category
of objects. Several objects of a certain class can be
instantiated (just like built in types).
int x;
// equivalent
Car myFiesta, myFerrari; // statements
A car can be declared in the same way that an integer
can be declared. The variable myFiesta is associated
with an object of type Car.
An object instance is not a single value. Math operators
have to be assigned a new meaning in this context.
float someValue = myFerrari myFiesta;
Declaring a Class
A simple car class will be designed to introduce the
basic concepts of class construction.
In the first iteration, a car object consists of two
characteristics, the age and retail price of the car.
A car class is declared using the
// car declaration
class keyword followed by the
class Car {
name of the class.
public:
The variables declared inside
int ageInYears;
the block are associated with
int priceInGBP;
this class.
}; // semi-colon
// terminated
These are known as member
variables or member data.
Member Functions
A class is a collection of member variables and a set of
related functions, known as member functions or class
methods.
Member functions are also
class Car {
accessed using the dot operator.
public:
int ageInYears;
They are distinguished from
int priceInGBP;
member variables by the use of
void brake();
parentheses.
void accelerate();
Like other functions they can take
};
arguments
Member functions have a role specific to
the class they are declared in. They are
primarily used to access and manipulate
class member data.
Car myFiesta;
myFiesta.brake();
Car myFiesta;
// error here
myFiesta.ageInYears = 5;
Accessor Methods
class Car {
Private member data can be
modified by calling public member public:
void brake();
functions known as accessor
void accelerate();
methods. Accessor methods are
void setAge(int age);
int getAge();
commonly known as getters and
void setPrice(int price);
setters.
int getPrice();
private:
Why go the trouble hiding the member
int ageInYears;
data? It is easier for me to access the data
int priceInGBP;
directly through the dot operator rather
};
than through this level of indirection.
A Point Class
The remainder of this lecture will be used to construct a
point class.
int main() {
Point a, b;
:
}
To design the interface, think in terms of the point object:
What is my identity?
What messages can you give me?
What is my distance from the origin?
What is my distance from another point in space?
// point class
// declaration
class Point {
public:
void setX(float x);
void setY(float y);
void setZ(float z);
float getX();
float getY();
float getZ();
private:
float itsX;
float itsY;
float itsZ;
[Point.h]
};
Preprocessor Macros
Before the compiler interprets code it is passed through
the preprocessor, whose task is to find preprocessor
directives, or macros.
Preprocessor macros are indicated by lines beginning
with a hash, #.
In fact, we have already been using the #include macro.
Accessor Implementation
The implementation of the
#include point.h
accessor functions, along with int main() {
Point a;
other class methods, are
// set coordinates for point a
placed in a source file,
a.setX(2); a.setY(1); a.setZ(5);
// display coordinates
seperate from the header file
cout <<"vector coordinates a: "
and the main program.
<<a.getX()<<","<< a.getY()
<<","<<a.getZ()<<endl;
The implementation is totally
}
[UsePoint.cpp]
separate from the interface.
#include point.h
void Point::setX(float x) {
itsX = x;
}
float Point::getX() {
return itsX;
}
[Point.cpp]
:
// get distance from origin
float Point::distanceFromOrigin() {
float distance = sqrt( pow(itsX,2)
+ pow(itsY,2) + pow(itsZ,2) );
return distance;
}
:
[Point.cpp]
In this first iteration the x,y and z coordinates of one of the Points is
passed into the new Points membre function. Theres a better way
class Point {
public:
[Point.h]
: };
The member variables
// distance between points
from another point obj
float Point::distanceFromPoint(Point anotherPoint) {
have to be accessed
float difX = itsX - anotherPoint.itsX;
via the dot operator.
float difY = itsY - anotherPoint.itsY;
N.B. another objects
float difZ = itsZ - anotherPoint.itsZ;
member data can be
float distance = sqrt( pow(difX,2) + pow(difY,2) +
accessed directly by
pow(difZ,2) );
an object of the same
[Point.cpp]
return distance;
class without calling
}
:
public accessor func. :
int main() {
:
C++ is not a pure object
float distance = a.distanceFromPoint(b);
orientated language!
:
(theres a compromise to
[UsePoint.cpp]
}
enhance functionality)
[Point.h]
float distanceFromOrigin();
float distanceFromPoint(Point anotherPoint);
:
};
class Point {
public:
:
};
What are
these?