Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Methods of the Class | Fundamentals of OOP in C++
C++ OOP

bookChallenge: Methods of the Class

Methods in a class are essentially just functions that are defined within the class. They are used to define the behaviors or actions that objects of the class can perform.

Methods often manipulate the attributes of the class or perform operations that are relevant to the objects.

Implementation of Methods Outside the Class

Methods can be defined outside the class declaration using the scope resolution operator (::). This is often done to separate the declaration in the header file from its implementation in the source file. Here's how you would do it:

Example.h

Example.h

Example.cpp

Example.cpp

copy
1234
class Example { public: void Method(); };

It's not mandatory to create two distinct files for this purpose; you can achieve it within a single file, and in certain situations, it proves to be beneficial.

main.cpp

main.cpp

copy
12345678
#include <iostream> class Example { public: void Method(); }; void Example::Method() { std::cout << "Method was called" << std::endl; }

Feel free to tackle the task using the method of your preference. But the common good practice is to separate declaration and implementation.

Task

Swipe to start coding

Imagine you are building a simple system to calculate the price of a product with a discount applied. The system should also enforce a special rule: no discount can exceed 50%, even if a higher value is provided.

You have a class Product with the following fields:

  • price of type float
  • discount of type float

You need to implement a method getFinalPrice() that:

  1. Checks the discount value: if it is greater than 50%, set it to 50%.
  2. Calculates the final price and returns it:
    • Inside parentheses, calculate the discount amount using the formula (price * discount / 100)
    • Subtract the discount amount from the price to get the final price after discount

Example

price = 100.0, discount = 60.0 -> 50.0
price = 80.0, discount = 30.0 -> 56.0
price = 50.0, discount = 20.0 -> 40.0

Solution

solution.cpp

solution.cpp

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 3.13

bookChallenge: Methods of the Class

Swipe to show menu

Methods in a class are essentially just functions that are defined within the class. They are used to define the behaviors or actions that objects of the class can perform.

Methods often manipulate the attributes of the class or perform operations that are relevant to the objects.

Implementation of Methods Outside the Class

Methods can be defined outside the class declaration using the scope resolution operator (::). This is often done to separate the declaration in the header file from its implementation in the source file. Here's how you would do it:

Example.h

Example.h

Example.cpp

Example.cpp

copy
1234
class Example { public: void Method(); };

It's not mandatory to create two distinct files for this purpose; you can achieve it within a single file, and in certain situations, it proves to be beneficial.

main.cpp

main.cpp

copy
12345678
#include <iostream> class Example { public: void Method(); }; void Example::Method() { std::cout << "Method was called" << std::endl; }

Feel free to tackle the task using the method of your preference. But the common good practice is to separate declaration and implementation.

Task

Swipe to start coding

Imagine you are building a simple system to calculate the price of a product with a discount applied. The system should also enforce a special rule: no discount can exceed 50%, even if a higher value is provided.

You have a class Product with the following fields:

  • price of type float
  • discount of type float

You need to implement a method getFinalPrice() that:

  1. Checks the discount value: if it is greater than 50%, set it to 50%.
  2. Calculates the final price and returns it:
    • Inside parentheses, calculate the discount amount using the formula (price * discount / 100)
    • Subtract the discount amount from the price to get the final price after discount

Example

price = 100.0, discount = 60.0 -> 50.0
price = 80.0, discount = 30.0 -> 56.0
price = 50.0, discount = 20.0 -> 40.0

Solution

solution.cpp

solution.cpp

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
single

single

some-alt