Lecture - 02 - Object Orientation in C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Object Orientation in C++

Data Types, Objects, Classes and Instances


Resource Person:
Shafaq Khan
Data Types, Objects, Classes and
Instances
• So far, we've learnt that C++ lets you create variables which
can be any of a range of basic data types:
int, long, double and so on. However, the variables of the
basic types don't allow you to model realworld objects (or
even imaginary objects) adequately.
• It's hard to model a box in terms of an int, for
example; what we need is something that allows us to collect
together the various attributes of an
object. In C++, we can do this very easily using classes
Data Types, Objects, Classes and
Instances
• You could define variables, length, breadth and height to
represent the dimensions of the box and bind
them together as members of a Box class, as follows:
class Box
{
public:
double length;
double breadth;
double height;
};
Data Types, Objects, Classes and
Instances
• The elements length, breadth and height are referred to as
data members.
• At the top of the class definition, you can see we have put the
keyword public - this just means that the data members are
generally accessible from outside the class.
• Instead of breaking down a problem in terms of what are
essentially computer-related data types (integer numbers,
floating point numbers and so on) and then writing a program,
we're going to be programming in terms of problem-related
data types, in other
words classes.
Data Types, Objects, Classes and
Instances
• Box myBox; //Declare a variable myBox of type Box
• Once we've defined the class Box, the declaration of
variables of this type is quite standard. The variable
myBox here is also referred to as an object or an instance of
the class Box.
• You can then create, manipulate and destroy as many Box
objects as you need to in your program.
Operations on Classes
• In C++ you can create new data types as classes to
represent whatever kinds of objects you like. As you'll
come to see, classes aren't limited to just holding data;
you can also define member functions that act on your
objects, or even operations that act between objects of
your classes using the standard C++
operators.
Operations on Classes
• You can define the class Box, for example, so that the
following statements work and have the meanings you
want them to have:
Box Box1;
Box Box2;
if(Box1 > Box2) // Fill the larger box
Box1.Fill();
else
Box2.Fill()
Operations on Classes
• You could also implement operations as part of the Box
class for adding, subtracting or even multiplying boxes - in
fact, almost any operation to which you could ascribe a
sensible meaning in the context of boxes.
• Program design now starts with deciding what new
application-specific data types you need to solve the
problem in hand and writing the program in terms of
operations on the specifics that the problem is concerned
with.
Terminology
• Let's summarize some of the terminology that we will be using
when discussing classes in C++:
– A class is a user-defined data type
– Object-oriented programming is the programming style based on the
idea of defining your own data types as classes
– Declaring an object of a class is sometimes referred to as
instantiation because you are creating an instance of a class
– Instances of a class are referred to as objects
– The idea of an object containing the data implicit in its definition,
together with the functions that operate on that data, is referred to as
encapsulation.
Member Functions of a Class
• A member function of a class is a function that has its
definition or its prototype within the class definition.
• It operates on any object of the class of which it is a
member, and has access to all the members of a class for
that object.

Example
// Calculating the volume of a box with a member function
#include <iostream>
using namespace std;
class Box // Class definition at global scope
{
public:
double length; // Length of a box in inches
double breadth; // Breadth of a box in inches
double height; // Height of a box in inches
Example
// Function to calculate the volume of a box
double Volume(void)
{
return length * breadth * height;
}
};
int main(void)
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
Box1.height = 18.0; // Define the values
Box1.length = 78.0; // of the members of
Box1.breadth = 24.0; // the object Box1
Example
Box2.height = Box1.height - 10; // Define Box2
Box2.length = Box1.length/2.0; // members in
Box2.breadth = 0.25*Box1.length; // terms of Box1
volume = Box1.Volume(); // Calculate volume of Box1
return length * breadth * height;
}
Example
cout << endl << "Volume of Box1 = " << volume;
cout << endl << "Volume of Box2 = " << Box2.Volume();
cout << endl << "A Box object occupies “ << sizeof Box1 <<
" bytes.";
cout << endl;
return 0;
}
Positioning a Member Function
Definition
• A member function definition need not be placed inside the class
definition. If you want to put it outside the class definition, you need
to put the prototype for the function inside the class.
• class Box // Class definition at global scope
{
public:
double length; // Length of a box in inches
double breadth; // Breadth of a box in inches
double height; // Height of a box in inches
double Volume(void); // Member function prototype
};
Positioning a Member Function
Definition
• Now we need to write the function definition, but there has
to be some way of telling the compiler that the function
belongs to the class Box.
• This is done by prefixing the function name with the name
of the
class and separating the two with the scope resolution
operator, ::, which is formed from two successive colons.
Positioning a Member Function
Definition
• // Function to calculate the volume of a box
double Box::Volume(void)
{
return length * breadth * height;
}
• It will produce the same output as the last example. However, it isn't
exactly the same program. In the second case, all calls to the
function are treated in the way that we're already familiar with.
• However, when we defined the function within the definition of the
class in Ex02.cpp, the compiler implicitly treated the function as an
inline function.

You might also like