Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Initialization List Practice | Constructors and Destructors
C++ OOP

book
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.

Рішення

cpp

solution

#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
#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;
}

Запитати АІ

expand
ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

some-alt