Lesson 2
Defining and Utilizing Methods
Lesson Introduction

Welcome! Today, we're diving into an essential topic in C++ programming: defining and utilizing methods. Methods, also known as member functions, are functions inside a class that can access its members. By the end of this lesson, you'll know how to define methods and call these methods to perform operations.

Importance of Methods

Why do we need methods? Imagine you have a robot. This robot can do tasks like walking, talking, and picking up items. In programming, these tasks are methods that belong to the robot class. Methods help organize our code, making it easier to manage and understand.

Defining a Method

Let's define a simple class with a method. In C++, methods are defined in a class.

Here's an example:

C++
1#include <iostream> 2 3class Robot { 4public: 5 // Method to make the robot talk 6 void talk() { 7 std::cout << "Hello, I am a robot!" << std::endl; // Output: Hello, I am a robot! 8 } 9};

Here, we have a Robot class with a talk() method that prints a message to the console.

Implementing Methods Outside the Class

To keep the class definition clean, methods are usually implemented outside the class using the scope resolution operator ::.

Here's an example:

C++
1#include <iostream> 2 3class Robot { 4public: 5 void talk(); 6}; 7 8void Robot::talk() { 9 std::cout << "Hello, I am a robot!" << std::endl; // Output: Hello, I am a robot! 10}

In this example, the talk() method is declared inside the class but implemented outside, keeping the definition simpler.

Example Code Walkthrough: Part 1

You can also add parameters to the methods. Let's create a Calculator class with a method to add numbers.

C++
1#include <iostream> 2 3class Calculator { 4public: 5 // Method to add two numbers 6 int add(int a, int b) { 7 return a + b; 8 } 9};

In this code, we define a Calculator class with an add method.

Example Code Walkthrough: Part 2

Let's add more functionality to the Calculator class by including a subtract method.

C++
1#include <iostream> 2 3class Calculator { 4public: 5 // Method to add two numbers 6 int add(int a, int b) { 7 return a + b; 8 } 9 10 // Method to subtract two numbers 11 int subtract(int a, int b) { 12 return a - b; 13 } 14};
Utilizing a Method

To use a method, create an instance of the class and call the method on it. Let's see an example:

C++
1int main() { 2 Calculator calc; 3 int result1 = calc.add(3, 5); 4 int result2 = calc.subtract(10, 4); 5 6 std::cout << "Addition: " << result1 << std::endl; // Output: Addition: 8 7 std::cout << "Subtraction: " << result2 << std::endl; // Output: Subtraction: 6 8 9 return 0; 10}

Here, we create an instance of our Calculator class. Then, we utilize its methods add and subtract to get results. As you can see, these methods work just like functions, but they are the part of the class, which makes the code more structured.

Lesson Summary

Congratulations! You’ve learned how to define and use methods in C++. Here are the key points:

  1. Defining Methods: Methods are defined in a class and can be implemented outside using the scope resolution operator ::.
  2. Using Methods: To use a method, create a class instance and call the method using dot notation.

Now, it's time to practice! Next, you'll get hands-on experience by creating classes with methods and calling these methods. This will help reinforce the concepts and improve your skills in defining and using methods in C++. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.