Professional Documents
Culture Documents
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
Overflowing can causing a program crash or a vulnerability that hackers might exploit.
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.
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();
private:
double x;
double y;
double r;
static int count;
};
#include "q4.h"
using namespace std;
int Circle :: count = 0;
Circle::~Circle(){
--count;
}
int main(){
cout<<Circle::getCount()<<endl;
Circle c(1.0,1.0,1.0);
c.getCount();
cout<<Circle::getCount()<<endl;
return 0;
}