Matthew wrote:Structures aren't complex. They are like classes but have different syntax and less features (No methods). They simply combine different piece of data together. To store the size of an array with an array, a structure is required.
I meant more a less that I wasn't expecting them to come into play for a simple function. I finally got the hang of pointers/free store/ect so that should be less trouble.
I spent today playing with virtual functions and came up with this incredibly useless piece of code:
Code: Select all
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
Animal() {cout << "Animal Constructor...\n";}
virtual ~Animal() {cout << "Animal Destructor...\n";}
virtual void Speak() {cout << "I am an animal\n";}
virtual void Feed() {cout << "Thank you for the food\n";}
virtual void Pet() {cout << "Thank you for the petting\n";}
};
class Pig : public Animal
{
public:
Pig() {cout << "Pig Constructor...\n";}
virtual ~Pig() {cout << "Pig Destructor...\n";}
virtual void Speak() {cout << "Oink!\n";}
virtual void Feed() {cout << "*Crunch* *Crunch* *Crunch*...\n";}
virtual void Pet() {cout << "*Grumble*\n";}
};
class Horse : public Animal
{
public:
Horse() {cout << "Horse Constructor...\n";}
virtual ~Horse() {cout << "Horse Destructor...\n";}
virtual void Speak() {cout << "Naaay!\n";}
virtual void Feed() {cout << "*Russle* *Russle*...\n";}
virtual void Pet() {cout << "*Shwishhhh*\n";}
};
class Chicken : public Animal
{
public:
Chicken() {cout << "Chicken Constructor...\n";}
virtual ~Chicken() {cout << "Chicken Destructor...\n";}
virtual void Speak() {cout << "Bawkak!\n";}
virtual void Feed() {cout << "*Peck* *Peck* *Peck* *Peck*...\n";}
virtual void Pet() {cout << "*Crrrr*\n";}
};
class SMALL_SIZE
{
public:
SMALL_SIZE() {cout << "SMALL_SIZE Constructor...\n";}
~SMALL_SIZE() {cout << "SMALL_SIZE Destructor...\n";}
};
int main()
{
Animal *ptr;
SMALL_SIZE useless;
int choice;
string animal;
cout << "\nAnimal = " << sizeof(Animal) << " bytes\n";
cout << "Pig = " << sizeof(Pig) << " bytes\n";
cout << "Horse = " << sizeof(Horse) << " bytes\n";
cout << "Chicken = " << sizeof(Chicken) << " bytes\n";
cout << "SMALL_SIZE = " << sizeof(SMALL_SIZE) << " bytes\n\n";
cout << "What type of animal would you like?\n";
cout << "Pig<1>\nHorse<2>\nChicken<3>";
cout << "\n> ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n";
ptr = new Pig;
animal = "pig";
break;
case 2:
cout << "\n";
ptr = new Horse;
animal = "horse";
break;
case 3:
cout << "\n";
ptr = new Chicken;
animal = "chicken";
break;
default:
cout << "\n";
ptr = new Animal;
animal = "animal";
break;
}
cout << "\nWhat would you like to do with your " << animal << "?\n";
cout << "Listen to it<1>\nFeed it<2>\nPet it<3>";
cout << "\n> ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n";
ptr->Speak();
break;
case 2:
cout << "\n";
ptr->Feed();
break;
case 3:
cout << "\n";
ptr->Pet();
break;
default:
cout << "\nWrong input!\n";
}
cout << "\n";
delete ptr;
return 0;
}
Then later made worked on the algorithm for a program that draws names for buying Christmas gifts for our family:
Code: Select all
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <vector>
int main()
{
using namespace std;
string familyNamesA[7] = {"Scott", "Derek", "Tanya", "Lindsay", "Jesse", "Sarah", "Tyler"};
vector <string> drawnNames;
int randName;
for (int i = 0; i < 7; i++)
drawnNames.push_back(familyNamesA[i]);
srand( (unsigned)time(0) );
for (int i = 0; i < 7; i++)
{
randName = (rand() % drawnNames.size());
if (drawnNames.size() == 0)
cout << "\nNo more names left\n";
else
{
cout << familyNamesA[i] << " buys for ";
if (familyNamesA[i] == drawnNames[randName] && drawnNames.size() == 1)
{
cout << string(100, '\n');
cout << "NEW LIST\n\n";
for (int j = 0; j < 7; j++)
drawnNames.push_back(familyNamesA[j]);
i = 0;
}
else
{
while (familyNamesA[i] == drawnNames[randName])
randName = (rand() % drawnNames.size());
cout << drawnNames[randName] << "\n";
drawnNames.erase(drawnNames.begin()+(randName));
}
}
}
cout << "\nPress ENTER to exit\n";
cin.get();
return 0;
}