Challenge: 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.cpp
1234class 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
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.
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:
priceof typefloatdiscountof typefloat
You need to implement a method getFinalPrice() that:
- Checks the discount value: if it is greater than 50%, set it to 50%.
- 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
priceto get the final price after discount
- Inside parentheses, calculate the discount amount using the formula
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
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.13
Challenge: 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.cpp
1234class 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
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.
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:
priceof typefloatdiscountof typefloat
You need to implement a method getFinalPrice() that:
- Checks the discount value: if it is greater than 50%, set it to 50%.
- 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
priceto get the final price after discount
- Inside parentheses, calculate the discount amount using the formula
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
Thanks for your feedback!
single