C++C++11C++14C++17Introduction to C++Learn C++

Discover Class Methods in C++

Object-Oriented Programming is a way to integrate with objects that might contain data (in the form of attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. Do you want to learn how to define a method in a class, inside a class, or outside of a class? Here we go,

A Method in a Class is a function that operates inside or outside of this belonging class. Methods are defined in a class in two ways, Inside of the class or at the outside of the class. Let’s see them,

1. Defining a Method Inside a Class

In this method, we will declare and define a method inside the class. Let’s define a THuman Class for a human and let’s define an Info() method inside a class that tells what it is,

Note that here THuman is a class and info() is the method of this class. Any objects created by this class example will have all properties and methods of this class including this info() method. We can use this class to create a new object from this class and we can get information from this class by using its info() method as in this full example below,

2. Defining a Method Outside of a Class

In this method we will define a method outside of the class and we will include its declaration inside of the class. If you want to learn more about definition and declaration in functions please see our previous post. As in example below, now let’s define our function outside. To do this we should declare it inside the Class as below,

Now our class has info function, but this function has no definition, we need a codeblock , statement that tells what this function will do. To define this function as a member of this class we should use ‘::’ operator to point this function is a method of that class. In a general form, a function, a method of class should be described by using ‘::’ operator, as given in below,

Thus, our info() method should be declared and defined outside like this,

This below is the full example of method declaration outside of a Class,

3. Defining a Method with Parameters Inside a Class

We can define a info() method with a parameter inside a class, This example below prints out the name given in this info() function,

4. Defining a Method with Parameters Outside of a Class

We can define a info() method with a parameter outside of a class, This example below prints out the name given in this info() function,

Get started building powerful apps with C++Builder!

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, and Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++

Worth reading...
Learn About Parameters Passed by Reference in Functions