exercise 1 - creating classes

This commit is contained in:
Alexander Zhirov 2021-06-20 22:46:32 +03:00
parent c451149748
commit 74c1f5598b
2 changed files with 125 additions and 0 deletions

110
lesson_2/exercise_1.hpp Normal file
View File

@ -0,0 +1,110 @@
#ifndef EXERCISE_1_HPP_
#define EXERCISE_1_HPP_
#include <iostream>
enum Gender
{
GENDER_MALE,
GENDER_FEMALE
};
class Person
{
private:
std::string m_name;
int m_age;
Gender m_gender;
float m_weight;
public:
Person(std::string name, int age, Gender gender, float weight) :
m_name(name), m_age(age), m_gender(gender), m_weight(weight) {}
void setName(std::string name = "")
{
if (name == "")
{
std::cout << "Введите новое имя: ";
std::cin >> m_name;
}
else
m_name = name;
}
void setAge(int age = 0)
{
if (age == 0)
{
std::cout << "Введите возраст: ";
std::cin >> m_age;
}
else
m_age = age;
}
void setWeight(float weight = 0.0)
{
if (weight == 0)
{
std::cout << "Введите вес: ";
std::cin >> m_weight;
}
else
m_weight = weight;
}
void printInfo() const
{
std::cout << "Имя: " << m_name << std::endl
<< "Возраст: " << m_age << std::endl
<< "Пол: " << (m_gender == GENDER_MALE ? "муж" : "жен") << std::endl
<< "Вес: " << m_weight << std::endl;
}
};
class Student : public Person
{
private:
static int count;
int m_yos; // year of study
public:
Student(std::string name, int age, Gender gender, float weight, int yos) :
Person(name, age, gender, weight), m_yos(yos)
{
count++;
}
static void printCount()
{
std::cout << "Количество студентов: " << count << std::endl;
}
void setYearStudy(int yos = 0)
{
if (yos == 0)
{
std::cout << "Введите год обучения: ";
std::cin >> m_yos;
}
else
m_yos = yos;
}
void incYear()
{
m_yos++;
}
void printInfo() const
{
Person::printInfo();
std::cout << "Год обучения: " << m_yos << std::endl;
}
~Student()
{
count--;
}
};
#endif

15
lesson_2/main.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <iostream>
#include "exercise_1.hpp"
using namespace std;
int Student::count = 0;
int main()
{
Student Alex("Олег", 20, GENDER_MALE, 75.2, 2020);
Alex.printInfo();
Student::printCount();
return 0;
}