You are on page 1of 4

CS 247 Mid-Term Name: ___Li Xiangyu_____

(25 Points for each question.)


1. Read tutorials on Topic 7- Constructors and Destructors, and complete the following tasks.
a. What is the purpose of constructors and destructors, respectively?

a constructor and a destructor are two special member functions of a class that are
automatically called when an object of that class is created or destroyed, respectively.

b. For the sample code segment below, provide one possible implementation for the constructor.

#include "q1.h"

Rectangle::Rectangle(){
width = 0.0;
length = 0.0;
}

2. Read tutorials on Topic 8- C-Strings, Parallel Arrays, and Buffer Overflows, and complete the following
tasks. Question:
a. What is buffer overflowing?

Buffer overflow occurs when data is input or written beyond the allocated bounds of an buffer, array,
or other object

b. What can buffer overflowing cause?

Overflowing can causing a program crash or a vulnerability that hackers might exploit.

c. How can we prevent buffer overflowing?


We can limit the number of characters it places in an array.

3. Read tutorials on Topic 9- Encapsulation and Data Hiding, and complete the following tasks.
Read the code, and answer the two questions below:
a. Why the data width and length are declared as private?

Users are not necessary to access these two data. Declaring private provides better control over the
data, and you can enforce constraints or validation rules on data access and modification if needed.

b. Why the five methods are defined as public?

Because we may need to outside classes access these five methods, using Public can provide a well-
defined interface for interacting with objects of the Rectangle class.

4. Read tutorials on Topic 10- Static Variables, Constructors, and Operator Overloading and Topic 11-
Inheritance, Polymorphism, Virtual Functions, and Dynamic Casting, and complete the following tasks.
Implement two classes of your own choice, and make sure that the two classes support the following
features of object-oriented programming:
a. Each class contains at least one constructor.
b. Each class contains a destructor.
c. One of the two classes is inherited from the other one.

#include <iostream>
class Circle
{
public:
Circle(double X, double Y, double R);
~Circle();

double getX() const;


void setX(double x);

double getY() const;


void setY(double y);
double getR() const;
void setR(double r);

double getArea() const;


double getPerimeter() const;

static int getCount();

private:
double x;
double y;
double r;
static int count;
};

#include "q4.h"
using namespace std;
int Circle :: count = 0;

Circle::Circle(double X,double Y,double R)


:x(X),
y(Y),
r(R)
{
++count;
}

Circle::~Circle(){
--count;
}

double Circle::getX() const{


return x;
}
void Circle::setX(double X){
x = X;
}

double Circle::getY() const{


return y;
}
void Circle::setY(double Y){
y = Y;
}

double Circle::getR() const{


return r;
}
void Circle::setR(double R){
r = R;
}

double Circle::getArea() const{


return 3.1415926535*r*r;
}

double Circle::getPerimeter() const{


return 2.0*3.1415926535*r;
}

int Circle:: getCount(){


return count;
}

int main(){
cout<<Circle::getCount()<<endl;

Circle c(1.0,1.0,1.0);
c.getCount();

cout<<Circle::getCount()<<endl;
return 0;
}

You might also like