<< Chapter < Page Chapter >> Page >

}

~Vector(){

if (value!=NULL)

delete value;

}

};

void main()

{

Vector v(5);

v.showdata();

Vector v2(v);

v2.showdata();

}

a. Explain why the program incurs one memory error at run-time.

b. Now add the following code segment in the Vector class definition.

Vector(const Vector&v){

dimension = v.dimension;

value=new int[dimension];

for (int i=0; i<dimension; i++)

value[i]=v.value[i];

}

Check if the program still incurs the memory error or not. Explain why.

2.3) a. Test the following program in a Visual C++ environment:

class some{ // code segment a

public:

~some() {

cout<<"some's destructor"<<endl;

}

};

void main() {

some s;

s.~some();

}

What is the output of the above program during execution? Explain the output.

b. Test the following program in Visual C++ environment:

class some{ // code segment b

int *ptr;

public:

some(){

ptr= new int;

}

~some(){

cout<<"some's destructor"<<endl;

if (ptr!=NULL){

cout<<"delete heap memory"<<endl;

delete ptr;

}

}

};

void main()

{

some s;

// s.~some();

}

What is the output of the above program during execution? Explain the output.

c. In the main() function of the program in b, if we remove the two slashes (//) before the statement s.~some(); then what the result is when the program is executed ? Explain why.

2.4) Given the class definition as follows:

class Auto {

public:

Auto(char*, double);

displayAuto(char*, double);

private:

char* szCarMake;

double dCarEngine;

};

Auto::Auto(char* szMake, double dEngine){

szCarMake = new char[25];

strcpy(szCarMake, szMake);

dCarEngineSize = dCarEngine;

}

Auto::displayAuto(){

cout<<“The car make: “<<szCarMake<<endl;

cout<<“The car engine size: “<<dCarEngine<<endl;

}

void main(){

Auto oldCar(“Chevy”, 351);

Auto newCar(oldCar);

oldCar.displayAuto();

newCar.displayAuto();

}

  1. Add an appropriate copy constructor to the Auto class .
  2. Add an appropriate destructor to the Auto class .

c. Run all the modifications in a C++ environment.

Lab session 10: inheritance

1. objective

The objectives of Lab session 10 are (1) to get familiar with class inheritance; (2) to learn the workings of the constructor and destructor of a derived class.

This lab session will use the inheritance hierarchies in the following figure.

2. experiment

2.1) Given the Point class defined as follows.

class Point{

private:

int color;

protected:

double x;

double y;

public:

Point(double x=0, double y=0){

this->x=x; this->y=y;

}

void move(double dx, double dy){

x=x+dx;

y=y+dy;

}

~Point(){

cout<<"Destructor Point called";

}

};

Derive from the class Point, another class named Point_Derive1 class, which is defined as follows.

class Point_Derive1:public Point{

private:

double z;

public:

Point_Derive1();

void move(double dx, double dy, double dz);

~Point_Derive1();

};

a. List out the data members and member functions of the Point_Derive1 class. And determine the access specifier of each of these members.

Derive from the class Point, another class named Point_Derive2 class, which is defined as follows.

class Point_Derive2:protected Point{

private:

double z;

public:

Point_Derive1();

void move(double dx, double dy, double dz);

~Point_Derive1();

};

b. List out the data members and member functions of the Point_Derive2 class. And determine the access specifier of each of these members.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask