Extra Lab: Defining Classes

Riyadh Philanthropic Society For Science
Prince Sultan College For Woman
Dept. of Computer & Information Sciences
CS 102
Computer Programming II
(Extra Lab: Defining Classes)
Exercise
Write a class Point, that might be used to store and manipulate the
location of a point in a plane. The class contains two variables: x and
y and the following functions:
• A default constructor that sets default values for all variables.
• A set function that sets the variables.
• A move function that moves the point by an amount along the
vertical and horizontal directions specified by the first and
second arguments.
• One get function for each private variable that returns the
current value.
Test your class. Note that there is no user input for this exercise.
Input to the program is hard-coded in the main function.
Extra Lab: Defining Classes
1
Solution
#include<iostream>
using namespace std;
class Point
{
public:
Point();
void set(double x_val, double y_val);
void move(double move_x, double move_y);
double get_x();
double get_y();
private:
double x, y;
};
continue
Extra Lab: Defining Classes
2
Solution
int main()
{
Point p1, p2;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
p1.set(1.0, 2.0);
p2.set(2.0, -1.0);
cout << "p1 = (" << p1.get_x() << "," << p1.get_y() << ")\n";
cout << "p2 = (" << p2.get_x() << "," << p2.get_y() << ")\n";
p1.move(0.8, -0.5);
cout << "New p1 = (" << p1.get_x() << "," << p1.get_y() << ")\n";
return 0;
}
Point::Point()
{
x = 0;
y = 0;
}
Extra Lab: Defining Classes
continue
3
Solution
void Point::set(double x_val, double y_val)
{
x = x_val;
y = y_val;
}
void Point::move(double move_x, double move_y)
{
x += move_x;
y += move_y;
}
double Point::get_x()
{
return x;
}
double Point::get_y()
{
return y;
}
Extra Lab: Defining Classes
4
Exercise
Write a class Circle.
The class contains two private variables:
Point center;
int radius;
The class also contains the following member functions:
Circle();
Circle(Point the_point, int the_radius);
Point get_center();
int get_radius();
void move(Point p); //moves the circle to the specified coordinates
void display();
//displays the point and the radius
Test your class. Note that there is no user input for this exercise.
Input to the program is hard-coded in the main function.
Extra Lab: Defining Classes
5
Solution
#include<iostream>
using namespace std;
class Point
{
public:
Point();
void set(double x_val, double y_val);
void move(double move_x, double move_y);
double get_x();
double get_y();
private:
double x, y;
};
continue
Extra Lab: Defining Classes
6
Solution
class Circle
{
public:
Circle();
Circle(Point the_point, int the_radius);
Point get_center();
int get_radius();
void move(Point p);
void display();
private:
Point center;
int radius;
};
continue
Extra Lab: Defining Classes
7
Solution
int main()
{
Point p1, p2;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
p1.set(1, 2);
Circle c1(p1, 5), c2;
cout << "c1:\n";
c1.display();
cout << "c2:\n";
c2.display();
c2.move(p1);
p2 = c2.get_center();
cout << "c2:\n"
<< "Point = (" << p2.get_x() << "," << p2.get_y() << ")\n"
<< "Radius = " << c2.get_radius() << endl;
return 0;
continue
}
Extra Lab: Defining Classes
8
Solution
Point::Point()
{
x = 0;
y = 0;
}
void Point::set(double x_val, double y_val)
{
x = x_val;
y = y_val;
}
void Point::move(double move_x, double move_y)
{
x += move_x;
y += move_y;
}
double Point::get_x()
{
return x;
}
double Point::get_y()
{
return y;
}
Extra Lab: Defining Classes
continue
9
Solution
Circle::Circle()
{
center.set(0, 0);
radius = 0;
}
Circle::Circle(Point the_point, int the_radius)
{
center = the_point;
radius = the_radius;
}
Point Circle::get_center()
{
return center;
}
int Circle::get_radius()
{
return radius;
}
Extra Lab: Defining Classes
continue
10
Solution
void Circle::move(Point p)
{
center = p;
}
void Circle::display()
{
cout << "Point = (" << center.get_x() << "," << center.get_y() << ")\n"
<< "Radius = " << radius << endl;
}
Extra Lab: Defining Classes
11