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

Зміст курсу

C++ OOP

C++ OOP

1. Fundamentals of OOP
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

bookMethods of the Class

Understanding methods

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.

Example

Typical methods a class Car might include:

         accelerate()
         brake()
         honk()

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:

h

Example

cpp

Example

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.

cpp

main

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.

Завдання
test

Swipe to show code editor

  • Implement area() and perimeter() methods in a Square class.
  • Call this methods using instance of the class in the main function.
  • Output the area and perimeter of the square object using methods you created.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
toggle bottom row

bookMethods of the Class

Understanding methods

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.

Example

Typical methods a class Car might include:

         accelerate()
         brake()
         honk()

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:

h

Example

cpp

Example

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.

cpp

main

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.

Завдання
test

Swipe to show code editor

  • Implement area() and perimeter() methods in a Square class.
  • Call this methods using instance of the class in the main function.
  • Output the area and perimeter of the square object using methods you created.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
toggle bottom row

bookMethods of the Class

Understanding methods

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.

Example

Typical methods a class Car might include:

         accelerate()
         brake()
         honk()

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:

h

Example

cpp

Example

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.

cpp

main

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.

Завдання
test

Swipe to show code editor

  • Implement area() and perimeter() methods in a Square class.
  • Call this methods using instance of the class in the main function.
  • Output the area and perimeter of the square object using methods you created.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Understanding methods

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.

Example

Typical methods a class Car might include:

         accelerate()
         brake()
         honk()

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:

h

Example

cpp

Example

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.

cpp

main

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.

Завдання
test

Swipe to show code editor

  • Implement area() and perimeter() methods in a Square class.
  • Call this methods using instance of the class in the main function.
  • Output the area and perimeter of the square object using methods you created.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 1. Розділ 5
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt