Chapter
1
Introduction
Chapter 2
Functions
Chapter 3
Branching
Chapter 4
Iteration
Chapter 5
OOP
Chapter 6
Arrays
|
CHAPTER 5
( Object Oriented Programming )
We humans are very good in recognizing and working with
objects, such as a pen, a dog, or a human being. We
learned to categorize them in such a way that make sense to
us. We may categorize them as animate object, inanimate
objects, pets, friends, etc. We some times classify objects
based on their attributes, for example, green apples or
red apples, fat or slim people, etc. If you think about it
each object has many attributes. If I ask you list the
attributes of an orange, you probably could list many things
such as color, shape, weight, smell, etc.
In addition to attributes, all objects exhibit
behaviors. A dog eats, barks, wags its tail, plays, and
begs. A dog exhibits many more other behaviors than this short
list. It is a good idea to practice listing attributes and
behaviors of many of the objects you come across each day.
Another thing we need to remember about objects is that
objects interact between each other.
Programmers, for over thirty years programmed using
functions and procedures. Each function and procedure was
called to carry out certain tasks on the data that were given
to it and to return (or not return) certain results back. With
this type of procedure oriented programming, we had to
adapt our thinking procedurally. Working with objects is a
more normal and suitable approach for human beings. Such
programming approach is called Object Oriented
Programming (OOP).
|
In Object Oriented Programming, objects are packages that
contain data and functions (methods) that can be
performed on the data. Data could be considered to be
attributes and functions are considered to be
behaviors of the object. We can say that the attributes and
behaviors are encapsulated into an object. The objects
interact between each other through their interfaces. As an
example a date object may have a set of data
consisting of month, day and year, and methods consisting of
assign date, display date, yesterday and tomorrow.
Data abstraction is another important feature of Object oriented
programming. OOP hides the details of data structures from the
users. For example, to set a date in the aforementioned example,
values of month, day and year are passed to the assign-date method.
The actual representation of the date is hidden from the user.
There can be various objects made of a particular class
(recall that many variables can be made from type). In OOP the
general type with which objects can be made is called a
class. An object is an instance of a class. Each class
contains data (data members) as well as set of functions
(member functions) that manipulate the data. Classes have the
following features:
- the capability to control access
- Constructors
- Destructors
- Data Members
- Member functions
- A hidden, special pointer called this
Program 5-1 is a program that incorporates a c++ object. The
class Grade contains three data members and three member
functions (methods) that manipulate the data. The class has a
public: section that is accessible to those using the class,
and a private: section that only accessible to member
functions and not accessible to the class users.
Program 5-1
/******************************************
Program Grades - class
By Dr. John Abraham
Written for CSCI 1380 students
Objective: introduce object oriented programming
*******************************************/
#include <iostream.h>
#include <iomanip.h>
class Grade {
public:
Grade(); //constructor
void getGrades(int, int, int);
void printGrades();
void printLetterGrade();
private:
int g1;
int g2;
int g3;
};
Grade::Grade()
{
g1=g2=g3=0;
}
void Grade:: getGrades (int a, int b,int c)
{
g1=a; g2=b; g3=c;
}
void Grade:: printGrades()
{
cout << "\n\n--------------------------------";
cout << "\nHere are the Grades you entered: "
<<g1<<setw(4)<<g2<<setw(4)<<g3;
}
void Grade::printLetterGrade()
{
float av;
av = (g1+g2+g3)/3.0;
cout << "\nYour Average and Letter Grade ->
"<<av;
if (av >= 90) cout <<" A\n";
else if (av >=80) cout << " B\n";
else if (av >=70) cout <<" C\n";
else if (av >= 60) cout << " D\n";
else cout << " F\n";
}
int main (void)
{
int a,b,c;
Grade n; //n is an object of class Grade
cout <<"\nEnter three grades separated by spaces
";
cin >> a >> b >> c;
n.getGrades(a,b,c);
n.printGrades();
n.printLetterGrade();
return (0);
} |
Program Run 5-1
Enter three grades separated by spaces 81 78
82
--------------------------------
Here are the Grades you entered: 81 78 82
Your Average and Letter Grade -> 80.3333 B
Press any key to
continue |
Let us discuss this program in detail. We have declared a
class named Grade. We have an object made up of
this class, namely n. We could have made other objects of
class Grade. An object encapsulates the data and functions that
operate on that data. In this case the data used are three integers
and three functions (methods) are getGrades, printGrades, and
printLetter. The public part of the class is visible and accessible
to all users of the class, the private part is not. The public part
contains a constructor; a constructor is a function that is
automatically called when an instance of a class is created. A
constructor is used to initialize any class member variables, and
allocate memory that the class will need. The member functions are
similar to the functions we have used so far except in the
declaration the scope operator :: is used. For example, void
Grade::getGrades (int a, int b, intc), clearly indicates that
getGrades is a member function of class Grade.
Now let us examine the main. Here we have three integer variables
a, b, c. and an object n. These values of these variables are read
from the keyboard and then the three member functions of the object
n are called. Imagine creating an include file of the class Grade.
Instead of imagining it, let us do it.
/******************************************
Program Grades - class
By Dr. John Abraham
Written for CSCI 1380 students
Objective: introduce object oriented programming
*******************************************/
#include <iostream.h>
#include <c:\tempc\classgrade.h>
int main (void)
{
int a,b,c;
Grade n;
cout <<"\nEnter three grades separated by spaces ";
cin >> a >> b >> c;
n.getGrades(a,b,c);
n.printGrades();
n.printLetterGrade();
return (0);
}
The include file is given below.
/******************************************
Program Grades - class
By Dr. John Abraham
Written for CSCI 1380 students
Objective: introduce object oriented programming
*******************************************/
#include <iomanip.h>
class Grade {
public:
Grade(); //constructor
void getGrades(int, int, int);
void printGrades();
void printLetterGrade();
private:
int g1;
int g2;
int g3;
};
Grade::Grade()
{
g1=g2=g3=0;
}
void Grade:: getGrades (int a, int b,int c)
{
g1=a; g2=b; g3=c;
}
void Grade:: printGrades()
{
cout << "\n\n--------------------------------";
cout << "\nHere are the Grades you entered: "
<<g1<<setw(4)<<g2<<setw(4)<<g3;
}
void Grade::printLetterGrade()
{
float av;
av = (g1+g2+g3)/3.0;
cout << "\nYour Average and Letter Grade ->
"<<av;
if (av >= 90) cout <<" A\n";
else if (av >=80) cout << " B\n";
else if (av >=70) cout <<" C\n";
else if (av >= 60) cout << " D\n";
else cout << " F\n";
}
Go to top of this
chapter
|