This page is optimized for mobile devices, if you would prefer the desktop version just click here

1.1 Exercises  (Page 8/8)

szCarMake = new char[25];

strcpy(szCarMake, szMake);

dCarEngine = dEngine;

}

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 destructor to the class Auto .
  2. Include the class Auto in a working C++ program that creates two Auto objects on the heap, displays them and destroys them.

4. Given the following class:

class Employee{

public:

Employee(const char*, const char*);

char *getFirstName() const;

char *getLastName() const;

private:

char *firstName;

char *lastName;

};

Employee::Employee(const char *first, const char *last){

firstName = new char[strlen(first)+1];

strcpy(firstName, first);

lastName = new char[strlen(last)+1];

strcpy(lastName, last);

}

char *Employee::getFirstName() const{

return firstName;

}

char *Employee::getLastName() const{

return lastName;

}

void main(){

Employee *e1Ptr = new Employee(“Susan”, “Baker”);

Employee *e2Ptr = new Employee(“Robert”, “Jones”);

cout<<“\n Employee 1: “

<<e1Ptr->getFirstName()

<<“ “<<e1Ptr->getLastName()

<<“\n Employee 2: “

<<e2Ptr->getFirstName()

<<“ “<<e2Ptr->getLastName()<<endl;

delete e1Ptr;

delete e2Ptr;

}

Add an appropriate destructor to the class Employee .

5. Given the following program which defines and uses the class Ob. What is the output of the main() function?

class Ob

{

private:

int field;

public:

Ob();

~Ob();

}

Ob::Ob()

{

field = 0;

cout<<“Object created. “<<endl;

}

Ob::~Ob()

Cout<<“Object detroyed. “<<endl;

}

void main()

{

Ob obj[6];

}

6. Given a base class named Circle which is defined as follows.

const double PI = 3.14159;

class Circle

{

protected:

double radius;

public:

Circle(double = 1.0); // constructor

double calcval();

};

// implementation section for Circle

// constructor

Circle::Circle(double r)

{

radius = r;

}

// calculate the area of a circle

double Circle::calcval()

{

return(PI * radius * radius);

}

a. Derive from the base class another class named Cylinder which contains an additional data member to store length . The only additional function members of Sphere should be a constructor and a calcval() function that returns the volume of the cylinder (Note: Volume = length*(pi*radius^2)).

b. Define another derived class name Sphere from the base Circle class. The only additional class members of Sphere should be a constructor and a calcval() function that returns the volume of the spere. (Note: Volume = 4/3*pi*radius^3).

c. Write the main() function in such a way that your program call all of the member functions in the Cylinder class and Sphere class.

7. Create a base class named Point that consists of an x and y coordinate. From this class, derive a class named Circle that has an additional data member named radius . For this derived class, the x and y data members represents the center coordinates of a circle. The function members of the first class should consist of a constructor and a distance() function that returns the distance between two points, where

distance = square-root((x2 – x1)^2 + (y2 – y1)^2)

Additionally, the derived class should have a constructor, an override distance() function that calculates the distance between circle centers, and a function named area that returns the area of a circle.

Include the two classes in a complete C++ program. Have your program call all of the member functions in each class. In addition, call the base class distance function with two Circle objects and explain the result returned by the function.

8. Create a base class named Rectangle that contains length and width data members. From this class, derive a class named Box having an additional data member named depth . The function members of the base Rectangle class should consist of a constructor and an area function. The derived Box class should have a constructor and an override function named area that returns the surface area of the box and a volume() function.

Include the two classes in a complete C++ program. Have your program call all of the member functions in each class and explain the result when the area() function is called using a Box object.

9. Modify the following code so that the overridden setSound() function executes correctly with the statements in the main() method.

class Instruments

{

public:

Instruments();

void setSound(char*);

char* getSound();

protected:

char* szSound;

}

Instruments:: Instruments(){

szSound = new char[25];

}

void Instruments::setSound(char* szSound){

szSound = “quiet”;

}

char* getSound(){

return szSound;

}

class Percussion: public Instruments

{

public:

Percussion();

void setSound(char*);

}

Percussion::Percussion(){

cout<<“I repeat, the drum goes “

<<getSound()<<endl;

}

void Percussion::setSound(char* szSound){

szSound = “boom”;

}

void main(){

Instruments* drums = new Percussion;

drums->setSound();

cout<<“The drum goes “<<drums->getSound()<<endl;

}

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.