Table of Contents
:
PegClass
Towers of Hanoi
TopList Class
Vector Library
Vectors As Parameters
Vector Constructors
Searching an Array
Dynamic Array
Matrix Library
Struct
Complex Struct
Constructors
Array of Structs
Creating Classes
Mouse Click Events
Using Buttons
Projects
I. PegClass
a. In lvp folder
XXXX|XXXX ? ring of size 4
X| X ? ring of size 1
_XXX|XXX_ ? ring of size 3
Peg
b. Peg Class
i. Constructor: PegClass(int height);
ii. Methods
iii. Void AddRing(intSize);
iv. Int RevmoveRing();
v. Int TopRing();
vi. Int RingsOnPeg();
vii. Void ShowAll();
II. Towers of Hanoi
Void moveRing(PegClass &FromRing, PegClass &ToPeg)
{
int size = FromPeg.RemoveRing();
ToPeg.AddRing(size);
}
Int main()
{
moveRing(Left, Right);
moveRing(Left, Mid);
moveRing(Right, Mid);
moveRing(Left, Right);
moveRing(Mid, Left);
moveRing(Mid, Right);
moveRing(Left, Right);
return 0;
}
III. TopList Class
a. #include
b. Can be used to create a top ten list of scores
c. Methods
i. String GetNames(int Rank);
ii. Long GetScore(int Rank);
iii. Void AddItem(long score, String name);
iv. Void Clear();
v. Int GetListSize()
vi. Void Display(ostream &os);
d. Create a list of size 3, with the following names & scores Bill, 1000; Jen, 750; Joe, 240;
IV. Vector Library
a. A vector class to create an array object.
b. Instantiate a vector object:
i. Vector ArrayName(# size);
c. vector name(9)
i. Holds 9 names indexed from 0 to 8
d. vector lotto(7);
i. Holds 7 numbers indexed from 0 to 6
e. vector numbers (15);
i. Holds 15 numbers indexed from 0 to 14
Include
Int main()
{
Vector name(4);
For (int i = 0; I < name.length(); I ++)
{
Cout<<”Enter you name”<>name[i];
}
Name.resize(5);
Cout<<”Enter your name”;
Cin>>name[4];
For(int x = 0; x < name.length(); x++)
Cout<
V. Vectors as Parameters
a. Formal parameters: vector &ArrayName;
b. Actual parameter: ArrayName
c. Int sum(const vector &num) ? by reference
i. For (int I = 0; I < num.length(); I ++)
1. sum += num[i];
ii. return sum;
d. Int main()
e. {
i. Vector grade(4);
ii. For(int I = 0; I< grade.lenght, I ++)
1. cout<<”enter a grade”;
2. cin>>grade[i];
iii. cout<
VI. Vector Constructors
a. Vector num (10, 4); //Makes 10 double elements all initialized to 4>
b. Vector job(9, “Empty”); //Creates 9 elements holding word empty>
c. Vector number; //Creates a blank array>
VII. Searching an Array
a. Int search(const vector &list, String key)
i. Int len = list.length();
ii. For (int I = 0; I
iii. {
1. if (key == list[i])
a. return I;
iv. }
v. Return -1;
VIII. Dynamic Array
a. Array that change in size(grow/ shrink)
void increaseArray(vector &number, int newValue)
{
Number.resize(1+number.lenght());
Number[number.length()-1] = newValue;
}
void removeAndResize(vector &id, String key)
{
Int position = search(id, key);
If (position >= 0)
{
For(int I = position; I
IX. Matrix Library
a. To dimensional array class
b. #include
c. Instantiate an object of matrix class:
d. matrix grid(#rows, #columns, #initalize)
e. Create a 9 by 5 table with the letter ‘Q’ in every location
i. matrix grid(9, 5, ‘Q’);
f. Create a table with 7 rows and 2 columns with “Empty” in every location
i. matrix grid(7, 2, “Empty”);
X. Struct
a. User defined data type
b. Can store several different variable types, grouped together.
c. Ex.
i. struct MarineFish
ii. {
1. String CommonName;
2. String scientificName;
3. double maxLenght;
4. int minAquariumSize;
iii. }; ? must put semicolon after the bracket to end struct
Int main()
{
marineFish myFish;
myFish.commonName = “Kole Tang”;
myFish.scientificName = “Ctenchaetus Strigous”;
myFish.MaxLength = “7.1;
myFish.minAquariumSize = 75;
display(myFish); ? function that will display values
XI. Complex Struct
a. Add an array of strings to a struct
b. Struct marineFish
c. {
d. ...
e. vector food;
f. }
XII. Constructors
a. Initializes the values of the struct(int datatypes have constructors too)
b. Int num(5); //initalizes num to 5
Format of Constructor with Struct
Struct marineFish
{ marineFish();//constructor coded after struct definition String
CommonName; String scientificName; double maxLenght; int
minAquariumSize; vector food; }; //constructor
MarineFish::MarienFish() :commonNmme(“Kole Tang”),
scientificName(“Cen…”), maxLength(7.1), minAquariumSize(75), food(2),
food[0](“alge”), food[1](“broccoli”); ^^initializer list
XIII. Array of structs
a. Vector myTank(5);
b. myTank[0].commonName = “Hawkfish”;
c. …
d. for(int I = 0; I< myTank.length(); I++)
e. {
i. Cout<<”Enter the common name of the fish”<
ii. Getline(cin, myTank[i].commonName);
iii. Cout<<”Enter the max length”;
iv. Cin>>myTank.maxLength
XIV. Creating Classes
a. Similar to a struct with built in methods
b. A class has private data & public methods
i. (encapsulation/ information hiding)
c. Two files created
i. 1. Header file (.h) outline of class/ Interface file
ii. 2. Implementation file (.cpp)- prototyped methods are codedM
Class Fish
{
Public:
Fish();
Fish(string name, double len, int tank);
Void display();
Double getLength();ßaccessor methods returns private data
Int getTankSize(); ß accessor methods returns private data
String getName(); ß accessor methods returns private data
Void setLength(double len); ß Mutator method changes private data
Void setTankSize(int size); ß Mutator method changes private data
Void setName(String n); ß Mutator method changes private data
Private:
String name;
Int minTankSize;
Double maxLength;
};
#inlcude “Fish.cpp”
//Fish.cpp (implementation file)
#include
#include
Fish::Fish()
:name (”None”), maxLength(0), minTankSize(0)
{
}
Fish::Fish(String aName, double maxLength, int tank)
: name(aName), maxLength(len), minTankSize(tank)
{
}
Double fish::getLength
{
Return maxLength;
}
Int Fish::getTankSize
{
Return minTankSize;
}
String Fish::getName
{
Return name;
}
Void Fish::setLength(double len)
{
maxLength = len;
}
Void Fish::setTankSize(int tank)
{
minTankSize = tank;
}
Void Fish::setName(String n)
{
Name= n
}
Void Fish::display()
{
Cout<<”The fish “<
XV. Mouse Click Events
a. void GuiClass::GuiMouseClick(int x, int y)
b. create private variables;
i. int lastX;
ii. int lastY;
c. to store mouse click location
EX.
void GuiClass::GuiMouseClick(int x, int y)
{
lastX =x;
lastY=y;
}
void GuiClass::GuiPaint()
{
Line(0,0,lastX, lastY);
}
XVI. Using Buttons
a. Copy and paste ButtonClass above GuiClass **but below the #include**
b. Add a private variable of ButtonClass to GuiClass
c. Initialize the button in the default constructor or use the setButton method of ButtonClass
EX:
GuiClass::GuiClass()
:myButton(“Click Me”, l00, 200, 170, 190);
{
}
void GuiClass::GuiMouseClick(int x, int y)
{
Lastx=x;
lastY = y;
if(myButton.IsHit(lastX, lastY))
messageBox(“HI”, “Button Example”);
void GuiClass::GuiPaint()
{
myButton.Paint();
}
XVII.
XVIII.
XIX.
XX.
XXI.
XXII.
XXIII.
XXIV.
Function Header: void functionName(formalParam)
XXV.
XXVI.
XXVII.
XXVIII.
XXIX.
XXX.
XXXI.
XXXII.
XXXII.