![]() |
Class Abstraction |
|
Overview |
|
So far, we have been able to create classes and inherit from them. Here is an example of a simple class we created in the previous lesson: |
| Header File: Rectangle.h | |
#pragma once
using namespace System;
public ref class CRectangle
{
private:
double len;
double hgt;
public:
property double Length
{
double get() { return len; }
void set(double L)
{
if( L <= 0 )
len = 0;
else
len = L;
}
}
property double Height
{
double get() { return hgt; }
void set(double h)
{
if( h <= 0 )
hgt = 0;
else
hgt = h;
}
}
property double Perimeter
{
double get() { return 2 * (Length + Height); }
}
property double Area
{
double get() { return Length * Height; }
}
public:
CRectangle();
CRectangle(double length, double height);
void CRectangle::ShowCharacteristics();
};
|
|
| Source File: Rectangle.cpp | |
#include "Rectangle.h"
CRectangle::CRectangle()
: len(0.00), hgt(0.00)
{
}
CRectangle::CRectangle(double length, double height)
: len(length), hgt(height)
{
}
void CRectangle::ShowCharacteristics()
{
Console::WriteLine(L"Rectangle Characteristics");
Console::WriteLine(L"Length: {0}", this->Length);
Console::WriteLine(L"Height: {0}", this->Height);
Console::WriteLine(L"Perimeter: {0}", this->Perimeter);
Console::WriteLine(L"Area: {0}", this->Area);
}
|
|
| Source File: Exercise.cpp | |
#include "Rectangle.h"
using namespace System;
int main()
{
CRectangle ^ rect = gcnew CRectangle(18.64, 28.42);
rect->ShowCharacteristics();
Console::WriteLine();
return 0;
}
|
|
This would produce:
Rectangle Characteristics Length: 18.64 Height: 28.42 Perimeter: 94.12 Area: 529.7488 Press any key to continue . . .
|
Imagine you want to create a rectangular parallelepiped. Using the above CRectangle class, you certainly would not have to start from scratch. You can derive from this class and create a new one. When inheriting from a class, a base class such as the above CRectangle can be configured to provide its children with the basic foundation they would need. Although a child class can implement a new behavior not available on the parent class, sometimes the derived class will need a customized implementation of a behavior that has already been configured in its parent. For example, if you derive a box from a rectangle, since a box has 6 faces, when creating the area of the box, you certainly would not expect the have the same value as that of the parent. Consider this: |
|
|
#pragma once
using namespace System;
public enum class ItemsCategories
{
Unknown,
CablesAndConnectors,
CellPhonesAndAccessories,
Headphones,
DigitalCameras,
PDAsAndAccessories,
TelephonesAndAccessories,
TVsAndVideos,
SurgeProtectors,
Instructional
};
namespace ElectronicsStore
{
public ref class CStoreItem
{
public:
// An item whose characteristics are not (yet) defined
CStoreItem(void);
// An item that is known by its make, model, and unit price
CStoreItem(long itmNbr, String ^ make,
String ^ model, double unitPrice);
// An item that is known by its name and unit price
CStoreItem(long itmNbr, String ^ name, double unitPrice);
// An item completely defined
CStoreItem(long itmNbr, ItemsCategories category,
String ^ make, String ^ model, double unitPrice);
~CStoreItem();
private:
long nbr;
ItemsCategories cat;
String ^ mk;
String ^ mdl;
String ^ nm;
double price;
public:
property long ItemNumber
{
long get() { return nbr; }
void set(long n) { this->nbr = n; }
}
property ItemsCategories Category
{
ItemsCategories get() { return cat; }
void set(ItemsCategories c) { this->cat = c; }
}
property String ^ Make
{
String ^ get() { return mk; }
void set(String ^ m) { this->mk = m; }
}
property String ^ Model
{
String ^ get() { return mdl; }
void set(String ^ m) { this->mdl = m; }
}
property String ^ Name
{
String ^ get() { return nm; }
void set(String ^ n) { this->nm = n; }
}
property double UnitPrice
{
double get() { return price; }
void set(double p) { this->price = p; }
}
};
}
|
#include "StoreItem.h"
namespace ElectronicsStore
{
CStoreItem::CStoreItem(void)
{
nbr = 0;
cat = ItemsCategories::Unknown;
mk = L"Unknown";
mdl = L"Unspecified";
nm = L"N/A";
price = 0.00;
}
CStoreItem::CStoreItem(long itmNbr, String ^ make,
String ^ model, double unitPrice)
{
nbr = itmNbr;
cat = ItemsCategories::Unknown;
mk = make;
mdl = model;
nm = L"N/A";
price = unitPrice;
}
CStoreItem::CStoreItem(long itmNbr, String ^ name,
double unitPrice)
{
nbr = itmNbr;
cat = ItemsCategories::Unknown;
mk = L"Unknown";
mdl = L"Unspecified";
nm = name;
price = unitPrice;
}
CStoreItem::CStoreItem(long itmNbr, ItemsCategories category,
String ^ make, String ^ model,
double unitPrice)
{
nbr = itmNbr;
cat = category;
mk = make;
mdl = model;
price = unitPrice;
}
CStoreItem::~CStoreItem()
{
}
}
|
#include "StoreItem.h"
using namespace System;
int main()
{
String ^ strTitle = L"=-= Nearson Electonics =-=\n"
L"******* Store Items ******";
Console::WriteLine();
return 0;
}
|
|
Virtual Members |
|
|
||
| Previous | Copyright © 2006 FunctionX, Inc. | Next |
|
|
||