Modify and complete the definition of the class InhomList, which represents an inhomogeneous list.. ■ Write the destructor for the InhomListclass.The destructor releases the memory occup
Trang 1Modify and complete the definition of the class InhomList, which represents an inhomogeneous list.
■ Write the destructor for the InhomListclass.The destructor releases the memory occupied by the remaining list elements.
■ Implement the getPrev()method and both versions of the insert() andinsertAfter()methods.The algorithm needed for inserting list ele-ments was described in the section “Implementing an Inhomogeneous List.”
■ Implement the displayAll()method, which walks through the list sequentially, outputting each element.
■ Test insertion and output of list elements Check whether the comments
on the objects are output, if present.
■ Define the getPos()method, which locates the position of an element
to be deleted If the element is in the list, its address is returned Other-wise a NULL pointer is returned.
■ Write the erasePos()method, which deletes a list element at a given position Pay attention to whether the element to be deleted is the first
or any other element in the list Since the destructor for Cellwas
declared virtual, only one version of the deletePos()method is neces-sary.
■ Define the erase()method, which deletes a list element with a given name from the list.
■ Test deletion of list elements Continually display the remaining elements
in the list to be certain.
■ Now implement the copy constructor and assignment Use the insert()
to construct the list, calling the applicable version of the method.You can call the typeid()operator to ascertain the type of the list element currently to be inserted.The operator is declared in the header file typeinfo.
Example: if( typeid(*ptr) == typeid(DerivedEl))
The expression is true if ptrreferences a DerivedEltype object.
■ Then test the copy constructor and the assignment
Trang 2■ SOLUTION
// -// cell.h
// Defines the classes Cell, BaseEl, and DerivedEl
//
-#ifndef _CELL_
#define _CELL_
#include <string>
#include <iostream>
using namespace std;
class Cell
{ private:
Cell* next;
protected:
Cell(Cell* suc = NULL ){ next = suc; }
public:
virtual ~Cell(){ } Cell* getNext() const { return next; } void setNext(Cell* suc) { next = suc; }
virtual void display() const = 0;
};
class BaseEl : public Cell
{ private:
string name;
public:
BaseEl( Cell* suc = NULL, const string& s = "") : Cell(suc), name(s){}
// Access methods:
void setName(const string& s){ name = s; } const string& getName() const { return name; }
void display() const {
cout << "\n -"
<< "\nName: " << name << endl;
} };
Trang 3class DerivedEl : public BaseEl
{
private:
string rem;
public:
DerivedEl(Cell* suc = NULL,
const string& s="", const string& b="") : BaseEl(suc, s), rem(b){ }
// Access methods:
void setRem(const string& b){ rem = b; }
const string& getRem() const { return rem; }
void display() const
{
BaseEl::display();
cout << "Remark: " << rem << endl;
}
};
#endif
//
-// List.h : Defines the class InhomList
//
-#ifndef _LIST_H_
#define _LIST_H_
#include "cell.h"
class InhomList
{
private:
Cell* first;
protected:
Cell* getPrev(const string& s);
Cell* getPos( const string& s);
void insertAfter(const string& s, Cell* prev);
void insertAfter(const string& s,const string& b,
Cell* prev);
void erasePos(Cell* pos);
public:
InhomList(){ first = NULL; }
InhomList(const InhomList& src);
~InhomList();
InhomList& operator=( const InhomList& src);
void insert(const string& n);
void insert(const string& n, const string& b);
void erase(const string& s);
void displayAll() const;
};
#endif
Trang 4// -// List.cpp : The methods of class InhomList
//
-#include "List.h"
#include <typeinfo>
// Copy constructor:
InhomList::InhomList(const InhomList& src) {
// Append the elements from src to the empty list
first = NULL;
Cell *pEl = src.first;
for( ; pEl != NULL; pEl = pEl->getNext() ) if(typeid(*pEl) == typeid(DerivedEl)) insert(dynamic_cast<DerivedEl*>(pEl)->getName(),
dynamic_cast<DerivedEl*>(pEl)->getRem()); else
insert(dynamic_cast<BaseEl*>(pEl)->getName()); }
// Assignment:
InhomList& InhomList::operator=(const InhomList& src) {
// To free storage for all elements:
Cell *pEl = first,
*next = NULL;
while( pEl != NULL ) {
next = pEl->getNext();
delete pEl;
pEl = next;
}
first = NULL; // Empty list
// Copy the elements from src to the empty list
pEl = src.first;
for( ; pEl != NULL; pEl = pEl->getNext() ) if(typeid(*pEl) == typeid(DerivedEl)) insert(dynamic_cast<DerivedEl*>(pEl)->getName(),
dynamic_cast<DerivedEl*>(pEl)->getRem()); else
insert(dynamic_cast<BaseEl*>(pEl)->getName());
return *this;
}
Trang 5// Destructor:
InhomList::~InhomList()
{
Cell *pEl = first,
*next = NULL;
while( pEl != NULL )
{
next = pEl->getNext();
delete pEl;
pEl = next;
}
}
Cell* InhomList::getPrev(const string& n)
{
Cell *pEl = first,
*prev = NULL;
while( pEl != NULL )
{
if( n > dynamic_cast<BaseEl*>(pEl)->getName() )
{
prev = pEl; pEl = pEl->getNext();
}
else
return prev;
}
return prev;
}
Cell* InhomList::getPos( const string& n)
{
Cell *pEl = first;
while( pEl != NULL &&
(n != dynamic_cast<BaseEl*>(pEl)->getName()))
pEl = pEl->getNext();
if( pEl != NULL &&
n == dynamic_cast<BaseEl*>(pEl)->getName())
return pEl;
else
return NULL;
}
void InhomList::insertAfter(const string& s, Cell* prev)
{
if( prev == NULL ) // Insert at the beginning:
first = new BaseEl( first, s);
else // In the middle or at the end:
{ Cell* p = new BaseEl(prev->getNext(), s);
prev->setNext(p);
}
}
Trang 6void InhomList::insertAfter( const string& s,
const string& b, Cell* prev) {
if( prev == NULL ) // Insert at the beginning:
first = new DerivedEl( first, s, b);
else // In the middle or at the end: {
Cell* p = new DerivedEl(prev->getNext(), s, b);
prev->setNext(p);
} }
void InhomList::insert(const string& n) {
Cell* pEl = getPrev(n);
insertAfter(n, pEl);
}
void InhomList::insert(const string& n, const string& b) {
Cell* pEl = getPrev(n);
insertAfter(n, b, pEl);
}
void InhomList::erasePos(Cell* pos) {
Cell* temp;
if( pos != NULL) if( pos == first ) // Delete the first element {
temp = first;
first = first->getNext();
delete temp;
} else // Delete from the middle or at the end { // Get the predecessor
temp = getPrev( dynamic_cast<BaseEl*>(pos)
->getName()); if(temp != NULL) // and bend pointer
temp->setNext(pos->getNext());
delete pos;
} }
void InhomList::erase(const string& n) {
erasePos( getPos(n));
}
Trang 7void InhomList::displayAll() const
{
Cell* pEl = first;
while(pEl != NULL)
{
pEl->display();
pEl = pEl->getNext();
}
}
//
-// List_t.cpp : Tests the sorted inhomogeneous list
//
-#include "List.h"
int main()
{
InhomList liste1;
cout << "\nTo test inserting " << endl;
liste1.insert("Bully, Max");
liste1.insert("Cheers, Rita", "always merry");
liste1.insert("Quick, John", "topfit");
liste1.insert("Banderas, Antonio");
liste1.displayAll(); cin.get();
cout << "\nTo test deleting " << endl;
liste1.erase("Banderas, Antonio");
liste1.erase("Quick, John");
liste1.erase("Cheers, Rita");
liste1.displayAll(); cin.get();
cout << "\n -"
<< "\nGenerate a copy and insert an element "
<< endl;
InhomList liste2(liste1), // Copy constructor
liste3; // and an empty list
liste2.insert("Chipper, Peter", "in good temper");
liste3 = liste2; // Assignment
cout << "\nAfter the assignment: " << endl;
liste3.displayAll();
return 0;
}
Trang 95 8 7
Multiple Inheritance
This chapter describes how new classes are created by multiple
inheritance and explains their uses Besides introducing you to creating and destroying objects in multiply-derived classes, virtual base classes are depicted to avoid ambiguity in multiple inheritance.
Trang 10class MotorHome : public Car, public Home
{
private:
// Additional private members here
protected:
// Additional protected members here
public:
// Additional public members here };
Car Home
MotorHome
■ MULTIPLY-DERIVED CLASSES
The multiply-derived class MotorHome
Definition scheme for class MotorHome