diff options
5 files changed, 121 insertions, 21 deletions
| -rw-r--r-- | Big4AndInheritance/AlcoholDrink.cpp | 17 | ||||
| -rw-r--r-- | Big4AndInheritance/AlcoholDrink.h | 11 | ||||
| -rw-r--r-- | Big4AndInheritance/Drink.cpp | 63 | ||||
| -rw-r--r-- | Big4AndInheritance/Drink.h | 41 | ||||
| -rw-r--r-- | Big4AndInheritance/main.cpp | 10 |
diff --git a/Big4AndInheritance/AlcoholDrink.cpp b/Big4AndInheritance/AlcoholDrink.cpp new file mode 100644 index 0000000..a876a0b --- /dev/null +++ b/Big4AndInheritance/AlcoholDrink.cpp @@ -0,0 +1,17 @@ +include "AlcoholDrink.h"
+
+
+AlcoholDrink::AlcoholDrink(const char* mark, int ml, int percentAlcohol) : Drink(mark,ml)
+{
+ setAlcohol(percentAlcohol);
+}
+void AlcoholDrink::setAlcohol(int alcohol)
+{
+ if (alcohol < 4 || alcohol>98)
+ alcohol = 20;
+ percentAlcohol = alcohol;
+}
+int AlcoholDrink::getAlcohol() const
+{
+ return alcohol;
+}
\ No newline at end of file diff --git a/Big4AndInheritance/AlcoholDrink.h b/Big4AndInheritance/AlcoholDrink.h new file mode 100644 index 0000000..fdac1bd --- /dev/null +++ b/Big4AndInheritance/AlcoholDrink.h @@ -0,0 +1,11 @@ +#include "Drink.h"
+
+class AlcoholDrink : public Drink
+{
+private:
+ int percentAlcohol;
+
+public:
+ AlcoholDrink(const char* mark, int ml, int percentAlcohol);
+ void setAlcohol(int alcohol);
+};
diff --git a/Big4AndInheritance/Drink.cpp b/Big4AndInheritance/Drink.cpp new file mode 100644 index 0000000..0e5bc77 --- /dev/null +++ b/Big4AndInheritance/Drink.cpp @@ -0,0 +1,63 @@ +#include "Drink.h"
+
+
+void Drink::CopyFrom(const Drink& other)
+{
+ int len = strlen(other.mark);
+ mark = new char[len + 1];
+ strcpy(mark, other.mark);
+
+ ml = other.ml;
+}
+void Drink::Free()
+{
+ delete[] mark;
+ ml = 0;
+}
+
+Drink::Drink()
+{
+ mark = new char[6];
+ strcpy(mark, "Water");
+ ml = 500;
+}
+Drink::Drink(const char* mark, int ml)
+{
+ int len = strlen(mark);
+ this->mark = new char[len + 1];
+ strcpy(this->mark, mark);
+
+ setML(ml);
+
+}
+Drink::Drink(const Drink& other)
+{
+ CopyFrom(other);
+}
+Drink& Drink::operator=(const Drink& other)
+{
+ if (this != &other)
+ {
+ Free();
+ CopyFrom(other);
+ }
+ return *this;
+}
+Drink::~Drink()
+{
+ Free();
+}
+void Drink::setML(int ml)
+{
+ if (ml < 100 || ml> 1000)
+ ml = 500;
+ this->ml = ml;
+}
+int Drink::getML() const
+{
+ return ml;
+}
+const char* Drink::getMark() const
+{
+ return mark;
+}
\ No newline at end of file diff --git a/Big4AndInheritance/Drink.h b/Big4AndInheritance/Drink.h index ffab919..0132e16 100644 --- a/Big4AndInheritance/Drink.h +++ b/Big4AndInheritance/Drink.h @@ -1,21 +1,20 @@ -#include <iostream> -using namespace std; - -class Drink -{ -private: - char* mark; - int ml; - void CopyFrom(const Drink& other); - void Free(); -public: - Drink(); - Drink(const char* mark, int ml); - Drink(const Drink& other); - Drink& operator=(const Drink& other); - ~Drink(); - - void setML(int ml); - int getML() const; - const char* getMark() const; -}; +#include <iostream>
+
+class Drink
+{
+private:
+ char* mark;
+ int ml;
+ void CopyFrom(const Drink& other);
+ void Free();
+public:
+ Drink();
+ Drink(const char* mark, int ml);
+ Drink(const Drink& other);
+ Drink& operator=(const Drink& other);
+ ~Drink();
+
+ void setML(int ml);
+ int getML() const;
+ const char* getMark() const;
+};
\ No newline at end of file diff --git a/Big4AndInheritance/main.cpp b/Big4AndInheritance/main.cpp new file mode 100644 index 0000000..e3e43e2 --- /dev/null +++ b/Big4AndInheritance/main.cpp @@ -0,0 +1,10 @@ +#include "AlcoholDrink.h"
+int main()
+{
+ AlcoholDrink DR1("Spirt", 100, 98);
+
+ AlcoholDrink dr2 (DR1);
+ dr2.getMark();
+
+
+}
\ No newline at end of file |
