exercise 2

This commit is contained in:
Alexander Zhirov 2021-06-21 14:22:07 +03:00
parent 496554687c
commit 87404d829e
2 changed files with 86 additions and 7 deletions

68
lesson_2/exercise_2.hpp Normal file
View File

@ -0,0 +1,68 @@
#ifndef EXERCISE_2_HPP_
#define EXERCISE_2_HPP_
#include <iostream>
class Fruit
{
private:
std::string m_name;
std::string m_color;
public:
void setName(std::string name)
{
m_name = name;
}
void setColor(std::string color)
{
m_color = color;
}
const std::string& getName() const
{
return m_name;
}
const std::string& getColor() const
{
return m_color;
}
};
class Apple : public Fruit
{
public:
Apple(std::string color = "")
{
if (color == "")
color = "green";
setName("apple");
setColor(color);
}
};
class Banana : public Fruit
{
public:
Banana(std::string color = "")
{
if (color == "")
color = "yellow";
setName("banana");
setColor(color);
}
};
class GrannySmith : public Apple
{
public:
GrannySmith()
{
setName("Granny Smith " + Apple::getName());
}
};
#endif

View File

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "exercise_1.hpp" #include "exercise_1.hpp"
#include "exercise_2.hpp"
using namespace std; using namespace std;
@ -23,15 +24,25 @@ int main()
// students[2].printInfo(); // students[2].printInfo();
vector<Student> students; // vector<Student> students;
//
// students.emplace_back("Олег", 20, GENDER_MALE, 75.2, 2020);
// students.emplace_back("Андрей", 19, GENDER_MALE, 72.8, 2020);
// students.emplace_back("Анастасия", 20, GENDER_FEMALE, 55.2, 2020);
// students.emplace_back("Ольга", 21, GENDER_FEMALE, 49.3, 2020);
// students.emplace_back("Владимир", 19, GENDER_MALE, 69.9, 2020);
//
// Student::printCount();
students.emplace_back("Олег", 20, GENDER_MALE, 75.2, 2020); // Exercise 2
students.emplace_back("Андрей", 19, GENDER_MALE, 72.8, 2020);
students.emplace_back("Анастасия", 20, GENDER_FEMALE, 55.2, 2020);
students.emplace_back("Ольга", 21, GENDER_FEMALE, 49.3, 2020);
students.emplace_back("Владимир", 19, GENDER_MALE, 69.9, 2020);
Student::printCount(); Apple a("red");
Banana b;
GrannySmith c;
std::cout << "My " << a.getName() << " is " << a.getColor() << ".\n";
std::cout << "My " << b.getName() << " is " << b.getColor() << ".\n";
std::cout << "My " << c.getName() << " is " << c.getColor() << ".\n";
return 0; return 0;
} }