0% found this document useful (0 votes)
108 views4 pages

CS 247 Midterm

The document contains the student's answers to questions on a midterm exam covering object-oriented programming concepts in C++. For question 1, the student defines the purpose of constructors and destructors as special member functions that are automatically called when an object is created or destroyed. For part b, the student provides a sample constructor implementation. For question 2, the student defines buffer overflow, explains what it can cause, and how it can be prevented. For question 3, the student explains why data members are declared private in the Rectangle class. For question 4, the student implements Circle and Sphere classes with constructors, destructors, inheritance, and other OOP features as requested

Uploaded by

karllixiangyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views4 pages

CS 247 Midterm

The document contains the student's answers to questions on a midterm exam covering object-oriented programming concepts in C++. For question 1, the student defines the purpose of constructors and destructors as special member functions that are automatically called when an object is created or destroyed. For part b, the student provides a sample constructor implementation. For question 2, the student defines buffer overflow, explains what it can cause, and how it can be prevented. For question 3, the student explains why data members are declared private in the Rectangle class. For question 4, the student implements Circle and Sphere classes with constructors, destructors, inheritance, and other OOP features as requested

Uploaded by

karllixiangyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy