Saturday, October 24, 2009

An Array of Objects

An Array of Objects

This example show how to declare and use an array of an object

#include struct Paper{public: void GetDimensions(); void Properties();private: double Perimeter() const; double Area() const; double Height; double Width;};void Paper::GetDimensions(){ cout << "Enter the dimensions of the label\n"; cout << "Height: "; cin >> Height; cout << "Width: "; cin >> Width;}void Paper::Properties(){ cout << "\n\tHeight = " << twidth = " << Width; cout << " tperimeter = " << Perimeter(); cout << " tarea = " << Area(); cout << ">> Samples; for(int i = 0; i < Samples; ++i) Label[i].GetDimensions(); cout << "\n\nHere are the characteristics of your labels\n"; for(int j = 0; j < Samples; ++j) { cout << "Label No. " << j; Label[j].Properties(); }}

Here is an example of running the program:

How many sample labels do you want? 2Enter the dimensions of the labelHeight: 3.25Width: 3.25Enter the dimensions of the labelHeight: 2.15Width: 4.55Here are the characteristics of your labelsLabel No. 0 Height = 3.25 Width = 3.25 Perimeter = 12.5625 Area = 10.5625Label No. 1 Height = 2.15 Width = 4.55 Perimeter = 11.7825 Area = 9.7825

No comments:

Post a Comment

Popular Posts