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

Contenido del Curso

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.

Tarea

  • 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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 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.

Tarea

  • 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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 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.

Tarea

  • 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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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.

Tarea

  • 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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 5
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt