Quote of the Week
"Curiosity is about Wisdom."
- Professor Wang
Let's take a look back at the previous week and what we accomplished in code. We're still on that C++ grind and it's the first week of my sixth semester in college.
As you can see from the picture above, we first started by identifying how to use class objects and methods, using a theoretical dog. His name is Fred, say HELLO!🐶
class MyDog {
protected: //This is the perfered method of making non-publically accessable values in C++
string _name;
int _weight = 300;
bool _isHealthy = false;
public:
//Properties
string getName()
{
return _name;
}
int getWeight()
{
return _weight;
}
void setWeight(int weight)
{
if (weight > 0)
{
_weight = weight;
}
}
void setIsHealthy(bool isHealty)
{
if (_weight > 200)
{
_isHealthy = false;
}
else
{
_isHealthy = true;
}
}
//Methods
MyDog(string name);
void DoDogRun();
};
MyDog::MyDog(string name)
{
if (name.length() == 0) throw "Error: Couldn't create my Dog.";
MyDog::_name = name;
}
void MyDog::DoDogRun()
{
if (MyDog::_isHealthy)
cout << MyDog::_name << "is running!" << endl;
else if (MyDog::_weight > 200)
cout << MyDog::_name << "is too fat to run!" << endl;
else
cout << MyDog::_name << "Is unhealty; see vet first" << endl;
}
int main() {
MyDog* thisDog;
try {
thisDog = new MyDog("Fred");
}
catch (const char* msg) {
cerr << msg << endl;
return -1;
}
cout << thisDog->getName() << "needs excercise" << endl;
thisDog->DoDogRun();
thisDog->setWeight(100);
thisDog->DoDogRun();
thisDog->setIsHealthy(true);
thisDog->DoDogRun();
delete thisDog;
thisDog = 0;
return 0;
}
This code serves the purpose of more practice using classes and pointers to hold instances of the object.
I then started working on trying to better understand derived classes and how they function. I have somewhat of an idea out of the gate, having some experience with Python classes. However, it works a little different with its syntax. To start let me tell you what a derived class is. It's a class that shares protected information with a parent class, making it a sort of child. This derived class can be invoked separately from the base class when written properly. It's used for multi-stepped projects or to keep regular users from accessing private variables you wouldn't want to be changed.
The example given to explain this is probably one of my favorites, as it involves frozen pizza haha.
class FrozenFood {
private:
int Price;
protected:
int Weight;
public:
FrozenFood(int APrice, int Aweight);
int GetPrice();
int GetWeight();
virtual void BakeChemistry();
};
class FrozenPizza : public FrozenFood { //This a good example of a derived class from the Frozen Food
protected:
int Diameter;
public:
FrozenPizza(int APrice, int AWeight, int ADiameter);
void DumpInfo();
void BakeChemistry();
};
class DeepDishPizza : public FrozenPizza {
private:
int Height;
public:
DeepDishPizza(int APrice, int AWeight, int ADiameter, int AHeight);
void DumpDensity();
void BakeChemistry();
};
//Frozen Food Functions
FrozenFood::FrozenFood(int APrice, int AWeight) {
Price = APrice;
Weight = AWeight;
}
int FrozenFood::GetPrice() {
return Price;
}
int FrozenFood::GetWeight() {
return Weight;
}
void FrozenFood :: BakeChemistry()
{
cout << "To Bake it put into Oven for 20-40 Minutes" << endl;
}
//Frozen Pizza Functions
FrozenPizza::FrozenPizza(int APrice, int AWeight, int ADiameter) : FrozenFood(APrice, AWeight) {
Diameter = ADiameter;
};
void FrozenPizza :: BakeChemistry()
{
cout << "To Bake it put into Oven for 20 Minutes" << endl;
}
void FrozenPizza :: DumpInfo() {
cout << "\tFrozen pizza info" << endl;
cout << "\t\tWeight:" << Weight << "ounces" << endl;
cout << "\t\tDiameter" << Diameter << " inches" << endl;
}
//DeepDish Pizza Functions
DeepDishPizza::DeepDishPizza(int APrice, int AWeight, int ADiameter, int AHeight) : FrozenPizza(APrice, AWeight, ADiameter) {
Height = AHeight;
}
void DeepDishPizza::DumpDensity() {
//Calculate the pounds per cubic food of deep dish pizza
cout << "\tDensity: ";
cout << Weight * 12 * 12 * 12 * 14 / (Height * Diameter * 22 * 16);
cout << " pounds per cubic foot" << endl;
}
void DeepDishPizza :: BakeChemistry()
{
cout << "To Bake it put into Oven for 30 Minutes" << endl;
}
void Bake(FrozenFood *)
{
cout << "Cooking" << endl;
}
int main()
{
cout << "Thin crust pepperoni" << endl;
FrozenPizza pepperoni(450, 12, 14);
pepperoni.DumpInfo();
pepperoni.BakeChemistry();
cout << "\tPrice " << pepperoni.GetPrice() << " cents" << endl;
cout << "Deep Dish extra-cheese" << endl;
DeepDishPizza extracheese(550, 21592, 14, 3);
extracheese.DumpInfo();
extracheese.DumpDensity();
extracheese.BakeChemistry();
cout << "\tPrice: " << extracheese.GetPrice() << " cents" << endl;
return 0;
}
That's all I would like to share for this week, next week expect something a little more theoretical, like code design patterns. Until then, Peace 💣
No comments:
Post a Comment