You are on page 1of 2

Приклад створення класу

// ClassFirst.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается


выполнение программы.
//

#include "pch.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class Cat // оголошення класу


{
// тіло класу
string name; // поля
int year_of_birth;
double Length;
public:
void GetInfo()
{
cout << "Cat " << name << " has length " << Length << ", his age " << 2020
- year_of_birth << endl;
}
// constructors
Cat()
{
cout << "Default constructor run" << endl;
name = "Vasya";
year_of_birth = 1;
Length = 10;

}
Cat(string _name, int _year_of_birth, double _lenght)
{
cout << "Parametrized constructor run" << endl;
name = _name;
year_of_birth = _year_of_birth;
Length = _lenght;

}
~Cat()
{
cout << "Destructor run" << endl;
}
double GetLenght()
{
return Length;
}
void SetLenght(double _lenght)
{
Length = _lenght;
}
}; // кінець оголошення класу

int main()
{
Cat cat1, cat2("Barsic", 2016, 25);
cat1.GetInfo();
cat2.GetInfo();
cat1.SetLenght(30);
cat1.GetInfo();
_getch();
}

You might also like