Initialization List Practice
Завдання
Swipe to start coding
- Rewrite constructor using the initialization list instead of assignment statements in the constructor body.
- Ensure that the order of initialization matches the order of declaration for the class member variables.
Рішення
solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
class Rectangle {
public:
Rectangle(float _length, float _width, float scale = 1)
:length(_length * scale), width(_width * scale), area(length * width) { }
float length, width, area;
};
int main()
{
Rectangle rect(2, 4, 1.5);
std::cout << rect.area;
}
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 2. Розділ 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
class Rectangle {
public:
Rectangle(float _length, float _width, float scale = 1)
{
length = _length * scale;
width = _width * scale;
area = length * width;
}
float length, area, width;
};
int main()
{
Rectangle rect(2, 4, 1.5);
std::cout << rect.area;
}
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат