Comp 213 1 Object Oriented Programming
Comp 213 1 Object Oriented Programming
COMP 213
CHAPTER ONE
INTRODUCTION TO CLASSES
Learning Outcomes: At the end of the chapter, the learner should be able to:
Explain reasons as to why object oriented programming may be better than
traditional procedural programming;
Explain the meanings of the terms class, object, members, encapsulation,
and data hiding; and
Write simple programs using classes.
8.1: Introduction
The main difference between C and C++ (procedural) programs is in inputting and
outputting.
Whereas in C we use ‘stdio.h’ as the header file for inputting and outputting, in C++,
we use ‘iostream.h’.
Secondly, to input in C, we use the inbuilt function scanf() and pass two parameters
i.e. a string showing the data type of the variable we are inputting, and the variable’s
name. In C++, we use the stream cin>> followed by the variable to input (the
variable’s data type is not specified here).
Lastly, to output in C, we use the inbuilt function printf() and pass what we want to
output as a parameter. In C++, we simply use cout<< followed by what we want to
output.
Examples of statements in C and their equivalent in C++ are shown below. Here, we
have assumed that variable sum stores the sum of the variables num1 and num2.
All other procedural programming features we are going to study (variables, decision
making, loops, functions, arrays, structs, and pointers) have the same basic syntax in
C as in C++.
C++ IS A SUPERSET OF C
As said at the beginning, C++ was developed as a modification of C to include object
oriented features. Thus, C++ contains all what is in C in addition to other features. In
fact, you can actually combine C++ statements with C statements in the same
program. But you must use a C++ compiler and not a C compiler since C++ is the
superset.
Example 1
The following program inputs two integers and outputs their sum and their product
using C++ and C statements.
The program
#include <iostream> // Include C++ header file
#include <stdio.h> // Include C header file
using namespace std;
void main()
{
int a, b, sum, product; // Declare variables
cout<<“\nInput a ”; cin>>a; // Input a using C++
printf(“\nInput b ”); scanf(“%d”, &b); // Input b using C
Example 2
_________________________________________________________
Write a program to compute the area and the perimeter of a rectangle.
Solution
We need to do the following tasks to solve the problem.
(i) Input the length, and the width - length, width
(ii) Compute area as area=length*width
(iii) Compute perimeter as perimeter=2*(length+width)
(iv) Output the area and the perimeter
The program
#include <iostream>
using namespace std;
void main()
{ double length, width, area, perimeter;
cout<<“\nInput the length and the width”;
cin>>length>>width;
area=length*width;
perimeter=2*(length+width);
All the previous chapters in this book are based on this style.
All chapters from the current one henceforth are based on this style.
ABOUT CLASSES
Object oriented programming can be defined as a programming technique that uses
the concept of classes to combine data and operations that can be identified with
objects of their type.
A class is therefore a user defined data type from which a variable of its type
(object) can be created. It can be thought of as a ‘template’ i.e. a main copy from
Examples of classes
The following are examples of classes that may be identified in the respective areas
of applications.
ABOUT OBJECTS
An object is an instance (or occurrence) of its class. When a new object is created, it
automatically gets a copy of all the characteristics (or members) of its class.
For example, in the class of windows (above), a particular window on the screen is an
object. It has particular values of code, color, size, and location. Also, the above
operations can be applied on it i.e. it can be opened, closed, moved, etc. If we have
many windows opened on the screen, then we can store the properties specified above
for each. We can also apply the specified operations on each.
(ii) Secondly, it provides for constructors that are special member functions for
initializing newly created objects. This is similar to initializing a newly declared
variable as explained in Chapter two.
(iii) It also provides for code reusing through inheritance. When we have an existing
class and we want to define a new class with similar members as the existing
class, we just make this new class inherit the existing class and so avoid
redefining common members. This makes programs shorter and eases
programmer’s work.
(iv) This programming technology also provides for polymorphism feature that
ensures that something can be used in different ways. Similar to using an
operator in different ways (e.g. * is used in C++ as the multiplication operator, in
declaring pointers, in pointer dereferencing and in code documentation), we can
have many member functions with the same name, each in a different class (but
in inheriting classes), and each doing a different task. For example we can have a
member function named draw() in a class of square (for outputting a square), in
a class of circle (for outputting a circle), etc. Thus, in inheriting class, we are
allowed to give different functions in different classes the same name. And
generally, we say that we are using the same name to implement different
functionalities of the program, and this simplifies programming.
(v) Object oriented programming also provides for files programming that allows
for storage of data permanently on secondary storge devices.
(vi) It also provides for class templates. Similar to functions templates covered in
Chapter four whereby we can pass data types to a function, we can also pass data
types of data members of a class from the calling statement (statement to declare
an object). This allows us to declare many objects from a single class whose data
members have different data types.
All these seven features of object oriented programming are covered in the
succeeding chapters.
EXAMPLE 1
To define a class named A for storing two private members x and y, and a public
member z, whereby all the members are integers, we write
class A
{
private:
int x, y;
public:
int z;
};
private accessibility level is the default i.e. if we don’t use either of the keywords
private or public when defining members of a class, then the defined members will
be, by default, private. For example, m and n are private in class d defined below.
class d
{
int m, n;
};
EXAMPLE 2
The details stored for a time are hours, minutes and seconds (all integers). We can
define a class for time as;
class time
{
int hours, minutes, seconds;
};
EXAMPLE 1
Using class A in Example 1 of section 8.2.1 above, we can create objects of type A as
A a, b;
A a[20];
class A
{
private:
int x, y;
public:
int z;
}a, b;
Object a b
Private members xy xy
Public members z z
Illustration 1.1: Members of objects
That is, each of the newly created objects a and b gets a copy of the class’s members
i.e. x, y and z.
int x;
Here, we create a memory space named x for storing the range of values specified in
the definition of the integer data type i.e. whole numbers. x also takes 4 bytes as
specified in the data type.
Declaring an object of its class type similarly tells the compiler to allocate enough
memory space for storing the object (depending on its characteristics i.e. sizes of data
members and member functions).
Therefore classes are analogous to data types while objects are analogous to
variables. Classes are thus user defined data types. Structs of procedural
programming are another example of user defined data types.
We should thus not think of memory allocation for the class, since a class is just a
template for creating copies from – same as the data type from which variables are
created. Each declared object gets a copy of its class’s members, and we can then
proceed to access these members (but not the class’s members since they don’t really
exist).
Note that both declaring a variable and declaring an object have the same syntax as
shown above i.e.
Type instance;
EXAMPLE 2
We can use the class time defined in Example 2 of section 8.2.1 above to create two
objects as
10
Both time1 and time2 are objects of class time, and for each, we can store hours,
minutes and seconds.
EXAMPLE
Using class A and its objects a and b shown in section 8.2.2 above, we can assign
member z of object a value 9 as
a.z=9;
cin>>a.z;
Note that we can only call the public members when outside the class. Therefore, the
following statements would be erroneous using the above class.
void main()
{
11
A a[20];
The member z of each of the objects a[0], a[1], a[2], …, a[19] can be input as
cin>>a[0].z;
cin>>a[1].z;
cin>>a[2].z;
etc.
Or using a loop as
int c;
for (c=0; c<20; c++)
cin>>a[c].z;
POINTERS OF OBJECTS
From Chapter 7
Remember from chapter seven above that a pointer can be used to access various data
structures including variables, arrays and structs. For example to declare a pointer p
for pointing to an integer, we write
int *p;
p=&x;
12
cin>>*p;
Pointing to objects
Similarly, a pointer can point to an object. In this case, we can declare a pointer p for
pointing to objects of class A above (i.e. p is of type A) as
A *p;
p=&a;
(*p).z=9;
void main()
{
A a; // declare an object a of type A
A *p; // declare pointer p of type A
p=&a; // let p point to object a
(*p).z=9; // assign z of what p points to value 9
NOTE
13
A single pointer can point to different objects, but to only one at a time. For
example we could declare many objects of type A, and then have a single point
p being assigned to point to those different objects.
Note that when accessing a member inside another member (function), we do not
require the object name since we are already inside the class.
For instance, we can modify the above program to have a function to input the data
members and another one to output their values, then just invoke the functions when
outside the class as shown below.
void output()
{
cout<<“\nThe values are: ”<<x<<“ ”<<y<<“ ”<<z;
}
};
14
void main()
{
// create two objects
A a, b;
Member functions are therefore declared inside the class just like data members, and
called the same way we call data members, but depending on their return types. For
example, input() and output() above are void functions and so are called as
a.input(); b.input();
a.output(); b.output();
// define class A
class A
{
private:
int x, y, z;
public:
void input(); // protoype of input()
void output(); // prototype of output()
};
15
The advantage
The advantage of specifying member functions in this ways is the same advantage of
using function prototypes in procedural programming. When writing large programs,
it could be simpler to the programmer to first list the functionalities
(operations/functions) to be provided by each class, and then define the body of each
function later outside the classes.
Note the importance of specifying the class that a particular function belongs to
during outside class definition i.e. two classes could be having the same function
name, and so we must show which class a particular function belongs to.
We could for example, modify the immediate above program to use a pointer to point
to objects a and b, so that main() is as follows.
void main()
{
A a, b; // create objects a,b of type A
A *p; // create a pointer p of type A
16
void main()
{
A a, b; // create objects a,b of type A
A *p; // create a pointer p of type A
8.3.1: Problem 1
_________________________________________________________
The details stored for a time are hours, minutes and seconds. The operations
performed on any time are inputting the time, displaying the time, and incrementing
the time by one second. Define a class for this.
Solution
// class definition
class time
{
// data members
int hours, minutes, seconds;
public:
void input(); // input time
void output(); // output time
void increment(); // increment time
};
void time::input()
{
cout<<“\nInput the time: hours, minutes, seconds”;
17
cin>>hours>>minutes>>seconds;
}
void time::output()
{
cout<<“\nThe time is ”<<hours<<“hours, ”;
cout<<minutes<<“ minutes, and ”<<seconds<<“ seconds.”;
}
void time::increment()
{ // increment in modulo of 60
seconds++;
if (seconds==60)
{
minutes++;
seconds=0;
if (minutes==60)
{
hours++; minutes=0;
}
}
}
8.3.2: Problem 2
_________________________________________________________
The details that are stored for a circle are its radius, area and circumference. The
operations performed on any circle are inputting its radius, computing its area and
circumference, and outputting the area and the circumference. Write a program to
implement this.
Solution
#include <iostream>
using namespace std;
#define pi 3.14 // define constant pi=3.14
// class definition
class circle
{
// declare data members
private:
double radius, area, circumference;
void input_radius();
void compute();
void output();
};
void circle::compute()
{
area=radius*radius*pi;
circumference=2*pi*radius;
}
void circle::output()
{
cout<<“\nArea: ”<<area;
cout<<“\nCircumference: ”<<circumference;
}
void main()
{
// create three objects
circle a, b, c;
8.3.3: Problem 3
_________________________________________________________
19
The details that are stored concerning a line on the screen are the lines coordinates,
and the line’s length. The operations done on any line are getting the details of the
line (receiving the details as parameters), and plotting the line. Write a program that
uses a class of lines to implement this. The program should plot five lines.
NB: The origin is usually taken to be the top left corner of the screen. However, to
ease programming, consider the current cursor position as the origin.
Solution
#include <iostream>
using namespace std;
20
// main function
void main()
{
// Declare array instance of 5 lines
const int number=5;
lines line[number];
int c, x, y, length;
line[c].get_values(x, y, length);
line[c].plot();
}
}
8.3.4: Problem 4
_________________________________________________________
The details stored for an account include the account’s number and credit. The
operations performed on an account include opening a new account, depositing into
the account (affect the credit), and withdrawing from the account (affect the credit).
Write a program that uses a class of accounts to solve this problem.
Solution
/* Program to have deposits and withdrawals */
#include <iostream>
using namespace std;
class accounts
{
long number; // account number
double credit; // account credit
public:
// prototypes
void input_details(); // to input account details
21
void accounts::input_details()
{
cout<<“\nInput the new account number ”; cin>>number;
cout<<“\nInput account opening credit”; cin>>credit;
}
void accounts::show_details()
{
cout<<“\nAccount number: ”<<number<<“, Credit: ”<<credit;
}
long accounts::get_number()
{ return(number); }
double accounts::get_credit()
{ return(credit); }
void accounts::deposit(double a)
{ credit+=a; }
void accounts::withdraw(double a)
{ credit-=a; }
void main()
{
accounts account[maximum_accounts];
int count=0; // counter for accounts
int number, c;
double amount;
char choice=‘1’;
// repeating operations
while (choice==‘1’ || choice==‘2’ || choice==‘3’ || choice==‘4’)
{
// show menu
cout<<“\nInput 1 to record a new account”;
cout<<“\n 2 to deposit”;
cout<<“\n 3 to withdraw”;
22
// select operation to do
switch (choice)
{
case ‘1’: // opening new account --------
account[count].input_details(); count++;
cout<<“\nAccount added.\n”;
break;
class m
{
int x;
};
Every object of class m will have its own copy of the variable x that can take any
integer value.
24
A static data member on the other hand is a data member that is common to the
class i.e. it’s shared by all the objects of the class. So there is only one copy of the
member created when we declare as many objects of the class as possible.
Static data members are used to store values that are common to the entire class, for
example, as a counter of the number of objects created.
EXAMPLE 1
Consider the following program.
// define class A
class A
{ // declare static data member count
static int count;
public:
// increment count
void increment()
{ count++; }
// output count
void output()
{ cout<<“\ncount= ”<<count; }
25
};
int A::count; // define count
void main()
{ A a; a.increment(); a.output();
A b; b.increment(); b.output();
A c; c.increment(); c.output();
}
Discussion
When the program is run, the output is
count=1
count=2
count=3
The first time an object of class A is created (object a), count is initialized to zero.
We then increment count to be 1. When objects b is created, count is not initialized
to zero because of the first property above. We then increment count, so its value is
now 2. This is similar for object c.
So, every time the value of the static data member count is incremented by one
(irrespective of which object has called increment()), the new value is the old value
plus one. Thus, count behaves as a common member to the class.
Note the output that we would however get if count was a normal data member – the
output will always be value 1 (assuming the member is initialized to zero during
creation of the object) as shown in the program below.
EXAMPLE 2
We rewrite program in Example 1 above such that count is not static.
// define class A
class A
{
int count; // declare a data member count
public:
/* initialize count to 0 when object is created (using a special member
function known as a constructor) */
A()
26
{ count=0;}
// increment count
void increment()
{ count++; }
// output count
void output()
{ cout<<“\ncount=”<<count; }
};
void main()
{
A a; a.increment(); a.output();
A b; b.increment(); b.output();
A c; c.increment(); c.output();
}
Discussion
When the program is run, the output is
count=1
count=1
count=1
In this case, since count is not static, each created object has its own copy of count,
such that when increment() is called by the object, it increments count for that object
to 1.
A static member function can be called using the class name instead of object name as
class_name::function_name;
EXAMPLE
We can modify the above program that has a static member count so that we have
static functions increment() and output() as shown below.
#include <iostream>
using namespace std;
27
// define class A
class A
{ // declare static data member count
static int count;
public:
// static function to increment count
static void increment()
{ count++; }
int A::count;
void main()
{
A a; A::increment(); A::output();
A b; A::increment(); A::output();
A c; A::increment(); A::output();
}
EXAMPLE 1
The following program declares three times (time1, time2 and time3) of class time,
and assigns values of time3 the sum of time1 and time2. It uses the member function
sum() that receives two times (objects), gets their sum and assigns this sum to the
current (calling) object.
#include <iostream>
using namespace std;
class time
{
28
public:
// declare prototypes of member functions
void get_time(int, int);
void sum(time, time);
int get_hours();
int get_minutes();
};
/* function to assign the current time the sum of the first time and the second
time */
void time::sum(time t1, time t2)
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
void main()
{
time time1, time2, time3; /* declare three times */
time1.get_time(2, 45); /* assign values of time1 */
time2.get_time(3, 30); /* assign values of time2 */
29
EXAMPLE 2
We can modify above program so that the member function sum() returns a time
(object) instead of doing assignment of the total time to the calling object.
/* function to return a new time (object) which is the sum of the two times
passed as parameters */
time time::sum(time t1, time t2)
{
time t3;
t3.minutes=t1.minutes+t2.minutes;
t3.hours=t3.minutes/60;
t3.minutes=t3.minutes%60;
t3.hours=t3.hours+t1.hours+t2.hours;
return(t3);
}
30
class time
{
// declare data members
int hours, minutes;
public:
// declare prototypes of member functions
void get_time(int, int);
time sum(time, time);
int get_hours();
int get_minutes();
};
/* function to assign the current time the sum of the first time and the second
time */
time time::sum(time t1, time t2)
{
time t3;
t3.minutes=t1.minutes+t2.minutes;
t3.hours=t3.minutes/60;
t3.minutes=t3.minutes%60;
t3.hours=t3.hours+t1.hours+t2.hours;
return(t3);
}
void main()
31
{
// declare three times
time time1, time2, time3;
// assign values of time1
time1.get_time(2, 45);
// assign values of time2
time2.get_time(3, 30);
// let time3 = time1+time2
time3=time3.sum(time1, time2);
/* function to assign the current time the sum of the first time and the second
time */
void time::sum(time t1, time t2)
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
32
We can however decide to refer to the calling object inside a member function as
below.
EXAMPLE 3
Consider class A with one data member x and a function input() that inputs x as
shown below.
class A
{ int x;
public:
void input() { cin>>x; }
};
Assume we want to modify the class to add a function larger() that receives an object
and returns the object with the largest value of x i.e. either the current object or the
received object. We can write larger as
A larger(A a)
{
if (a.x>x)
return (a);
else return (*this);
}
A m, n;
m.input(); n.input();
A k;
k=m.larger(n);
Here, the statement k=m.larger(n); calls the member function larger() using object
m and passes object n. The function returns either the received object (i.e. n) or the
current object (object m referred to inside the function as *this).
ANALYSIS
We saw that during analysis stage, we specify outputs, inputs, and steps (or
processes). In short, we emphasize on the processes that will transform the inputs
into the outputs of the system.
DESIGN
At the design stage, we transform the above processes into a description of the
program’s logic using either pseudo codes or flowcharts.
ANALYSIS
Modeling characteristics, behavior, interactions of entities
Rather than just describe the processes in a system that transform the inputs into the
appropriate outputs, using object oriented programming technique, we model the
system i.e. include also the objects that are responsible for such transformation. We
show the characteristics and behaviors of those objects. We also show the
interactions between the various objects.
34
Different object oriented analysis and design methodologies exist, but the object
oriented analysis stage might be basically considered to consist of the following steps.
35
Basic stages
(i) Specify the basic requirements of the user. This is just like in procedural analysis.
We specify the expected outputs, then inputs that are needed to obtain those
outputs, and then processes that need to be executed to transform the inputs into
the outputs.
(ii) Identify the entities responsible for the above processes. Entities represent roles
which may include human users, external hardware or other systems. We also
identify the attributes of the entities. These are the characteristics mentioned
above.
For example in a sales company, various entities include customer, salesman, etc.
Attributes of a customer might be a person’s id, name, contacts, and location.
Attributes of a salesman on the other hand might be a person’s id, name, contacts,
department and salary.
(iii) Identify the services to be offered by each entity. These services are also known
as interfaces, and represent the behavior mentioned above. Also, list the steps
involved in each service.
For example, a customer’s services may be register, place order, deliver goods,
etc.
(iv) Show the interactions between the various entities in the system.
One tool used here is Entities Relationships Diagrams. It shows the entities as
well as linkages showing how they are related with one another.
Another tool popularly used here is the Data Flow Diagrams. It shows how data
flows in the system. It shows how data originates from the entities, is
transformed by the various services in the system, and goes to other entities as
results of processing.
Yet another tool is a Use Case Diagram that shows relationships among the
entities (or actors) and use cases (actions representing the interactions of the
entities i.e. the services). An actor is usually drawn as a named stick figure, while
a use case is drawn as an ellipse.
36
Receive goods
from salesman
DESIGN
The design stage uses the specifications of the analysis stage to produce a more
specific description of the system, similar to the design using the procedural
technique. Here, we produce a design of the various classes in the system, similar to
producing a design of the various functions in procedural technique.
One tool used here is the class diagrams. Using a methodology known as Unified
Modeling Language (UML), a class is represented by a rectangle divided into three
parts. The top part shows the class’s name, the middle part the data members and the
bottom part the member functions.
Example
Using the entities customer and salesman explained above in analysis stage, we
could have formed another entity (person) to store the common attributes of
customers and salesmen (i.e. id, name, contacts) during stage (iv) of showing
interactions between entities. In design stage, this is implemented through
inheritance.
We will have a class person storing (id, name, and contacts). Then the classes
customer and salesman will inherit this class and define their unique members (i.e.
37
location and department, salary respectively). A class diagram for this might look like
below.
person
Functions: register
customer salesman
8.6: Exercises
(c) The ___________ and ____________ keywords are used to show hidden
and unhidden members of a class respectively.
(e) The statement to call the member function named show() (a void function)
of object m is _________________________.
38
(b) The code to declare a class named q with only one member z (an integer and
a private) is ___________________________.
(c) Assume you want a class named m with one private data member named
m_data (a character), and one public void member function named
m_function() that simply inputs the value of m_data.
(i) Write the class m.
(ii) The statement to create an object named n of m is ____.
(iii) The statement to call m_function for object n is ______.
(iv) The statement to create a pointer named q for pointing to objects of m is
_________________________________.
(v) The statement to let q point to n is _________________.
(vi) Two equivalent statements each of which invokes m_function for n
using q are _____________________.
(e) Assume you want to modify class m above (in part 2.(c)) to include a
member named retrieve() that returns the value of m_data. This function
should also be defined outside the class.
(i) The declaration of retrieve is _____________________.
(ii) The definition of retrieve is ______________________.
(iii) The statements to declare a pointer named z of type m and have it point
to an object n of m are _____________.
(iv) The statement to invoke retrieve of n using z is ______.
(b) Private data members can not be accessed outside the class, but private
member functions can be.
39
(c) If a member is defined outside its class, then there must be a prototype of the
member inside the class definition.
(e) A static data member is common to all objects of the class no matter how
many they are.
(h) A difference between a usual member function and a static member function
is that the former is called using an object’s name while the later is called
using the class’s name.
2. (a) declare an array of objects of type A of size 20. The array name is a
(b) class q
{
int z;
};
(c) (i) class m
{
char m_data;
public:
void m_function()
{ cin>>m_data; }
};
(ii) m n;
(iii) n.m_function();
(iv) m *q;
40
(v) q=&n;
(vi) (*q).m_function();
q->m_function();
(d) (i) m p[50];
(ii) for (int c=0; c<50; c++)
p[c].m_function();
(e) (i) char retrieve();
(ii) char m::retrieve()
{ return(m_data); }
(iii) m *z;
z=&n;
(iv) cout<<z->retrieve();
8.6.2: Exercises
_________________________________________________________
1. (a) Explain the advantages of object oriented programming over procedural
programming.
(b) Explain the meanings of the terms class, object, and member.
(c) Show the syntax of defining members of a class, creating objects, and calling
members of a class.
(d) In what way is a member function different from the conventional user
defined function?
(e) Explain the difference between a data member of a class and the
conventional variables.
2. (a) What is an array of class objects? How are arrays of class objects defined in
C++?
(b) What are static data members? Why would we want to use static data
members? Also show the syntax and rules of using static data members.
(c) (i) Describe how we pass and return objects. Use sample code.
(ii) Write a program with a class named integer which contains one integer-
type data member, and a member function to input the value of the data
member. Include a member function that receives two objects as
parameters and returns the one that is larger than the other (has a larger
value of the integer).
41
void output()
{
cout<<“\nInput a ”; cin>>a;
cout<<“\nInput a ”; cin>>a;
}
void main()
{
// create objects y and z
x y z;
(b) The following shows a class of storing details of a cube. Write a complete
program using the class. Create an array of 10 cubes, and invoke the member
functions appropriately.
class cube
{ // declare data members
double length, width, height;
42
public:
// prototypes
void input(); // to input the data members
double get_surface_area(); // to return the area
double get_volume(); // to return the volume
};
4. The details that concern a student are the student’s registration number, name,
course, and fee amount paid. The operations done on a student include
registering a new student, outputting a student’s details and paying fees
(increment with the existing fee).
5. The details that are stored for any employee are the employee’s name,
department, and salary. The operations performed on any employee are to
register a new employee, output an employee’s details, and return the employees
salary.
6. For each item, we store the item’s code, name, price and quantity in stock. The
operations we can perform on any item include registering a new item,
purchasing an item (modify stock appropriately), and selling an item (input
quantity bought).
7. We can store the following details for each rectangle on the screen: the
rectangle’s length, width, the location on the screen (x and y coordinates – based
on the origin (top left corner of the screen)), and the fill-character of the
rectangle. Each rectangle can be drawn, cleared, or moved (by x and y values).
Required: Write a program using a class of rectangle to do the above using only
one rectangle.
Hints
Use a menu in the main function.
You need to get a C++ statement that clears the screen. To clear a rectangle,
you just need to clear the screen.
To output a rectangle, you need to clear the screen and then output it based
on its location and size.
43
8. Modify exercise 7 above such that you can output many rectangles on the screen.
Hint
To clear one rectangle, you need to clear the screen, and then redraw the
existing rectangles except the one to clear.
44